forked from sa2blv/SVXportal
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstation_heartbeat.php
294 lines (173 loc) · 6.52 KB
/
station_heartbeat.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/*This script is executed on the server
* with watch -n 1 logdeamon.php
*
*/
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/
include_once 'config.php';
include_once 'function.php';
define_settings();
$file = 'tmp/cache_json.txt';
// Create connection
$conn = new mysqli($host, $user, $password, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// this is a multplier that detect if noode is down and how many times it is down 20sec * $downtime_time
$downtime_time = 4;
function check_if_dead($call) {
global $conn;
global $counter_down;
global $downtime_time;
$sql ="SELECT `Station_Down` , `Station_Down_timmer_count` FROM RefletorStations where Callsign='$call'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
return $row['Station_Down'];
}
function check_if_dead_time($call) {
global $conn;
global $counter_down;
global $downtime_time;
$sql ="SELECT `Station_Down` , `Station_Down_timmer_count` FROM RefletorStations where Callsign='$call'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if($row['Station_Down'] == 1)
{
if($row['Station_Down_timmer_count'] >= $downtime_time)
{
$count1 = $downtime_time+1;
$sql1 ="UPDATE RefletorStations SET Station_Down_timmer_count = $count1 WHERE Callsign = '".$call."'";
}
else
{
$sql1 ="UPDATE RefletorStations SET Station_Down_timmer_count = Station_Down_timmer_count + 1 WHERE Callsign = '".$call."'";
}
$conn->query($sql1);
}
return $row['Station_Down_timmer_count'];
}
function check_if_sendmail($call) {
global $conn;
global $downtime_time;
$sql ="SELECT `Station_Down` , `Station_Down_timmer_count` FROM RefletorStations where Callsign='$call'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if($row['Station_Down_timmer_count'] == ($downtime_time+1))
{
return 1;
}
return 0;
}
function send_mail($email,$node,$type)
{
if($type == 1)
{
$subject = "Node $node down";
$message = "The node $node has lost the connection to reflector !";
}
else
{
$subject = "Node $node up";
$message = "the node $node has connected to reflector !";
}
$to = SYSMATER_MAIL;
$headers = 'From: '.SYSTEM_MAIL.'' . "\r\n" .
'Reply-To:'.SYSTEM_MAIL.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if($email !="")
{
mail($email, $subject, $message, $headers);
}
mail($to, $subject, $message, $headers);
}
function wrie_to_cache($json) {
global $file;
$myfile = fopen($file, "w");
fwrite($myfile, $json);
fclose($myfile);
}
function read_cache()
{
global $file;
if(!is_file($file)){
$contents = 'This is a test!'; // Some simple example content.
file_put_contents($file, $contents); // Save our content to the file.
}
$myfile = fopen($file, "r") ;
$json = fread($myfile,filesize("cache_json.txt"));
fclose($myfile);
return $json;
}
// Get data from server
$json_data = file_get_contents($serveradress);
$json = json_decode($json_data);
echo "check data to write!";
wrie_to_cache($json_data);
mysqli_set_charset($conn,"utf8");
$sql ="SELECT `Callsign` FROM RefletorStations where Monitor=1";
$result = $conn->query($sql);
$i=1;
while($row = $result->fetch_assoc())
{
$Station_callsings_fromdb[$i]= $row["Callsign"];
$i++;
}
$find_array =$Station_callsings_fromdb;
//var_dump($json->nodes);
foreach ($json->nodes as $key => $value) {
// $arr[3] will be updated with each value from $arr...
//var_dump($key);
//var_dump($value);
//echo "{$key} => {$value} ";
//print_r($arr);
// instert station to db if note exist
$array_key = array_search($key,$find_array);
unset($find_array[$array_key]);
if($array_key)
{
if(check_if_dead($key) == 1)
{
echo $key ." is has joned reflektor ";
// set station is restored
$sql = "INSERT INTO `RefletorNodeLOG` (`Id`, `Callsign`, `Type`, `Active`, `Talkgroup`, `NODE`, `Siglev`, `Duration`, `IsTalker`,`Nodename`,`Talktime`) VALUES (NULL, '".$key."', '3', '0', '0', '', '0', '0', '0', '','0');";
$conn->query($sql);
if(check_if_sendmail($key) == 1 )
{
if(SEND_MAIL_TO_SYSOP ==1)
{
send_mail("",$key,0);
}
}
$sql1 ="UPDATE RefletorStations SET Station_Down_timmer_count = 0 WHERE Callsign = '".$key."'";
$conn->query($sql1);
}
$sql_insert = "UPDATE `RefletorStations` SET Last_Seen=now(), Station_Down='0' WHERE Callsign='$key'";
$conn->query($sql_insert);
}
}
foreach ($find_array as $key => $value)
{
if(check_if_dead($value) == 0)
{
echo $value ." is has dropt from reflektor ";
$sql = "INSERT INTO `RefletorNodeLOG` (`Id`, `Callsign`, `Type`, `Active`, `Talkgroup`, `NODE`, `Siglev`, `Duration`, `IsTalker`,`Nodename`,`Talktime`) VALUES (NULL, '".$value."', '3', '1', '0', '', '0', '0', '0', '','0');";
$conn->query($sql);
}
$sql_insert = "UPDATE `RefletorStations` SET Station_Down='1' WHERE Callsign='$value'";
$conn->query($sql_insert);
if(check_if_dead_time($value) == 4)
{
echo "test";
if(SEND_MAIL_TO_SYSOP ==1)
{
echo " Send mail test";
send_mail("",$value,1);
}
}
}
$conn->close();
?>