-
Notifications
You must be signed in to change notification settings - Fork 6
/
uptime.php
112 lines (108 loc) · 5.28 KB
/
uptime.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
<?PHP
include "config.inc.php";
$db_handle = mysqli_connect($DBServer, $DBUser, $DBPassword);
$db_found = mysqli_select_db($db_handle, $DBName);
if ($db_found) {
$SQL = "select * from servers";
$result = mysqli_query($db_handle, $SQL);
while ($db_field = mysqli_fetch_assoc($result)) {
$id = $db_field['id'];
$lastup = $db_field['lastup'];
$device = $db_field['device'];
$ip = $db_field['ip'];
$downs = $db_field['downs'];
$Email_sent = $db_field['Email_Sent'];
$date = date("Y-m-d H:i:s");
$up = pingtest($ip);
$online = $up ? 'online' : 'offline';
if ($online == 'online') {
if ($Email_sent == 'yes') {
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
set_include_path("." . PATH_SEPARATOR . ($UserDir = dirname($_SERVER['DOCUMENT_ROOT'])) . "/pear/php" . PATH_SEPARATOR . get_include_path());
require_once "Mail.php";
$host = 'ssl://'.$smtp;
$port = $smtp_port;
$to = $admin_email;
$email_from = $smtp_username;
$email_subject = "PStatus - Device Up - " . $device;
$email_body = $device . " has recovered";
$headers = array(
'From' => $email_from,
'To' => $to,
'Subject' => $email_subject
);
$smtp = Mail::factory('smtp', array(
'debug'=> true,
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $smtp_username,
'password' => $smtp_password
));
$mail = $smtp->send($to, $headers, $email_body);
//$response = http_get("sendpush.php?Title=Pstatus - Device Up - " . $device . "&Message=Device Recovered Alert", array("timeout"=>1), $info);
}
$SQL2 = "UPDATE servers SET count = count + 1, ups = ups + 1, downs = '0', state = 'online', Email_sent = '', lastup = '" . $date . "' WHERE id = '" . $id . "'";
} else {
if ($downs + 1 == $alert_limit) {
$SQL2 = "UPDATE servers SET count = count + 1, downs = downs + 1, Email_Sent = 'yes', lastdown = '" . $date . "' WHERE id = '" . $id . "'";
//$response = http_get("sendpush.php?Title=Pstatus - Device Down - " . $device . "&Message=Device Down Alert", array("timeout"=>1), $info);
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
set_include_path("." . PATH_SEPARATOR . ($UserDir = dirname($_SERVER['DOCUMENT_ROOT'])) . "/pear/php" . PATH_SEPARATOR . get_include_path());
require_once "Mail.php";
$host = 'ssl://'.$smtp;
$port = $smtp_port;
$to = $admin_email;
$email_from = $smtp_username;
$email_subject = "PStatus - Device Down - " . $device;
$email_body = $device . " has not replied to " . $alert_limit . " ping request(s). Last time seen was " . $lastup . ".";
$headers = array(
'From' => $email_from,
'To' => $to,
'Subject' => $email_subject,
);
$smtp = Mail::factory('smtp', array(
'debug'=> true,
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $smtp_username,
'password' => $smtp_password
));
$mail = $smtp->send($to, $headers, $email_body);
} else {
$SQL2 = "UPDATE servers SET count = count + 1, downs = downs + 1, state = 'offline', lastdown = '" . $date . "' WHERE id = '" . $id . "'";
}
}
if (mysqli_query($db_handle, $SQL2)) {
echo "Record updated successfully<br>";
} else {
echo "Error updating record: " . mysqli_error($db_handle);
}
}
$SQL3 = "select * from smartdevices";
$result = mysqli_query($db_handle, $SQL3);
while ($db_field = mysqli_fetch_assoc($result)) {
$id = $db_field['id'];
$ip = $db_field['ip'];
$date = date("Y-m-d H:i:s");
$up = pingtest($ip);
$online = $up ? 'online' : 'offline';
if ($online == 'online') {
$SQL4 = "UPDATE smartdevices SET count = count + 1, ups = ups + 1, lastup = '" . $date . "' WHERE id = '" . $id . "'";
} else {
$SQL4 = "UPDATE smartdevices SET count = count + 1, downs = downs + 1, lastdown = '" . $date . "' WHERE id = '" . $id . "'";
}
if (mysqli_query($db_handle, $SQL4)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($db_handle);
}
}
}
function pingtest($ip)
{
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($ip)), $errorNo, $errorStr);
return $errorStr === 0;
}
?>