forked from hien/status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.php
75 lines (65 loc) · 2.88 KB
/
cron.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
<?php
/*
This file is part of the status project.
The status project is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The status project is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with the status project. If not, see <http://www.gnu.org/licenses/>.
*/
if (isset($argv[1])) $conf = $argv[1]; else $conf = "config.php";
require $conf;
require 'Net/Ping.php';
try {
$db = new PDO('sqlite:'. $db);
} catch (PDOException $e) {
die('Unable to connect to the database.'. $e);
}
$dbs = $db->prepare("SELECT * FROM servers WHERE disabled = 0");
$result = $dbs->execute();
if ($result) {
$ra = $dbs->fetchAll(PDO::FETCH_ASSOC);
foreach ($ra as $i => $row) {
$fp = @fsockopen($row['hostname'], $port, $errno, $errstr, 5);
if (!$fp) {
$ping = Net_Ping::factory();
$ping->setArgs(array('count' => 8));
$pr = $ping->ping($row['hostname']);
if ($pr->_loss == "0") {
updateserver(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, $row['uid']);
if ($row['status'] == '0') {
mail($email, 'Server '. $row['uid'] .' is up!', 'Server '. $row['uid'] .' on node '. $row['node'] .' is up. That rocks!');
}
} else {
updateserver(0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, $row['uid']);
if ($row['status'] == '1') {
mail($email, 'Server '. $row['uid'] .' is down', 'Server '. $row['uid'] .' on node '. $row['node'] .' is down. That sucks!');
}
}
} else {
$result = fgets($fp, 2048);
@fclose($fp);
$result = json_decode($result, true);
updateserver(1, $result['uplo']['uptime'], $result['ram']['total'], $result['ram']['used'], $result['ram']['free'], $result['ram']['bufcac'], $result['uplo']['load1'], $result['uplo']['load5'], $result['uplo']['load15'], $row['uid']);
if ($row['status'] == "0") {
mail($email, 'Server '. $row['uid'] .' is up', 'Server '. $row['uid'] .' on node '. $row['node'] .' is up. That rocks!');
}
}
}
}
function updateserver($status, $uptime, $mtotal, $mused, $mfree, $mbuffers, $load1, $load5, $load15, $uid) {
global $db;
try {
$dbs = $db->prepare('UPDATE servers SET time = ?, status = ?, uptime = ?, mtotal = ?, mused = ?, mfree = ?, mbuffers = ?, load1 = ?, load5 = ?, load15 = ? WHERE uid = ?');
$dbs->execute(array(time(), $status, $uptime, $mtotal, $mused, $mfree, $mbuffers, $load1, $load5, $load15, $uid));
} catch (PDOException $e) {
echo $e;
die('Something broke!');
}
}
?>