File check_replication_slave of Package nagios-plugins-various
1
#!/usr/bin/php
2
<?php
3
//#####################################################
4
//# check_replication_slave - Nagios Plugin to check #
5
//# state of mysql slave replication server. #
6
//# #
7
//# By Yunus Bookwala <yunusbookwala@gmail.com> #
8
//#####################################################
9
error_reporting(0);
10
11
if ( $argc < 3 ) {
12
print "Usage: $argv[0] DB_HOST DB_USER DB_PASS\n";
13
print "$argc\n";
14
exit;
15
}
16
17
$db_host = "$argv[1]";
18
$db_user = "$argv[2]";
19
$db_pass = "$argv[3]";
20
21
$warn_sec_behind_master = "60";
22
$critical_sec_behind_master = "120";
23
24
$errors = array (
25
"UNKNOWN" => -1,
26
"OK" => 0,
27
"WARNING" => 1,
28
"CRITICAL" => 2
29
);
30
31
$state = "UNKNOWN";
32
33
$link = mysql_connect($db_host, $db_user, $db_pass);
34
if (!$link) {
35
$errmesg = mysql_error();
36
$state = "CRITICAL";
37
print "$state: $errmesg\n";
38
exit ($errors[$state]);
39
};
40
41
$sql = "show slave status";
42
$result = mysql_query ( "$sql" );
43
if (!$result) {
44
$state = "UNKNOWN";
45
$errmesg = mysql_error();
46
print "$state: $errmesg\n";
47
exit ($errors[$state]);
48
}
49
50
$status = mysql_fetch_object( $result );
51
52
$status_slave_io_running = $status->Slave_IO_Running;
53
$status_slave_sql_running = $status->Slave_SQL_Running;
54
$status_seconds_behind_master = $status->Seconds_Behind_Master;
55
56
$answer = "SLAVE IO Running: $status_slave_io_running, SLAVE SQL Running: $status_slave_sql_running, $status_seconds_behind_master secs behind Master";
57
58
if ( "2" == checkcritical ( $status_slave_io_running, $status_slave_sql_running, $status_seconds_behind_master, $critical_sec_behind_master ) ) {
59
$state = 'CRITICAL';
60
} elseif ( "1" == checkwarning ( $status_seconds_behind_master, $warn_sec_behind_master ) ) {
61
$state = 'WARNING';
62
} elseif ( "0" == checkok ( $status_slave_io_running, $status_slave_sql_running, $status_seconds_behind_master, $warn_sec_behind_master ) ) {
63
$state = 'OK';
64
}
65
66
function checkcritical ( $status_slave_io_running, $status_slave_sql_running, $status_seconds_behind_master, $critical_sec_behind_master )
67
{
68
69
if ( "Yes" != $status_slave_io_running or "Yes" != $status_slave_sql_running or $critical_sec_behind_master < $status_seconds_behind_master) {
70
71
return 2;
72
}
73
return -1;
74
}
75
76
function checkwarning ( $status_seconds_behind_master, $warn_sec_behind_master )
77
{
78
if ( $warn_sec_behind_master < $status_seconds_behind_master ) {
79
return 1;
80
}
81
return -1;
82
}
83
84
function checkok ( $status_slave_io_running, $status_slave_sql_running, $status_seconds_behind_master, $warn_sec_behind_master )
85
{
86
if ( "Yes" == $status_slave_io_running and "Yes" == $status_slave_sql_running and $warn_sec_behind_master > $status_seconds_behind_master) {
87
return 0;
88
}
89
return -1;
90
}
91
92
print "$state: $answer\n";
93
exit ($errors[$state]);
94
?>
95