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.c165
1 files changed, 155 insertions, 10 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 4fbdc2b..db670e2 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,10 +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;
58char *opt_file = NULL;
59char *opt_group = NULL;
52unsigned int db_port = MYSQL_PORT; 60unsigned int db_port = MYSQL_PORT;
53int check_slave = 0, warn_sec = 0, crit_sec = 0; 61int check_slave = 0, warn_sec = 0, crit_sec = 0;
54int verbose = 0; 62int verbose = 0;
55 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
56thresholds *my_threshold = NULL; 90thresholds *my_threshold = NULL;
57 91
58int process_arguments (int, char **); 92int process_arguments (int, char **);
@@ -73,6 +107,9 @@ main (int argc, char **argv)
73 char *result = NULL; 107 char *result = NULL;
74 char *error = NULL; 108 char *error = NULL;
75 char slaveresult[SLAVERESULTSIZE]; 109 char slaveresult[SLAVERESULTSIZE];
110 char* perf;
111
112 perf = strdup ("");
76 113
77 setlocale (LC_ALL, ""); 114 setlocale (LC_ALL, "");
78 bindtextdomain (PACKAGE, LOCALEDIR); 115 bindtextdomain (PACKAGE, LOCALEDIR);
@@ -86,9 +123,17 @@ main (int argc, char **argv)
86 123
87 /* initialize mysql */ 124 /* initialize mysql */
88 mysql_init (&mysql); 125 mysql_init (&mysql);
126
127 if (opt_file != NULL)
128 mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
89 129
90 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client"); 130 if (opt_group != NULL)
131 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
132 else
133 mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
91 134
135 if (ssl)
136 mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers);
92 /* establish a connection to the server and error checking */ 137 /* establish a connection to the server and error checking */
93 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)) {
94 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) 139 if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
@@ -118,6 +163,37 @@ main (int argc, char **argv)
118 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql)); 163 die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
119 } 164 }
120 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
121 if(check_slave) { 197 if(check_slave) {
122 /* check the slave status */ 198 /* check the slave status */
123 if (mysql_query (&mysql, "show slave status") != 0) { 199 if (mysql_query (&mysql, "show slave status") != 0) {
@@ -157,7 +233,7 @@ main (int argc, char **argv)
157 } 233 }
158 234
159 } else { 235 } else {
160 /* mysql 4.x.x */ 236 /* mysql 4.x.x and mysql 5.x.x */
161 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;
162 MYSQL_FIELD* fields; 238 MYSQL_FIELD* fields;
163 239
@@ -178,13 +254,17 @@ main (int argc, char **argv)
178 } 254 }
179 } 255 }
180 256
257 /* Check if slave status is available */
181 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)) {
182 mysql_free_result (res); 259 mysql_free_result (res);
183 mysql_close (&mysql); 260 mysql_close (&mysql);
184 die (STATE_CRITICAL, "Slave status unavailable\n"); 261 die (STATE_CRITICAL, "Slave status unavailable\n");
185 } 262 }
186 263
264 /* Save slave status in slaveresult */
187 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 */
188 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) {
189 mysql_free_result (res); 269 mysql_free_result (res);
190 mysql_close (&mysql); 270 mysql_close (&mysql);
@@ -199,17 +279,24 @@ main (int argc, char **argv)
199 } 279 }
200 } 280 }
201 281
282 /* Check Seconds Behind against threshold */
202 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)) {
203 double value = atof(row[seconds_behind_field]); 284 double value = atof(row[seconds_behind_field]);
204 int status; 285 int status;
205 286
206 status = get_status(value, my_threshold); 287 status = get_status(value, my_threshold);
207 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
208 if (status == STATE_WARNING) { 295 if (status == STATE_WARNING) {
209 printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult); 296 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf);
210 exit(STATE_WARNING); 297 exit(STATE_WARNING);
211 } else if (status == STATE_CRITICAL) { 298 } else if (status == STATE_CRITICAL) {
212 printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult); 299 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf);
213 exit(STATE_CRITICAL); 300 exit(STATE_CRITICAL);
214 } 301 }
215 } 302 }
@@ -224,9 +311,9 @@ main (int argc, char **argv)
224 311
225 /* print out the result of stats */ 312 /* print out the result of stats */
226 if (check_slave) { 313 if (check_slave) {
227 printf ("%s %s\n", result, slaveresult); 314 printf ("%s %s|%s\n", result, slaveresult, perf);
228 } else { 315 } else {
229 printf ("%s\n", result); 316 printf ("%s|%s\n", result, perf);
230 } 317 }
231 318
232 return STATE_OK; 319 return STATE_OK;
@@ -248,6 +335,8 @@ process_arguments (int argc, char **argv)
248 {"database", required_argument, 0, 'd'}, 335 {"database", required_argument, 0, 'd'},
249 {"username", required_argument, 0, 'u'}, 336 {"username", required_argument, 0, 'u'},
250 {"password", required_argument, 0, 'p'}, 337 {"password", required_argument, 0, 'p'},
338 {"file", required_argument, 0, 'f'},
339 {"group", required_argument, 0, 'g'},
251 {"port", required_argument, 0, 'P'}, 340 {"port", required_argument, 0, 'P'},
252 {"critical", required_argument, 0, 'c'}, 341 {"critical", required_argument, 0, 'c'},
253 {"warning", required_argument, 0, 'w'}, 342 {"warning", required_argument, 0, 'w'},
@@ -255,6 +344,12 @@ process_arguments (int argc, char **argv)
255 {"verbose", no_argument, 0, 'v'}, 344 {"verbose", no_argument, 0, 'v'},
256 {"version", no_argument, 0, 'V'}, 345 {"version", no_argument, 0, 'V'},
257 {"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'},
258 {0, 0, 0, 0} 353 {0, 0, 0, 0}
259 }; 354 };
260 355
@@ -262,7 +357,7 @@ process_arguments (int argc, char **argv)
262 return ERROR; 357 return ERROR;
263 358
264 while (1) { 359 while (1) {
265 c = getopt_long (argc, argv, "hvVSP:p:u:d: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);
266 361
267 if (c == -1 || c == EOF) 362 if (c == -1 || c == EOF)
268 break; 363 break;
@@ -282,6 +377,24 @@ process_arguments (int argc, char **argv)
282 case 'd': /* database */ 377 case 'd': /* database */
283 db = optarg; 378 db = optarg;
284 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;
285 case 'u': /* username */ 398 case 'u': /* username */
286 db_user = optarg; 399 db_user = optarg;
287 break; 400 break;
@@ -294,6 +407,12 @@ process_arguments (int argc, char **argv)
294 optarg++; 407 optarg++;
295 } 408 }
296 break; 409 break;
410 case 'f': /* client options file */
411 opt_file = optarg;
412 break;
413 case 'g': /* client options group */
414 opt_group = optarg;
415 break;
297 case 'P': /* critical time threshold */ 416 case 'P': /* critical time threshold */
298 db_port = atoi (optarg); 417 db_port = atoi (optarg);
299 break; 418 break;
@@ -302,9 +421,11 @@ process_arguments (int argc, char **argv)
302 break; 421 break;
303 case 'w': 422 case 'w':
304 warning = optarg; 423 warning = optarg;
424 warning_time = strtod (warning, NULL);
305 break; 425 break;
306 case 'c': 426 case 'c':
307 critical = optarg; 427 critical = optarg;
428 critical_time = strtod (critical, NULL);
308 break; 429 break;
309 case 'V': /* version */ 430 case 'V': /* version */
310 print_revision (progname, NP_VERSION); 431 print_revision (progname, NP_VERSION);
@@ -355,6 +476,12 @@ validate_arguments (void)
355 if (db_user == NULL) 476 if (db_user == NULL)
356 db_user = strdup(""); 477 db_user = strdup("");
357 478
479 if (opt_file == NULL)
480 opt_file = strdup("");
481
482 if (opt_group == NULL)
483 opt_group = strdup("");
484
358 if (db_host == NULL) 485 if (db_host == NULL)
359 db_host = strdup(""); 486 db_host = strdup("");
360 487
@@ -390,6 +517,10 @@ print_help (void)
390 517
391 printf (" %s\n", "-d, --database=STRING"); 518 printf (" %s\n", "-d, --database=STRING");
392 printf (" %s\n", _("Check database with indicated name")); 519 printf (" %s\n", _("Check database with indicated name"));
520 printf (" %s\n", "-f, --file=STRING");
521 printf (" %s\n", _("Read from the specified client options file"));
522 printf (" %s\n", "-g, --group=STRING");
523 printf (" %s\n", _("Use a client options group"));
393 printf (" %s\n", "-u, --username=STRING"); 524 printf (" %s\n", "-u, --username=STRING");
394 printf (" %s\n", _("Connect using the indicated username")); 525 printf (" %s\n", _("Connect using the indicated username"));
395 printf (" %s\n", "-p, --password=STRING"); 526 printf (" %s\n", "-p, --password=STRING");
@@ -404,6 +535,19 @@ print_help (void)
404 printf (" %s\n", "-c, --critical"); 535 printf (" %s\n", "-c, --critical");
405 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); 536 printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
406 printf (" %s\n", _("behind master")); 537 printf (" %s\n", _("behind master"));
538 printf (" %s\n", "-l, --ssl");
539 printf (" %s\n", _("Use ssl encryptation"));
540 printf (" %s\n", "-C, --ca-cert=STRING");
541 printf (" %s\n", _("Path to CA signing the cert"));
542 printf (" %s\n", "-a, --cert=STRING");
543 printf (" %s\n", _("Path to SSL certificate"));
544 printf (" %s\n", "-k, --key=STRING");
545 printf (" %s\n", _("Path to private SSL key"));
546 printf (" %s\n", "-D, --ca-dir=STRING");
547 printf (" %s\n", _("Path to CA directory"));
548 printf (" %s\n", "-L, --ciphers=STRING");
549 printf (" %s\n", _("List of valid SSL ciphers"));
550
407 551
408 printf ("\n"); 552 printf ("\n");
409 printf (" %s\n", _("There are no required arguments. By default, the local database is checked")); 553 printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
@@ -424,5 +568,6 @@ print_usage (void)
424{ 568{
425 printf ("%s\n", _("Usage:")); 569 printf ("%s\n", _("Usage:"));
426 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname); 570 printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
427 printf (" [-u user] [-p password] [-S]\n"); 571 printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
572 printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");
428} 573}