-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-slave-checkweb.php
64 lines (50 loc) · 1.98 KB
/
mysql-slave-checkweb.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
<?php
/****************************************************************************
mysql-slave-checkweb.php 0.1
Copyright 2011 Mario Witte
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Report bugs to: [email protected]
02/25/2011 v0.1
****************************************************************************/
/* settings */
$mysql_host = 'localhost'; // host:port or socket path
$mysql_user = 'root';
$mysql_pass = '';
/* operations */
/* DON'T CHANGE ANYTHING BELOW THIS LINE */
// mysql connect
$dbh = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
if (!$dbh) {
ex("CRITICAL", "cannot connect to $mysql_host");
}
$query = 'SHOW SLAVE STATUS';
$result = mysql_query($query, $dbh);
$resultset = mysql_fetch_assoc($result);
if (sizeof($resultset) <= 1) {
ex("CRITICAL", "no slave seems to be running");
}
$ok_io = ($resultset['Slave_IO_Running'] == 'Yes' ? true : false);
$ok_sql = ($resultset['Slave_SQL_Running'] == 'Yes' ? true : false);
$delay = $resultset['Seconds_Behind_Master'];
if ($ok_io and $ok_sql and $delay < 60) {
ex("OK", "");
} elseif ($ok_io and $ok_sql and $delay >= 60) {
ex("WARNING", "slave running, but has a delay of $delay seconds");
} else {
ex("CRITICAL", "slave stopped: IO ".($ok_io ? 'running' : 'stopped').', SQL '.($ok_sql ? 'running' : 'stopped'));
}
exit;
function ex($state, $msg) {
echo $state.($msg ? ' - '.$msg : '');
exit;
}