summaryrefslogtreecommitdiffstats
path: root/plugins/check_mysql.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_mysql.c')
-rw-r--r--plugins/check_mysql.c138
1 files changed, 128 insertions, 10 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index cacc6c2..521c902 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -5,7 +5,7 @@
5* License: GPL 5* License: GPL
6* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at) 6* Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
7* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) 7* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
8* Copyright (c) 1999-2009 Nagios Plugins Development Team 8* Copyright (c) 1999-2011 Nagios Plugins Development Team
9* 9*
10* Description: 10* Description:
11* 11*
@@ -31,7 +31,7 @@
31*****************************************************************************/ 31*****************************************************************************/
32 32
33const char *progname = "check_mysql"; 33const char *progname = "check_mysql";
34const char *copyright = "1999-2007"; 34const char *copyright = "1999-2011";
35const char *email = "nagiosplug-devel@lists.sourceforge.net"; 35const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 36
37#define SLAVERESULTSIZE 70 37#define SLAVERESULTSIZE 70
@@ -49,12 +49,44 @@ char *db_host = NULL;
49char *db_socket = NULL; 49char *db_socket = NULL;
50char *db_pass = NULL; 50char *db_pass = NULL;
51char *db = NULL; 51char *db = NULL;
52char *ca_cert = NULL;
53char *ca_dir = NULL;
54char *cert = NULL;
55char *key = NULL;
56char *ciphers = NULL;
57bool ssl = false;
52char *opt_file = NULL; 58char *opt_file = NULL;
53char *opt_group = NULL; 59char *opt_group = NULL;
54unsigned int db_port = MYSQL_PORT; 60unsigned int db_port = MYSQL_PORT;
55int check_slave = 0, warn_sec = 0, crit_sec = 0; 61int check_slave = 0, warn_sec = 0, crit_sec = 0;
56int verbose = 0; 62int verbose = 0;
57 63
64static double warning_time = 0;
65static double critical_time = 0;
66
67#define LENGTH_METRIC_UNIT 6
68static const char *metric_unit[LENGTH_METRIC_UNIT] = {
69 "Open_files",
70 "Open_tables",
71 "Qcache_free_memory",
72 "Qcache_queries_in_cache",
73 "Threads_connected",
74 "Threads_running"
75};
76
77#define LENGTH_METRIC_COUNTER 9
78static const char *metric_counter[LENGTH_METRIC_COUNTER] = {
79 "Connections",
80 "Qcache_hits",
81 "Qcache_inserts",
82 "Qcache_lowmem_prunes",
83 "Qcache_not_cached",
84 "Queries",
85 "Questions",
86 "Table_locks_waited",
87 "Uptime"
88};
89
58thresholds *my_threshold = NULL; 90thresholds *my_threshold = NULL;
59 91
60int process_arguments (int, char **); 92int process_arguments (int, char **);
@@ -75,6 +107,9 @@ main (int argc, char **argv)
75 char *result = NULL; 107 char *result = NULL;
76 char *error = NULL; 108 char *error = NULL;
77 char slaveresult[SLAVERESULTSIZE]; 109 char slaveresult[SLAVERESULTSIZE];
110 char* perf;
111
112 perf = strdup ("");
78 113
79 setlocale (LC_ALL, ""); 114 setlocale (LC_ALL, "");
80 bindtextdomain (PACKAGE, LOCALEDIR); 115 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -97,6 +132,8 @@ main (int argc, char **argv)
97 else 132 else
98 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); 133 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
99 134
135 if (ssl)
136 mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers);
100 /* establish a connection to the server and error checking */ 137 /* establish a connection to the server and error checking */
101 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) { 138 if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
102 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) 139 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
@@ -126,6 +163,37 @@ main (int argc, char **argv)
126 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 163 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
127 } 164 }
128 165
166 /* try to fetch some perf data */
167 if (mysql_query (&mysql, "show global status") == 0) {
168 if ( (res = mysql_store_result (&mysql)) == NULL) {
169 error = strdup(mysql_error(&mysql));
170 mysql_close (&mysql);
171 die (STATE_CRITICAL, _("status store_result error: %s\n"), error);
172 }
173
174 while ( (row = mysql_fetch_row (res)) != NULL) {
175 int i;
176
177 for(i = 0; i < LENGTH_METRIC_UNIT; i++) {
178 if (strcmp(row[0], metric_unit[i]) == 0) {
179 xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i],
180 atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
181 continue;
182 }
183 }
184 for(i = 0; i < LENGTH_METRIC_COUNTER; i++) {
185 if (strcmp(row[0], metric_counter[i]) == 0) {
186 xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i],
187 atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
188 continue;
189 }
190 }
191 }
192 /* remove trailing space */
193 if (strlen(perf) > 0)
194 perf[strlen(perf) - 1] = '\0';
195 }
196
129 if(check_slave) { 197 if(check_slave) {
130 /* check the slave status */ 198 /* check the slave status */
131 if (mysql_query (&mysql, "show slave status") != 0) { 199 if (mysql_query (&mysql, "show slave status") != 0) {
@@ -165,7 +233,7 @@ main (int argc, char **argv)
165 } 233 }
166 234
167 } else { 235 } else {
168 /* mysql 4.x.x */ 236 /* mysql 4.x.x and mysql 5.x.x */
169 int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; 237 int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
170 MYSQL_FIELD* fields; 238 MYSQL_FIELD* fields;
171 239
@@ -186,13 +254,17 @@ main (int argc, char **argv)
186 } 254 }
187 } 255 }
188 256
257 /* Check if slave status is available */
189 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { 258 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
190 mysql_free_result (res); 259 mysql_free_result (res);
191 mysql_close (&mysql); 260 mysql_close (&mysql);
192 die (STATE_CRITICAL, "Slave status unavailable\n"); 261 die (STATE_CRITICAL, "Slave status unavailable\n");
193 } 262 }
194 263
264 /* Save slave status in slaveresult */
195 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown"); 265 snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown");
266
267 /* Raise critical error if SQL THREAD or IO THREAD are stopped */
196 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) { 268 if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
197 mysql_free_result (res); 269 mysql_free_result (res);
198 mysql_close (&mysql); 270 mysql_close (&mysql);
@@ -207,17 +279,24 @@ main (int argc, char **argv)
207 } 279 }
208 } 280 }
209 281
282 /* Check Seconds Behind against threshold */
210 if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) { 283 if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
211 double value = atof(row[seconds_behind_field]); 284 double value = atof(row[seconds_behind_field]);
212 int status; 285 int status;
213 286
214 status = get_status(value, my_threshold); 287 status = get_status(value, my_threshold);
215 288
289 xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s",
290 TRUE, (double) warning_time,
291 TRUE, (double) critical_time,
292 FALSE, 0,
293 FALSE, 0));
294
216 if (status == STATE_WARNING) { 295 if (status == STATE_WARNING) {
217 printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult); 296 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf);
218 exit(STATE_WARNING); 297 exit(STATE_WARNING);
219 } else if (status == STATE_CRITICAL) { 298 } else if (status == STATE_CRITICAL) {
220 printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult); 299 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf);
221 exit(STATE_CRITICAL); 300 exit(STATE_CRITICAL);
222 } 301 }
223 } 302 }
@@ -232,9 +311,9 @@ main (int argc, char **argv)
232 311
233 /* print out the result of stats */ 312 /* print out the result of stats */
234 if (check_slave) { 313 if (check_slave) {
235 printf ("%s %s\n", result, slaveresult); 314 printf ("%s %s|%s\n", result, slaveresult, perf);
236 } else { 315 } else {
237 printf ("%s\n", result); 316 printf ("%s|%s\n", result, perf);
238 } 317 }
239 318
240 return STATE_OK; 319 return STATE_OK;
@@ -265,6 +344,12 @@ process_arguments (int argc, char **argv)
265 {"verbose", no_argument, 0, 'v'}, 344 {"verbose", no_argument, 0, 'v'},
266 {"version", no_argument, 0, 'V'}, 345 {"version", no_argument, 0, 'V'},
267 {"help", no_argument, 0, 'h'}, 346 {"help", no_argument, 0, 'h'},
347 {"ssl", no_argument, 0, 'l'},
348 {"ca-cert", optional_argument, 0, 'C'},
349 {"key", required_argument,0,'k'},
350 {"cert", required_argument,0,'a'},
351 {"ca-dir", required_argument, 0, 'D'},
352 {"ciphers", required_argument, 0, 'L'},
268 {0, 0, 0, 0} 353 {0, 0, 0, 0}
269 }; 354 };
270 355
@@ -272,7 +357,7 @@ process_arguments (int argc, char **argv)
272 return ERROR; 357 return ERROR;
273 358
274 while (1) { 359 while (1) {
275 c = getopt_long (argc, argv, "hvVSP:p:u:d:f:g:H:s:c:w:", longopts, &option); 360 c = getopt_long (argc, argv, "hlvVSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
276 361
277 if (c == -1 || c == EOF) 362 if (c == -1 || c == EOF)
278 break; 363 break;
@@ -292,6 +377,24 @@ process_arguments (int argc, char **argv)
292 case 'd': /* database */ 377 case 'd': /* database */
293 db = optarg; 378 db = optarg;
294 break; 379 break;
380 case 'l':
381 ssl = true;
382 break;
383 case 'C':
384 ca_cert = optarg;
385 break;
386 case 'a':
387 cert = optarg;
388 break;
389 case 'k':
390 key = optarg;
391 break;
392 case 'D':
393 ca_dir = optarg;
394 break;
395 case 'L':
396 ciphers = optarg;
397 break;
295 case 'u': /* username */ 398 case 'u': /* username */
296 db_user = optarg; 399 db_user = optarg;
297 break; 400 break;
@@ -318,9 +421,11 @@ process_arguments (int argc, char **argv)
318 break; 421 break;
319 case 'w': 422 case 'w':
320 warning = optarg; 423 warning = optarg;
424 warning_time = strtod (warning, NULL);
321 break; 425 break;
322 case 'c': 426 case 'c':
323 critical = optarg; 427 critical = optarg;
428 critical_time = strtod (critical, NULL);
324 break; 429 break;
325 case 'V': /* version */ 430 case 'V': /* version */
326 print_revision (progname, NP_VERSION); 431 print_revision (progname, NP_VERSION);
@@ -434,6 +539,19 @@ print_help (void)
434 printf (" %s\n", "-c, --critical"); 539 printf (" %s\n", "-c, --critical");
435 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); 540 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
436 printf (" %s\n", _("behind master")); 541 printf (" %s\n", _("behind master"));
542 printf (" %s\n", "-l, --ssl");
543 printf (" %s\n", _("Use ssl encryptation"));
544 printf (" %s\n", "-C, --ca-cert=STRING");
545 printf (" %s\n", _("Path to CA signing the cert"));
546 printf (" %s\n", "-a, --cert=STRING");
547 printf (" %s\n", _("Path to SSL certificate"));
548 printf (" %s\n", "-k, --key=STRING");
549 printf (" %s\n", _("Path to private SSL key"));
550 printf (" %s\n", "-D, --ca-dir=STRING");
551 printf (" %s\n", _("Path to CA directory"));
552 printf (" %s\n", "-L, --ciphers=STRING");
553 printf (" %s\n", _("List of valid SSL ciphers"));
554
437 555
438 printf ("\n"); 556 printf ("\n");
439 printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); 557 printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
@@ -454,6 +572,6 @@ print_usage (void)
454{ 572{
455 printf ("%s\n", _("Usage:")); 573 printf ("%s\n", _("Usage:"));
456 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname); 574 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
457 printf (" [-u user] [-p password] [-S] [-f optfile]\n"); 575 printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
458 printf (" [-g group]\n"); 576 printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");
459} 577}