RrdLogController.php
2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Swpb\Bundle\CocarBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Swpb\Bundle\CocarBundle\Entity\Rrd;
class RrdLogController extends Controller
{
private $dir;
/**
* @Route("/rrdlog", name="cocar_rrdlog")
* @Template()
*/
public function rrdLogAction()
{
set_time_limit(0);
$this->dir = $this->get('kernel')->getRootDir() . "/../web/rrd/";
$em = $this->getDoctrine()->getManager();
$circuits = $em->getRepository('CocarBundle:Circuits')->findAll();
foreach($circuits as $cir)
{
$rrd = $cir->getId() . ".rrd";
$arq = $this->dir . $rrd;
if(file_exists($arq))
{
$end = (date('U')) - 120;
$start = $end - 86400;
$com = "rrdtool fetch $arq AVERAGE --start $start --end $end | sed -e \"s/ds[01]//g\" |
sed \"s/nan/0/g\" | tr \":\" \" \" | tr -s \" \" | sed -e \"s/ \$//\" | grep -v \"^\$\"";
$lines = explode("\n", shell_exec($com));
$codInt = strtr($cir->getId(), ".", "_");
for ($i=0; $i < count($lines); $i++)
{
$fields = explode(" ", $lines[$i]);
$date = new \DateTime();
$date->setTimestamp(intval($fields[0]));
$volIn = $this->calc(isset($fields[1]) ? $fields[1] : 0);
$volOut = $this->calc(isset($fields[2]) ? $fields[2] : 0);
if ($date->format('Y-m-d H:i:s') != "1970-01-01 00:00:00" && $date->format('Y-m-d H:i:s') != "1969-12-31 21:00:00")
{
$rrdLog = new Rrd();
$rrdLog->setDatetime($date);
$rrdLog->setCodeInterface($codInt);
$rrdLog->setVolumeIn($volIn);
$rrdLog->setVolumeOut($volOut);
$em->persist($rrdLog);
}
$em->flush();
}
}
}
return new Response();
}
public function calc($value)
{
$value = strtr($value, ",", ".");
settype ($value, "double");
return round($value, 1);
}
}