diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-03-15 19:54:32 +0000 |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-03-15 19:54:32 +0000 |
commit | 1c229b2c4572f1fcf5e1fe470f49df49111816f5 (patch) | |
tree | 6167387224b617a43edd091afec075b262113dc2 /plugins | |
parent | edf94c7d2a21142b0cf9613dbbc1ff7a48f456ef (diff) | |
download | monitoring-plugins-1c229b2c4572f1fcf5e1fe470f49df49111816f5.tar.gz |
Alert on amount of time a slave is behind (Steven Kreuzer)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1324 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_mysql.c | 81 | ||||
-rw-r--r-- | plugins/t/check_mysql.t | 11 |
2 files changed, 71 insertions, 21 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c index 45f86a9c..92ac7ff0 100644 --- a/plugins/check_mysql.c +++ b/plugins/check_mysql.c | |||
@@ -6,7 +6,7 @@ | |||
6 | * License: GPL | 6 | * License: GPL |
7 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) | 7 | * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) |
8 | * portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | 8 | * portions (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) |
9 | * | 9 | * |
10 | * $Id$ | 10 | * $Id$ |
11 | * | 11 | * |
12 | * Description: | 12 | * Description: |
@@ -16,10 +16,10 @@ | |||
16 | 16 | ||
17 | const char *progname = "check_mysql"; | 17 | const char *progname = "check_mysql"; |
18 | const char *revision = "$Revision$"; | 18 | const char *revision = "$Revision$"; |
19 | const char *copyright = "1999-2004"; | 19 | const char *copyright = "1999-2006"; |
20 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | 20 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; |
21 | 21 | ||
22 | #define SLAVERESULTSIZE 40 | 22 | #define SLAVERESULTSIZE 70 |
23 | 23 | ||
24 | #include "common.h" | 24 | #include "common.h" |
25 | #include "utils.h" | 25 | #include "utils.h" |
@@ -33,15 +33,16 @@ char *db_host = NULL; | |||
33 | char *db_pass = NULL; | 33 | char *db_pass = NULL; |
34 | char *db = NULL; | 34 | char *db = NULL; |
35 | unsigned int db_port = MYSQL_PORT; | 35 | unsigned int db_port = MYSQL_PORT; |
36 | int check_slave = 0; | 36 | int check_slave = 0, warn_sec = 0, crit_sec = 0; |
37 | int verbose = 0; | ||
38 | |||
39 | thresholds *my_threshold = NULL; | ||
37 | 40 | ||
38 | int process_arguments (int, char **); | 41 | int process_arguments (int, char **); |
39 | int validate_arguments (void); | 42 | int validate_arguments (void); |
40 | void print_help (void); | 43 | void print_help (void); |
41 | void print_usage (void); | 44 | void print_usage (void); |
42 | 45 | ||
43 | |||
44 | |||
45 | int | 46 | int |
46 | main (int argc, char **argv) | 47 | main (int argc, char **argv) |
47 | { | 48 | { |
@@ -137,37 +138,60 @@ main (int argc, char **argv) | |||
137 | 138 | ||
138 | } else { | 139 | } else { |
139 | /* mysql 4.x.x */ | 140 | /* mysql 4.x.x */ |
140 | int slave_io_field = -1 , slave_sql_field = -1, i, num_fields; | 141 | int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; |
141 | MYSQL_FIELD* fields; | 142 | MYSQL_FIELD* fields; |
142 | 143 | ||
143 | num_fields = mysql_num_fields(res); | 144 | num_fields = mysql_num_fields(res); |
144 | fields = mysql_fetch_fields(res); | 145 | fields = mysql_fetch_fields(res); |
145 | for(i = 0; i < num_fields; i++) | 146 | for(i = 0; i < num_fields; i++) { |
146 | { | 147 | if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { |
147 | if (0 == strcmp(fields[i].name, "Slave_IO_Running")) | ||
148 | { | ||
149 | slave_io_field = i; | 148 | slave_io_field = i; |
150 | continue; | 149 | continue; |
151 | } | 150 | } |
152 | if (0 == strcmp(fields[i].name, "Slave_SQL_Running")) | 151 | if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { |
153 | { | ||
154 | slave_sql_field = i; | 152 | slave_sql_field = i; |
155 | continue; | 153 | continue; |
156 | } | 154 | } |
155 | if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { | ||
156 | seconds_behind_field = i; | ||
157 | continue; | ||
158 | } | ||
157 | } | 159 | } |
158 | if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) | 160 | if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { |
159 | { | ||
160 | mysql_free_result (res); | 161 | mysql_free_result (res); |
161 | mysql_close (&mysql); | 162 | mysql_close (&mysql); |
162 | die (STATE_CRITICAL, "Slave status unavailable\n"); | 163 | die (STATE_CRITICAL, "Slave status unavailable\n"); |
163 | } | 164 | } |
164 | 165 | ||
165 | snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[slave_io_field], row[slave_sql_field]); | 166 | snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], row[seconds_behind_field]); |
166 | if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { | 167 | if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { |
167 | mysql_free_result (res); | 168 | mysql_free_result (res); |
168 | mysql_close (&mysql); | 169 | mysql_close (&mysql); |
169 | die (STATE_CRITICAL, "%s\n", slaveresult); | 170 | die (STATE_CRITICAL, "%s\n", slaveresult); |
170 | } | 171 | } |
172 | |||
173 | if (verbose >=3) { | ||
174 | if (seconds_behind_field == -1) { | ||
175 | printf("seconds_behind_field not found\n"); | ||
176 | } else { | ||
177 | printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]); | ||
178 | } | ||
179 | } | ||
180 | |||
181 | if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) { | ||
182 | double value = atof(row[seconds_behind_field]); | ||
183 | int status; | ||
184 | |||
185 | status = get_status(value, my_threshold); | ||
186 | |||
187 | if (status == STATE_WARNING) { | ||
188 | printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult); | ||
189 | exit(STATE_WARNING); | ||
190 | } else if (status == STATE_CRITICAL) { | ||
191 | printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult); | ||
192 | exit(STATE_CRITICAL); | ||
193 | } | ||
194 | } | ||
171 | } | 195 | } |
172 | 196 | ||
173 | /* free the result */ | 197 | /* free the result */ |
@@ -193,6 +217,8 @@ int | |||
193 | process_arguments (int argc, char **argv) | 217 | process_arguments (int argc, char **argv) |
194 | { | 218 | { |
195 | int c; | 219 | int c; |
220 | char *warning = NULL; | ||
221 | char *critical = NULL; | ||
196 | 222 | ||
197 | int option = 0; | 223 | int option = 0; |
198 | static struct option longopts[] = { | 224 | static struct option longopts[] = { |
@@ -201,6 +227,8 @@ process_arguments (int argc, char **argv) | |||
201 | {"username", required_argument, 0, 'u'}, | 227 | {"username", required_argument, 0, 'u'}, |
202 | {"password", required_argument, 0, 'p'}, | 228 | {"password", required_argument, 0, 'p'}, |
203 | {"port", required_argument, 0, 'P'}, | 229 | {"port", required_argument, 0, 'P'}, |
230 | {"critical", required_argument, 0, 'c'}, | ||
231 | {"warning", required_argument, 0, 'w'}, | ||
204 | {"check-slave", no_argument, 0, 'S'}, | 232 | {"check-slave", no_argument, 0, 'S'}, |
205 | {"verbose", no_argument, 0, 'v'}, | 233 | {"verbose", no_argument, 0, 'v'}, |
206 | {"version", no_argument, 0, 'V'}, | 234 | {"version", no_argument, 0, 'V'}, |
@@ -212,7 +240,7 @@ process_arguments (int argc, char **argv) | |||
212 | return ERROR; | 240 | return ERROR; |
213 | 241 | ||
214 | while (1) { | 242 | while (1) { |
215 | c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option); | 243 | c = getopt_long (argc, argv, "hvVSP:p:u:d:H:c:w:", longopts, &option); |
216 | 244 | ||
217 | if (c == -1 || c == EOF) | 245 | if (c == -1 || c == EOF) |
218 | break; | 246 | break; |
@@ -241,12 +269,21 @@ process_arguments (int argc, char **argv) | |||
241 | case 'S': | 269 | case 'S': |
242 | check_slave = 1; /* check-slave */ | 270 | check_slave = 1; /* check-slave */ |
243 | break; | 271 | break; |
272 | case 'w': | ||
273 | warning = optarg; | ||
274 | break; | ||
275 | case 'c': | ||
276 | critical = optarg; | ||
277 | break; | ||
244 | case 'V': /* version */ | 278 | case 'V': /* version */ |
245 | print_revision (progname, revision); | 279 | print_revision (progname, revision); |
246 | exit (STATE_OK); | 280 | exit (STATE_OK); |
247 | case 'h': /* help */ | 281 | case 'h': /* help */ |
248 | print_help (); | 282 | print_help (); |
249 | exit (STATE_OK); | 283 | exit (STATE_OK); |
284 | case 'v': | ||
285 | verbose++; | ||
286 | break; | ||
250 | case '?': /* help */ | 287 | case '?': /* help */ |
251 | usage2 (_("Unknown argument"), optarg); | 288 | usage2 (_("Unknown argument"), optarg); |
252 | } | 289 | } |
@@ -254,6 +291,8 @@ process_arguments (int argc, char **argv) | |||
254 | 291 | ||
255 | c = optind; | 292 | c = optind; |
256 | 293 | ||
294 | set_thresholds(&my_threshold, warning, critical); | ||
295 | |||
257 | while ( argc > c ) { | 296 | while ( argc > c ) { |
258 | 297 | ||
259 | if (strlen(db_host) == 0) | 298 | if (strlen(db_host) == 0) |
@@ -326,7 +365,11 @@ print_help (void) | |||
326 | ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\ | 365 | ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\ |
327 | Your clear-text password will be visible as a process table entry\n\ | 366 | Your clear-text password will be visible as a process table entry\n\ |
328 | -S, --check-slave\n\ | 367 | -S, --check-slave\n\ |
329 | Check if the slave thread is running properly.\n")); | 368 | Check if the slave thread is running properly.\n\ |
369 | -w, --warning\n\ | ||
370 | Exit with WARNING status if slave server is more then INTEGER seconds behind master\n\ | ||
371 | -c, --critical\n\ | ||
372 | Exit with CRITICAL status if slave server is more then INTEGER seconds behind master\n")); | ||
330 | 373 | ||
331 | printf (_("\n\ | 374 | printf (_("\n\ |
332 | There are no required arguments. By default, the local database with\n\ | 375 | There are no required arguments. By default, the local database with\n\ |
diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t index e961106b..78413c6e 100644 --- a/plugins/t/check_mysql.t +++ b/plugins/t/check_mysql.t | |||
@@ -19,7 +19,7 @@ use vars qw($tests); | |||
19 | 19 | ||
20 | plan skip_all => "check_mysql not compiled" unless (-x "check_mysql"); | 20 | plan skip_all => "check_mysql not compiled" unless (-x "check_mysql"); |
21 | 21 | ||
22 | plan tests => 7; | 22 | plan tests => 10; |
23 | 23 | ||
24 | my $bad_login_output = '/Access denied for user /'; | 24 | my $bad_login_output = '/Access denied for user /'; |
25 | my $mysqlserver = getTestParameter( | 25 | my $mysqlserver = getTestParameter( |
@@ -58,10 +58,17 @@ SKIP: { | |||
58 | } | 58 | } |
59 | 59 | ||
60 | SKIP: { | 60 | SKIP: { |
61 | skip "No mysql server with slaves defined", 2 unless $with_slave; | 61 | skip "No mysql server with slaves defined", 5 unless $with_slave; |
62 | $result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login"); | 62 | $result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login"); |
63 | cmp_ok( $result->return_code, '==', 0, "Login okay"); | 63 | cmp_ok( $result->return_code, '==', 0, "Login okay"); |
64 | 64 | ||
65 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login"); | 65 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login"); |
66 | cmp_ok( $result->return_code, "==", 0, "Slaves okay" ); | 66 | cmp_ok( $result->return_code, "==", 0, "Slaves okay" ); |
67 | |||
68 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login -w 60"); | ||
69 | cmp_ok( $result->return_code, '==', 0, 'Slaves are not > 60 seconds behind'); | ||
70 | |||
71 | $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login -w 60:"); | ||
72 | cmp_ok( $result->return_code, '==', 1, 'Alert warning if < 60 seconds behind'); | ||
73 | like( $result->output, "/^SLOW_SLAVE WARNING:/", "Output okay"); | ||
67 | } | 74 | } |