-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.php
416 lines (376 loc) · 16 KB
/
collector.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* Collector File Doc Comment
*
* @category Data_Collector
* @package MyMon
* @author himaster <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://mymon.pkwteile.de
*/
error_reporting(E_ALL);
require_once 'config.php';
require_once 'functions.php';
declare(ticks=1);
set_error_handler('errHandler');
pcntl_signal(SIGTERM, 'sigHandler');
$dbconnection = new mysqli($dbhost, $dbusername, $dbpass, $database);
if ($dbconnection->connect_errno) {
printf("MySQL connection error: %s\n", $dbconnection->connect_error);
exit();
}
$result = $dbconnection->query("SELECT ip, servername, db, mysql, err, el, mon, red, git
FROM $database.`stats`;");
$dbconnection->close();
while ($array = $result->fetch_assoc()) {
$pid = pcntl_fork();
if ($pid == -1) {
die("Child process can't be created");
} elseif ($pid) {
} else {
child_();
exit;
}
}
$result->free();
$pid = pcntl_fork();
if ($pid == -1) {
die("Child process can't be created");
} elseif ($pid) {
} else {
botips_();
exit;
}
exit;
function botips_() {
global $balancerip;
global $ssh_callbacks;
global $retry_num;
global $docroot;
global $loglevel;
global $dbhost;
global $dbusername;
global $dbpass;
global $database;
$i = 1;
if ($loglevel > 1) {
common_log("botips_ - started.");
}
start:
if (( ! $balancer_ssh_conn = ssh2_connect($balancerip, 22, $ssh_callbacks))
or ( ! ssh2_auth_pubkey_file($balancer_ssh_conn, 'root', $docroot.'/id_rsa.pub', $docroot.'/id_rsa', ''))) {
common_log("botips_ - retry #".$i++.".");
sleep(1);
if ($i < $retry_num) {
goto start;
} else {
exit(1);
}
}
$dbconnection = new mysqli($dbhost, $dbusername, $dbpass, $database);
if ($dbconnection->connect_errno) {
printf("MySQL connection error: %s\n", $dbconnection->connect_error);
exit();
}
$i = 31;
foreach (botips($balancer_ssh_conn) as $value) {
$query = "INSERT INTO $database.`botips` (id, amount, ipaddr)
VALUES (".--$i.", ".$value['amount'].", '".$value['ipaddr']."')
ON DUPLICATE KEY UPDATE `amount` = ".$value['amount'].", `ipaddr` = '".$value['ipaddr']."';";
$result = $dbconnection->query($query);
if (!isset($result)) {
common_log("botips_ - not updated!");
}
}
unset($balancer_ssh_conn);
if ($loglevel > 1) {
common_log("botips_ - ended.");
}
}
function child_() {
global $array;
global $stop_server;
global $docroot;
global $loglevel;
global $ssh_callbacks;
global $retry_num;
global $dbhost;
global $dbusername;
global $dbpass;
global $database;
$serverip = $array["ip"];
$servername = $array["servername"];
$errs = $array["err"];
$elastic = $array["el"];
$db = $array["db"];
$mysql = $array["mysql"];
$mon = $array["mon"];
$red = $array["red"];
$git = $array["git"];
$i = 1;
if ($loglevel > 1) {
common_log($servername. " - started.");
}
start:
if (( ! $server_ssh_conn = ssh2_connect($serverip, 22, $ssh_callbacks))
or ( ! ssh2_auth_pubkey_file($server_ssh_conn, 'root', $docroot.'/id_rsa.pub', $docroot.'/id_rsa', ''))) {
common_log($servername." - retry #".$i++.".");
sleep(1);
if ($i < $retry_num) {
goto start;
} else {
exit(1);
}
}
$dbconnection = new mysqli($dbhost, $dbusername, $dbpass, $database);
if ($dbconnection->connect_errno) {
printf("MySQL connection error: %s\n", $dbconnection->connect_error);
exit();
}
$value = la($server_ssh_conn, $serverip, $servername);
$query = "UPDATE `$database`.`stats`
SET `la`='".$value."', `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - LA not updated!");
}
$value = ($db == 1)?$dbconnection->escape_string(rep($server_ssh_conn, $serverip, $servername)):"";
$query = "UPDATE `$database`.`stats`
SET `rep`='".$value."', `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ($loglevel > 1) {
common_log($servername." - ".$query);
}
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - REP not updated!");
}
$value = ($errs == 1)?err500($server_ssh_conn, $serverip, $servername):"";
$query = "UPDATE `$database`.`stats`
SET `500`='" .$value."', `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - 500 not updated!");
}
$value = ($elastic == 1)?elastic($server_ssh_conn, $serverip, $servername):"";
$query = "UPDATE `$database`.`stats`
SET `elastic`='" .$value."', `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - ELASTIC not updated!");
}
$value = ($mysql == 1)?locks($server_ssh_conn, $serverip, $servername):"";
$query = "UPDATE `$database`.`stats`
SET `locks`='" .$value."', `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - LOCKS not updated!");
}
$value = ($mon == 1)?mongo($server_ssh_conn, $serverip, $servername):"";
$query = "UPDATE `$database`.`stats`
SET `mongo`='" .$value."', `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - MONGO not updated!");
}
$value = ($red == 1)?redis($server_ssh_conn, $serverip, $servername):"";
$query = "UPDATE `$database`.`stats`
SET `redis`='" .$value."' , `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - REDIS not updated!");
}
$value = ($git == 1)?repo($server_ssh_conn, $serverip, "prod")."' , `test_repo`='" .repo($server_ssh_conn, $serverip, "dev"):"' , `test_repo`='";
$query = "UPDATE `$database`.`stats`
SET `master_repo`='" .$value."' , `timestamp`=CURRENT_TIMESTAMP
WHERE `ip`='" .$serverip. "';";
var_bump($query);
if ( ! $result = $dbconnection->query($query)) {
common_log($servername." - git not updated!");
}
$dbconnection->close();
unset($server_ssh_conn);
if ($loglevel > 1) {
common_log($servername. " - ended.");
}
}
function la($ssh_conn, $serverip, $servername = null) {
$la_string = substr(strrchr(ssh2_return($ssh_conn, "/usr/bin/uptime"), ":"), 1);
$la = floatval(array_map("trim", explode(",", $la_string))[0]);
$core = intval(ssh2_return($ssh_conn, "grep -c processor /proc/cpuinfo"));
$percent = intval($la/$core*100);
if ($percent < 75) {
$fontcolor = "<span style=\"color: green;\">";
} elseif (($percent >= 75) && ($percent < 100)) {
$fontcolor = "<span style=\"color: #CAC003\">";
} else {
$fontcolor = "<span style=\"color: red\">";
}
return "<a title=\"".$la_string."\"
href=\"/index.php?task=top&serverip=" .$serverip. "\"
target=\"_blank\">" .$fontcolor. "<b>" .$percent. "%</b></span>\n</a>";
}
function rep($ssh_conn, $serverip, $servername = null) {
global $loglevel;
$data = array();
$str = ssh2_return($ssh_conn, "printf %s \"$(mysql -e 'show slave status\G' | awk 'FNR>1')\"");
foreach (explode("\n", $str) as $cLine) {
if (strpos($cLine, "Timeout") != false) {
return "<font color=\"red\">".strpos($cLine, "Timeout")." - stopped</font>";
}
list($cKey, $cValue) = explode(':', "$cLine:");
$data[trim($cKey)] = trim($cValue);
}
$onclick = "";
if ($loglevel > 0) {
common_log($servername.' - SQL :'.$data["Slave_SQL_Running"].' IO :'.$data["Slave_IO_Running"]);
}
if (array_key_exists("Slave_SQL_Running", $data) && ($data["Slave_SQL_Running"] == "Yes")) {
$sqlfontcolor = "<font color=\"green\">";
$sql = "✓";
} else {
slackbot($servername.": replication SQL problem");
$sqlfontcolor = "<script type=\"text/javascript\">notify(\"$servername: replication SQL problem\");</script>".
"<font color=\"red\">";
$sql = "x";
$onclick = "onclick=\"javascript: if(event.ctrlKey || event.metaKey) {
if(confirm('Want to RESTART replication?')) {
replica_restart('" .$serverip. "');
}
}
else {
if(confirm('Want to skip one error and start?')) {
replica_repair('" .$serverip. "');
}
}
return false;\"";
}
if (array_key_exists("Slave_IO_Running", $data) && ($data["Slave_IO_Running"] == "Yes")) {
$iofontcolor = "<font color=\"green\">";
$io = "✓";
} else {
slackbot($servername.": replication IO problem");
$iofontcolor = "<script type=\"text/javascript\">notify(\"$servername: replication IO problem\");</script>".
"<font color=\"red\">";
$io = "x";
$onclick = "onclick=\"javascript: if(event.ctrlKey || event.metaKey) {
if(confirm('Want to RESTART replication?')) {
replica_restart('" .$serverip. "');
}
}
else {
if(confirm('Want to skip one error and start?')) {
replica_repair('" .$serverip. "');
}
}
return false;\"";
}
if (array_key_exists("Seconds_Behind_Master", $data) && ($data["Seconds_Behind_Master"] == "0")) {
$deltafontcolor = "<font color=\"green\">";
$delta = "0";
} elseif ((array_key_exists("Seconds_Behind_Master", $data) && ($data["Seconds_Behind_Master"] == "NULL"))
|| ! array_key_exists("Seconds_Behind_Master", $data)) {
$deltafontcolor = "<font color=\"red\">";
$delta = "x";
} else {
$deltafontcolor = "<font color=\"red\">";
$delta = $data["Seconds_Behind_Master"];
}
return "<a title=\"".(array_key_exists("Last_SQL_Error", $data) ? $data["Last_SQL_Error"]:'').(array_key_exists("Last_IO_Error", $data) ? $data["Last_IO_Error"]:'')."\"
href=\"#\"". $onclick . " >
SQL: " .$sqlfontcolor. "<b>" .$sql. "</b></font>
IO: " .$iofontcolor. "<b>" .$io. "</b></font>
Δ: " .$deltafontcolor. "<b>" .$delta. "</b></font>\n</a>";
}
function err500($ssh_conn, $serverip, $servername = null) {
$str = trim(ssh2_return($ssh_conn, "cat /var/log/500err.log"));
return '<a title="Click to show 500 errors" href="/index.php?task=500err&serverip='.$serverip.'" target="_self">'.$str.'</a>';
}
function elastic($ssh_conn, $serverip, $servername = null) {
$str = ssh2_return($ssh_conn, "date1=\$((\$(date +'%s%N') / 1000000));
hostname=\$(ip -f inet addr show `[ \"\$HOSTNAME\" == \"front5.pkwteile.de\" ] && echo \"em2\" || echo \"eth1\"` | grep -Po 'inet \K[\d.]+')
curl -sS -o /dev/null -XGET http://\$hostname:9200/_cluster/health?pretty;
date2=\$((\$(date +'%s%N') / 1000000));
echo -n \$((\$date2-\$date1));");
if ($str == "Timeout") {
$fontcolor = "<script type=\"text/javascript\">notify(\"".$servername.": elastic problem\");</script>
<font color=\"red\">";
} else {
$fontcolor = "<font color=\"green\">";
}
return $fontcolor.$str. " ms</font>";
}
function locks($ssh_conn, $serverip, $servername = null) {
$query = "SELECT info FROM INFORMATION_SCHEMA.PROCESSLIST WHERE state LIKE '%lock%' AND time > 30";
$locked = trim(ssh2_return($ssh_conn, "mysql -Ne \"".$query."\" | wc -l"));
$query = "SHOW STATUS WHERE variable_name = 'Threads_connected'";
$conns = trim(ssh2_return($ssh_conn, "mysql -Nse \"".$query."\" | awk '{print $2}'"));
if ($locked === "Timeout") {
$locked = "T";
}
if ($conns === "Timeout") {
$conns = "T";
}
if (($locked == "0") and ($conns < "5000")) {
$fontcolor = "<font color=\"green\">";
} else {
slackbot($servername.": DB locks");
$fontcolor = "<script type=\"text/javascript\">notify(\"".$servername.": DB locks\");</script>
<font color=\"red\">";
}
return $fontcolor.$conns. " / " .$locked. "</font>";
}
function mongo($ssh_conn, $serverip, $servername = null) {
$str = ssh2_return($ssh_conn, "date1=\$((\$(date +'%s%N') / 1000000));
mongo admin --quiet --eval 'printjson(db.serverStatus().connections.current)' 1>/dev/null;
date2=\$((\$(date +'%s%N') / 1000000));
echo -n \$((\$date2-\$date1));");
if ($str == "Timeout") {
slackbot($servername.": mongo problem");
$fontcolor = "<script type=\"text/javascript\">notify(\"".$servername.": mongo problem\");</script>
<font color=\"red\">";
} else {
$fontcolor = "<font color=\"green\">";
}
return $fontcolor.$str. " ms</font>";
}
function redis($ssh_conn, $serverip, $servername = null) {
$str = ssh2_return($ssh_conn, "date1=\$((\$(date +'%s%N') / 1000000));
redis-cli info 1>/dev/null;
date2=\$((\$(date +'%s%N') / 1000000));
echo -n \$((\$date2-\$date1));");
if ($str == "Timeout") {
slackbot($servername.": redis problem");
$fontcolor = "<script type=\"text/javascript\">notify(\"".$servername.": redis problem\");</script>
<font color=\"red\">";
} else {
$fontcolor = "<font color=\"green\">";
}
return $fontcolor.$str. " ms</font>";
}
function repo($ssh_conn, $serverip, $repository) {
$str = ssh2_return($ssh_conn, "cd /home/developer/www/fuel.$repository/ && git rev-parse HEAD");
#if ($str == "Timeout") {
# slackbot($servername.": redis problem");
# $fontcolor = "<script type=\"text/javascript\">notify(\"".$servername.": redis problem\");</script>
# <font color=\"red\">";
#} else {
# $fontcolor = "<font color=\"green\">";
#}
return $str;
}
function botips($ssh_conn) {
global $iplistnum;
$str = ssh2_return($ssh_conn, "tail -n 1000000 /var/log/nginx/access.log |
awk '{print $1}' |
sort |
uniq -c |
sort -n |
tail -n".$iplistnum);
$i = 0;
foreach (explode("\n", rtrim($str, "\n")) as $cLine) {
$i++;
$cLine = trim($cLine);
list($ipaddrarray[$i]['amount'], $ipaddrarray[$i]['ipaddr']) = explode(' ', "$cLine ");
}
return $ipaddrarray;
}