AlarmController.php
4.04 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?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;
class AlarmController extends Controller
{
private $dir;
/**
* @Route("/generatealarm", name="cocar_generatealarm")
* @Template()
*/
public function generateAlarmAction()
{
$this->dir = $this->get('kernel')->getRootDir() . "/../web/rrd/";
$em = $this->getDoctrine()->getManager();
$circuits = $em->createQuery(
"SELECT c FROM CocarBundle:Circuits c
WHERE c.generateAlarm = :alarm
AND c.manages <> :manage
AND c.status = 'UP'"
)
->setParameter('alarm', true)
->setParameter('manage', 'Entidades Externas')
->getResult();
foreach($circuits as $cir)
{
$media_in = 0;
$media_out = 0;
$qtd = 0;
$rrd = $cir->getId() . ".rrd";
if(file_exists($this->dir . $rrd))
{
$fim = (date('U')) - 60;
$inicio = $fim - 720;
$com = "rrdtool fetch $this->dir$rrd AVERAGE --start $inicio --end $fim | sed -e \"s/ds[01]//g\" |
sed \"s/nan/0/g\" | tr \":\" \" \" | tr -s \" \" | sed -e \"s/ \$//\" | grep -v \"^\$\" |tail -10";
$linhas = explode("\n", shell_exec($com));
for ($i=0; $i < count($linhas) - 1; $i++)
{
$campos = explode(" ", $linhas[$i]);
$data = strftime("%Y-%m-%d %H:%M:%S",$campos[0]);
$vol_in = $this->calc($campos[1]);
$vol_out = $this->calc($campos[2]);
if ($data != "1970-01-01 00:00:00" && $data != "1969-12-31 21:00:00")
{
$media_in += $vol_in;
$media_out += $vol_out;
$qtd++;
}
}
$history = strtoupper($cir->getHistory());
$hist_novo = "N";
if ($qtd != 0)
{
# INICIO DO TRATAMENTO COMO CIRCUITO
$media_in = round(($media_in * 0.008)/$qtd, 1);
$media_out = round(($media_out * 0.008)/$qtd, 1);
if (strtolower($cir->getTypeInterface()) == "circuito" && (!preg_match("/ETH/i", $cir->getSerialBackbone())))
{
$cir_in = round(($cir->getCirIn() * 1.2), 1);
$cir_out = round(($cir->getCirOut() * 1.2), 1);
if ($media_in > $cir_in || $media_out > $cir_out)
{
if($history != "A1" && $history != "A2")
$hist_novo = "A1";
elseif($history == "A1" || $history == "A2")
$hist_novo = "A2";
}
elseif($media_out == 0.0 || $media_in == 0.0 )
{
if($history != "Z1" && $history != "Z2" )
$hist_novo = "Z1";
elseif($history == "Z1" || $history == "Z2")
$hist_novo = "Z2";
}
}
else
{
# INICIO DO TRATAMENTO COMO CONCENTRADORA
$porta = round(($cir->getSpeed() * 0.85), 1);
if ($media_in > $porta || $media_out > $porta)
{
if ($history != "A1" && $history != "A2")
$hist_novo = "A1";
elseif($history == "A1" || $history == "A2")
$hist_novo = "A2";
}
elseif ($media_out == 0.0 || $media_in == 0.0 )
{
if($history != "Z1" && $history != "Z2")
$hist_novo = "Z1";
elseif($history == "Z1" || $history == "Z2")
$hist_novo = "Z2";
}
}
}
else
{
$arq = strftime("%d-%m-%Y", $fim);
$fp = fopen($this->dir."logs/".$arq.".log", 'a');
$info = "Arquivo RRD: $rrd";
}
$cir->setHistory($hist_novo);
$em->persist($cir);
$em->flush();
}
}
return new Response();
}
/**
* @Route("/endalarm", name="cocar_endalarm")
* @Template()
*/
public function endAlarmAction()
{
$em = $this->getDoctrine()->getManager();
$circuits = $em->getRepository('CocarBundle:Circuits')->findAll();
foreach ($circuits as $cir) {
$cir->setHistory('N');
$em->persist($cir);
$em->flush();
}
return new Response();
}
public function calc($value)
{
$value = strtr($value, ",", ".");
settype ($value, 'double');
return round($value,1);
}
}