forked from K4Y5/Ariane-Monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.php
197 lines (152 loc) · 8.16 KB
/
API.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
<?php
include 'pages/functions.php';
$error = false;
if ($_POST["KEY"] == NULL) {
die("Key not set");
}
$key = $_POST['KEY'];
$ip = $_POST['IP'];
$uptime = $_POST['UPTIME'];
$kernel = $_POST['KERNEL'];
$cpu = $_POST['CPU'];
$cpu_cores = $_POST['CPU_CORES'];
$cpu_mhz = $_POST['CPU_MHZ'];
$cpu_usage = $_POST['CPU_USAGE'];
$cpu_steal = $_POST['CPU_STEAL'];
$io_wait = $_POST['IO_WAIT'];
$cpu_steal = str_replace(' ', '', $cpu_steal);
$io_wait = str_replace(' ', '', $io_wait);
$io_wait = str_replace('wa', '', $io_wait);
$memory_total = $_POST['RAM_TOTAL'];
$memory_free = $_POST['RAM_FREE'];
$memory_cached = $_POST['RAM_CACHED'];
$memory_buffer = $_POST['RAM_BUFFER'];
$memory_active = $_POST['RAM_ACTIVE'];
$memory_inactive = $_POST['RAM_INACTIVE'];
$hdd_usage = $_POST['HDD_USAGE'];
$hdd_total = $_POST['HDD_TOTAL'];
$tx = $_POST['TX'];
$rx = $_POST['RX'];
if(!preg_match("/^[a-zA-Z0-9]+$/",$key)){ die("Key contains invalid Letters!");}
if ($ip != "") {
if(!filter_var($ip, FILTER_VALIDATE_IP)) { die("Invalid IP!"); }
}
$success = true;
$stmt = $database->prepare("SELECT id,server_name,user_id FROM servers WHERE server_key = ? LIMIT 1");
$stmt->bind_param('s', $key);
if (!$stmt->execute()) { $success = false; }
$stmt->bind_result($server_id,$server_name,$user_id);
$stmt->fetch();
$stmt->close();
if ($success == false) {
die ("MySQL Error");
}
if (empty($server_id)) {
die("Invalid Key!");
}
if(!is_numeric($uptime)){ addLog($user_id,1,"Uptime contains invalid Letters!"); die("Uptime contains invalid Letters!");}
if(!preg_match("/^[A-Za-z0-9.-]+$/",$key)){ addLog($user_id,1,"Kernel contains invalid Letters!"); die("Kernel contains invalid Letters!");}
if(!preg_match("/^[A-Za-z0-9 ()@.-]+$/",$cpu)){ addLog($user_id,1,"CPU Name contains invalid Letters!"); die("CPU Name contains invalid Letters!");}
if(!is_numeric($cpu_cores)){ addLog($user_id,1,"CPU Cores contains invalid Letters!"); die("CPU contains invalid Letters!");}
if(!is_numeric($cpu_mhz)){ addLog($user_id,1,"CPU Mhz contains invalid Letters!"); die("CPU contains invalid Letters!");}
if(!is_numeric($cpu_usage)){ addLog($user_id,1,"CPU Usage contains invalid Letters!"); die("CPU contains invalid Letters!");}
if(!is_numeric($cpu_steal)){ addLog($user_id,1,"CPU Steal contains invalid Letters!"); die("CPU contains invalid Letters!");}
if(!is_numeric($io_wait)){ addLog($user_id,1,"I/O Wait contains invalid Letters!"); die("IO contains invalid Letters!");}
if(!is_numeric($memory_total)){ addLog($user_id,1,"Memory Total contains invalid Letters!"); die("Memory contains invalid Letters!");}
if(!is_numeric($memory_free)){ addLog($user_id,1,"Memory Free contains invalid Letters!"); die("Memory contains invalid Letters!");}
if(!is_numeric($memory_cached)){ addLog($user_id,1,"Memory Cached invalid Letters!"); die("Memory contains invalid Letters!");}
if(!is_numeric($memory_buffer)){ addLog($user_id,1,"Memory Buffer contains invalid Letters!"); die("Memory contains invalid Letters!");}
if(!is_numeric($memory_active)){ addLog($user_id,1,"Memory Active contains invalid Letters!"); die("Memory contains invalid Letters!");}
if(!is_numeric($memory_inactive)){ addLog($user_id,1,"Memory Inactive contains invalid Letters!"); die("Memory contains invalid Letters!");}
if(!is_numeric($hdd_usage)){ addLog($user_id,1,"HDD Usage contains invalid Letters!"); die("HDD contains invalid Letters!");}
if(!is_numeric($hdd_total)){ addLog($user_id,1,"HDD Total contains invalid Letters!"); die("HDD contains invalid Letters!");}
if(!is_numeric($tx)){ addLog($user_id,1,"Network TX contains invalid Letters!"); die("NET contains invalid Letters!");}
if(!is_numeric($rx)){ addLog($user_id,1,"Network RX contains invalid Letters!"); die("NET contains invalid Letters!");}
$stmt = $database->prepare("SELECT server_uptime FROM servers WHERE id = ? LIMIT 1");
$stmt->bind_param('i', $server_id);
if (!$stmt->execute()) { $success = false; }
$stmt->bind_result($db_uptime_before);
$stmt->fetch();
$stmt->close();
$last_update = time();
//Update IP/Uptime/CPU/Cores....
$stmt = $database->prepare("UPDATE servers SET server_ip = ?,server_uptime = ?,server_kernel = ?,server_cpu = ?,server_cpu_cores = ?,server_cpu_mhz = ?, last_update = ? WHERE id = ?");
$stmt->bind_param('ssssidii',$ip,$uptime,$kernel,$cpu,$cpu_cores,$cpu_mhz,$last_update,$server_id);
if (!$stmt->execute()) { $success = false; }
$stmt->close();
if ($success == false) {
addLog($user_id,1,"MySQL Error after Updating IP/Uptime...");
die ("MySQL Error");
}
$server_time = time();
$stmt = $database->prepare("SELECT server_tx,server_rx FROM servers_data WHERE server_id = ? ORDER by id DESC LIMIT 1");
$stmt->bind_param('i', $server_id);
if (!$stmt->execute()) { $success = false; }
$stmt->bind_result($db_server_tx,$db_server_rx);
$stmt->fetch();
$stmt->close();
if ($success == false) {
addLog($user_id,1,"MySQL Error after selecting TX/RX...");
die ("MySQL Error");
}
$tx_diff = $tx - $db_server_tx;
$rx_diff = $rx - $db_server_rx;
$stmt = $database->prepare("INSERT INTO servers_data(server_id,memory_total,memory_free,memory_cached,memory_buffer,server_tx,server_rx,cpu_load,server_timestamp,server_tx_diff,server_rx_diff,memory_active,memory_inactive,hdd_usage,hdd_total,cpu_steal,io_wait) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?, ?, ?)");
$stmt->bind_param('iiiiiiididdiiiidd', $server_id,$memory_total,$memory_free,$memory_cached,$memory_buffer,$tx,$rx,$cpu_usage,$server_time,$tx_diff,$rx_diff,$memory_active,$memory_inactive,$hdd_usage,$hdd_total,$cpu_steal,$io_wait);
if (!$stmt->execute()) { $success = false; }
$stmt->close();
if ($success == false) {
addLog($user_id,1,"MySQL Error after Insert new Data...");
die ("MySQL Error");
}
//Limit Check
$stmt = $database->prepare("SELECT cpu_alert,cpu_alert_send,cpu_steal_alert,cpu_steal_alert_send,io_wait_alert,io_wait_alert_send,reboot_alert FROM servers WHERE id = ? LIMIT 1");
$stmt->bind_param('i', $server_id);
if (!$stmt->execute()) { $success = false; }
$stmt->bind_result($db_cpu,$db_cpu_send,$db_cpu_steal,$db_cpu_steal_send,$db_io_wait,$db_io_wait_send,$db_reboot_alert);
$stmt->fetch();
$stmt->close();
if ($success == false) {
die ("MySQL Error");
}
//CPU alert
if ($cpu_usage >= $db_cpu AND $db_cpu != NULL AND $db_cpu_send <= time()) {
$msg = "Alert: The CPU Load of the Server ". escape($server_name) . " has reached " . $cpu_usage . "%";
$headers = "From: "._email_sender."\r\n";
mail(_email_target,"Ariane - CPU Load Alert - " . escape($server_name),$msg,$headers);
$lock = strtotime('+30 minutes', time());
$stmt = $database->prepare("UPDATE servers SET cpu_alert_send = ? WHERE id = ?");
$stmt->bind_param('ii',$lock,$server_id);
$stmt->execute();
$stmt->close();
}
//CPU Steal Alert
if ($cpu_steal >= $db_cpu_steal AND $db_cpu_steal != NULL AND $db_cpu_steal_send <= time()) {
$msg = "Alert: The CPU Steal of the Server ". escape($server_name) . " has reached " . $cpu_steal . "%";
$headers = "From: "._email_sender."\r\n";
mail(_email_target,"Ariane - CPU Steal Alert - " . escape($server_name),$msg,$headers);
$lock = strtotime('+30 minutes', time());
$stmt = $database->prepare("UPDATE servers SET cpu_steal_alert_send = ? WHERE id = ?");
$stmt->bind_param('ii',$lock,$server_id);
$stmt->execute();
$stmt->close();
}
//I/O Alert
if ($io_wait >= $db_io_wait AND $db_io_wait != NULL AND $db_io_wait_send <= time()) {
$msg = "Alert: The I/O Load of the Server ". escape($server_name) . " has reached " . $io_wait . "%";
$headers = "From: "._email_sender."\r\n";
mail(_email_target,"Ariane - I/O Wait Alert - " . escape($server_name),$msg,$headers);
$lock = strtotime('+30 minutes', time());
$stmt = $database->prepare("UPDATE servers SET io_wait_alert_send = ? WHERE id = ?");
$stmt->bind_param('ii',$lock,$server_id);
$stmt->execute();
$stmt->close();
}
//Reboot Alert
if ($db_uptime_before > $uptime AND $db_reboot_alert == 1) {
$msg = "Alert: The Server ". escape($server_name) . " has been rebooted.";
$headers = "From: "._email_sender."\r\n";
mail(_email_target,"Ariane - Reboot Alert - " . escape($server_name),$msg,$headers);
}
echo "Okay";
?>