-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmon_maria.pl
executable file
·230 lines (200 loc) · 4.71 KB
/
mon_maria.pl
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
#!/usr/bin/perl -w
# Check galera status
# (C) Mark Janssen -- Sig-I/O Automatisering
# 2013/09/10 -- Initial version
# 2017/01/21 -- Put config in variables for release
use strict;
use DBI;
# Hostname for Send-NSCA
my $hostname = qx/hostname/;
chomp($hostname);
# Connect to the database and keep using this connection
my $database = "mysql";
my $databasehost = "localhost";
my $databaseport = "3306";
my $checkuser = "galeracheck";
my $checkpassword = "replacewithyourpassword";
my $nagioshost = "nagios.local";
my $nscaconfig = "/usr/local/etc/send_nsca.cfg";
my $nscabin = "/usr/sbin/send_nsca";
my $galeraclustersize = "3";
my $dbh;
# Setup some globals
my $sth;
my $errstring = "";
my $errcount = 0;
my $loopcount = 0;
my $oldstatus = 0;
my $debug = 1;
my $downfile = "/tmp/mysql-down"; # Created when a check fails, to be used by keepalived
my $offfile = "/tmp/mysql-off"; # Created manually to force checks to fail, will create $downfile
my $pidfile = "/home/galeramon/mon_maria.pid";
sub writepid()
{
open( PID, ">$pidfile" ) or die "Can't open PIDFILE\n";
printf PID "$$";
close( PID) ;
}
sub connectdb()
{
if ( $dbh = DBI->connect("DBI:mysql:$database:$databasehost:$databaseport", $checkuser, $checkpassword ) )
{
$dbh->{mysql_auto_reconnect} = 1;
}
else
{
$errcount += 2;
$errstring = "Can't connect to database";
notifynsca();
exit 2;
}
}
# Tell Nagios (NSCA) what we found
sub notifynsca()
{
my $status = $errcount;
if ( $status > 2 )
{
$status = 2;
}
if ( $status == 0 )
{
$errstring = "OK"
}
open( NSCA, "|$nscabin -H $nagioshost -c $nscaconfig 2>&1 > /dev/null" ) or die "Can't open NSCA\n";
printf NSCA "$hostname\tgalera_check\t$status\t$errstring ($loopcount)\n";
print "$hostname\tgalera_check\t$status\t$errstring\n" if $debug;
close( NSCA) ;
}
writepid();
connectdb();
# Keep looping through the checks
while( 1 == 1 )
{
my @result;
#
# CHECK 1: show global status where variable_name="wsrep_local_state"
#
# Local state should be 4
$sth = $dbh->prepare('show global status where variable_name="wsrep_local_state"');
if (!$sth) {
$errcount++;
$errstring .= $dbh->errstr . " ";
}
if (!$sth->execute) {
$errcount++;
$errstring .= $dbh->errstr . " ";
}
@result = $sth->fetchrow_array();
$sth->finish();
if ( $result[1] != 4 )
{
$errcount++;
$errstring .= "Local state not OK ";
}
#
# CHECK 2: show global variables where variable_name="wsrep_on"
#
# wsrep_on should be on
$sth = $dbh->prepare('show global variables where variable_name="wsrep_on"');
if (!$sth) {
$errcount++;
$errstring .= $dbh->errstr . " ";
}
if (!$sth->execute) {
$errcount++;
$errstring .= $dbh->errstr . " ";
}
@result = $sth->fetchrow_array();
$sth->finish();
if ( $result[1] ne 'ON' )
{
$errcount++;
$errstring .= "WSREP not ON ";
}
#
# CHECK 3: Cluster size
#
# wsrep_cluster_size should be 3 ($galeraclustersize)
$sth = $dbh->prepare('select variable_name,variable_value from information_schema.global_status where variable_name = "wsrep_cluster_size"');
if (!$sth) {
$errcount++;
$errstring .= $dbh->errstr . " ";
}
if (!$sth->execute) {
$errcount++;
$errstring .= $dbh->errstr . " ";
}
@result = $sth->fetchrow_array();
$sth->finish();
if ( $result[1] != '$galeraclustersize' )
{
$errcount++;
$errstring .= "Cluster size not $galeraclustersize ";
}
#
# CHECK 4: Override check
#
if ( -e $offfile )
{
$errcount++;
$errstring .= "MySQL manually forced off ";
}
else
{
if ( -e $downfile )
{
unlink $downfile;
}
}
if ( $errcount != 0 )
{
# Create downfile, so keepalived knows this node is not working correctly
open(MYSQLDOWN,">$downfile");
print MYSQLDOWN "";
close( MYSQLDOWN );
}
#
# After all the checks, report back to nagios if results are different
# or this is the 30th concurrent check with the same results.
# So we always tell nagios at least every 30 runs.
#
if ( $errcount > 0 )
{
print "$errcount errors encountered: $errstring\n";
if ( $oldstatus != $errcount )
{
notifynsca();
$loopcount = 0;
}
else
{
$loopcount++;
}
$oldstatus = $errcount;
$errcount = 0;
$errstring = "";
}
else
{
if ( $oldstatus != $errcount )
{
notifynsca();
$loopcount = 0;
}
else
{
$loopcount++;
}
$oldstatus = 0;
}
if ( ($loopcount % 30) == 0 )
{
notifynsca();
}
# Delay a bit before running again
sleep 1;
}
# Disconnect from the database... though we should never get here
$dbh->disconnect();
unlink $pidfile;