summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/check_mysql.c92
1 files changed, 55 insertions, 37 deletions
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 6ba12feb..039a42e5 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -34,7 +34,7 @@ const char *progname = "check_mysql";
34const char *copyright = "1999-2024"; 34const char *copyright = "1999-2024";
35const char *email = "devel@monitoring-plugins.org"; 35const char *email = "devel@monitoring-plugins.org";
36 36
37#define SLAVERESULTSIZE 96 37#define REPLICA_RESULTSIZE 96
38 38
39#include "common.h" 39#include "common.h"
40#include "utils.h" 40#include "utils.h"
@@ -95,7 +95,7 @@ int main(int argc, char **argv) {
95 95
96 char *result = NULL; 96 char *result = NULL;
97 char *error = NULL; 97 char *error = NULL;
98 char slaveresult[SLAVERESULTSIZE] = {0}; 98 char replica_result[REPLICA_RESULTSIZE] = {0};
99 char *perf; 99 char *perf;
100 100
101 perf = strdup(""); 101 perf = strdup("");
@@ -200,20 +200,20 @@ int main(int argc, char **argv) {
200 if (mysql_query(&mysql, "show slave status") != 0) { 200 if (mysql_query(&mysql, "show slave status") != 0) {
201 error = strdup(mysql_error(&mysql)); 201 error = strdup(mysql_error(&mysql));
202 mysql_close(&mysql); 202 mysql_close(&mysql);
203 die(STATE_CRITICAL, _("slave query error: %s\n"), error); 203 die(STATE_CRITICAL, _("replica query error: %s\n"), error);
204 } 204 }
205 205
206 /* store the result */ 206 /* store the result */
207 if ((res = mysql_store_result(&mysql)) == NULL) { 207 if ((res = mysql_store_result(&mysql)) == NULL) {
208 error = strdup(mysql_error(&mysql)); 208 error = strdup(mysql_error(&mysql));
209 mysql_close(&mysql); 209 mysql_close(&mysql);
210 die(STATE_CRITICAL, _("slave store_result error: %s\n"), error); 210 die(STATE_CRITICAL, _("replica store_result error: %s\n"), error);
211 } 211 }
212 212
213 /* Check there is some data */ 213 /* Check there is some data */
214 if (mysql_num_rows(res) == 0) { 214 if (mysql_num_rows(res) == 0) {
215 mysql_close(&mysql); 215 mysql_close(&mysql);
216 die(STATE_WARNING, "%s\n", _("No slaves defined")); 216 die(STATE_WARNING, "%s\n", _("No replicas defined"));
217 } 217 }
218 218
219 /* fetch the first row */ 219 /* fetch the first row */
@@ -221,32 +221,32 @@ int main(int argc, char **argv) {
221 error = strdup(mysql_error(&mysql)); 221 error = strdup(mysql_error(&mysql));
222 mysql_free_result(res); 222 mysql_free_result(res);
223 mysql_close(&mysql); 223 mysql_close(&mysql);
224 die(STATE_CRITICAL, _("slave fetch row error: %s\n"), error); 224 die(STATE_CRITICAL, _("replica fetch row error: %s\n"), error);
225 } 225 }
226 226
227 if (mysql_field_count(&mysql) == 12) { 227 if (mysql_field_count(&mysql) == 12) {
228 /* mysql 3.23.x */ 228 /* mysql 3.23.x */
229 snprintf(slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]); 229 snprintf(replica_result, REPLICA_RESULTSIZE, _("Replica running: %s"), row[6]);
230 if (strcmp(row[6], "Yes") != 0) { 230 if (strcmp(row[6], "Yes") != 0) {
231 mysql_free_result(res); 231 mysql_free_result(res);
232 mysql_close(&mysql); 232 mysql_close(&mysql);
233 die(STATE_CRITICAL, "%s\n", slaveresult); 233 die(STATE_CRITICAL, "%s\n", replica_result);
234 } 234 }
235 235
236 } else { 236 } else {
237 /* mysql 4.x.x and mysql 5.x.x */ 237 /* mysql 4.x.x and mysql 5.x.x */
238 int slave_io_field = -1, slave_sql_field = -1, seconds_behind_field = -1, i, num_fields; 238 int replica_io_field = -1, replica_sql_field = -1, seconds_behind_field = -1, i, num_fields;
239 MYSQL_FIELD *fields; 239 MYSQL_FIELD *fields;
240 240
241 num_fields = mysql_num_fields(res); 241 num_fields = mysql_num_fields(res);
242 fields = mysql_fetch_fields(res); 242 fields = mysql_fetch_fields(res);
243 for (i = 0; i < num_fields; i++) { 243 for (i = 0; i < num_fields; i++) {
244 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) { 244 if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
245 slave_io_field = i; 245 replica_io_field = i;
246 continue; 246 continue;
247 } 247 }
248 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) { 248 if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
249 slave_sql_field = i; 249 replica_sql_field = i;
250 continue; 250 continue;
251 } 251 }
252 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) { 252 if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
@@ -255,19 +255,19 @@ int main(int argc, char **argv) {
255 } 255 }
256 } 256 }
257 257
258 /* Check if slave status is available */ 258 /* Check if replica status is available */
259 if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) { 259 if ((replica_io_field < 0) || (replica_sql_field < 0) || (num_fields == 0)) {
260 mysql_free_result(res); 260 mysql_free_result(res);
261 mysql_close(&mysql); 261 mysql_close(&mysql);
262 die(STATE_CRITICAL, "Slave status unavailable\n"); 262 die(STATE_CRITICAL, "Replica status unavailable\n");
263 } 263 }
264 264
265 /* Save slave status in slaveresult */ 265 /* Save replica status in replica_result */
266 snprintf(slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], 266 snprintf(replica_result, REPLICA_RESULTSIZE, "Replica IO: %s Replica SQL: %s Seconds Behind Master: %s", row[replica_io_field],
267 row[slave_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown"); 267 row[replica_sql_field], seconds_behind_field != -1 ? row[seconds_behind_field] : "Unknown");
268 268
269 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */ 269 /* Raise critical error if SQL THREAD or IO THREAD are stopped, but only if there are no mysqldump threads running */
270 if (strcmp(row[slave_io_field], "Yes") != 0 || strcmp(row[slave_sql_field], "Yes") != 0) { 270 if (strcmp(row[replica_io_field], "Yes") != 0 || strcmp(row[replica_sql_field], "Yes") != 0) {
271 MYSQL_RES *res_mysqldump; 271 MYSQL_RES *res_mysqldump;
272 MYSQL_ROW row_mysqldump; 272 MYSQL_ROW row_mysqldump;
273 unsigned int mysqldump_threads = 0; 273 unsigned int mysqldump_threads = 0;
@@ -286,9 +286,9 @@ int main(int argc, char **argv) {
286 mysql_close(&mysql); 286 mysql_close(&mysql);
287 } 287 }
288 if (mysqldump_threads == 0) { 288 if (mysqldump_threads == 0) {
289 die(STATE_CRITICAL, "%s\n", slaveresult); 289 die(STATE_CRITICAL, "%s\n", replica_result);
290 } else { 290 } else {
291 strncat(slaveresult, " Mysqldump: in progress", SLAVERESULTSIZE - 1); 291 strncat(replica_result, " Mysqldump: in progress", REPLICA_RESULTSIZE - 1);
292 } 292 }
293 } 293 }
294 294
@@ -312,10 +312,10 @@ int main(int argc, char **argv) {
312 false, 0)); 312 false, 0));
313 313
314 if (status == STATE_WARNING) { 314 if (status == STATE_WARNING) {
315 printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf); 315 printf("SLOW_REPLICA %s: %s|%s\n", _("WARNING"), replica_result, perf);
316 exit(STATE_WARNING); 316 exit(STATE_WARNING);
317 } else if (status == STATE_CRITICAL) { 317 } else if (status == STATE_CRITICAL) {
318 printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf); 318 printf("SLOW_REPLICA %s: %s|%s\n", _("CRITICAL"), replica_result, perf);
319 exit(STATE_CRITICAL); 319 exit(STATE_CRITICAL);
320 } 320 }
321 } 321 }
@@ -330,7 +330,7 @@ int main(int argc, char **argv) {
330 330
331 /* print out the result of stats */ 331 /* print out the result of stats */
332 if (check_replica) { 332 if (check_replica) {
333 printf("%s %s|%s\n", result, slaveresult, perf); 333 printf("%s %s|%s\n", result, replica_result, perf);
334 } else { 334 } else {
335 printf("%s|%s\n", result, perf); 335 printf("%s|%s\n", result, perf);
336 } 336 }
@@ -338,6 +338,8 @@ int main(int argc, char **argv) {
338 return STATE_OK; 338 return STATE_OK;
339} 339}
340 340
341#define CHECK_REPLICA_OPT CHAR_MAX + 1
342
341/* process command-line arguments */ 343/* process command-line arguments */
342int process_arguments(int argc, char **argv) { 344int process_arguments(int argc, char **argv) {
343 int c; 345 int c;
@@ -345,17 +347,29 @@ int process_arguments(int argc, char **argv) {
345 char *critical = NULL; 347 char *critical = NULL;
346 348
347 int option = 0; 349 int option = 0;
348 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"socket", required_argument, 0, 's'}, 350 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
349 {"database", required_argument, 0, 'd'}, {"username", required_argument, 0, 'u'}, 351 {"socket", required_argument, 0, 's'},
350 {"password", required_argument, 0, 'p'}, {"file", required_argument, 0, 'f'}, 352 {"database", required_argument, 0, 'd'},
351 {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'P'}, 353 {"username", required_argument, 0, 'u'},
352 {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, 354 {"password", required_argument, 0, 'p'},
353 {"check-slave", no_argument, 0, 'S'}, {"ignore-auth", no_argument, 0, 'n'}, 355 {"file", required_argument, 0, 'f'},
354 {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, 356 {"group", required_argument, 0, 'g'},
355 {"help", no_argument, 0, 'h'}, {"ssl", no_argument, 0, 'l'}, 357 {"port", required_argument, 0, 'P'},
356 {"ca-cert", optional_argument, 0, 'C'}, {"key", required_argument, 0, 'k'}, 358 {"critical", required_argument, 0, 'c'},
357 {"cert", required_argument, 0, 'a'}, {"ca-dir", required_argument, 0, 'D'}, 359 {"warning", required_argument, 0, 'w'},
358 {"ciphers", required_argument, 0, 'L'}, {0, 0, 0, 0}}; 360 {"check-slave", no_argument, 0, 'S'},
361 {"check-replica", no_argument, 0, CHECK_REPLICA_OPT},
362 {"ignore-auth", no_argument, 0, 'n'},
363 {"verbose", no_argument, 0, 'v'},
364 {"version", no_argument, 0, 'V'},
365 {"help", no_argument, 0, 'h'},
366 {"ssl", no_argument, 0, 'l'},
367 {"ca-cert", optional_argument, 0, 'C'},
368 {"key", required_argument, 0, 'k'},
369 {"cert", required_argument, 0, 'a'},
370 {"ca-dir", required_argument, 0, 'D'},
371 {"ciphers", required_argument, 0, 'L'},
372 {0, 0, 0, 0}};
359 373
360 if (argc < 1) { 374 if (argc < 1) {
361 return ERROR; 375 return ERROR;
@@ -424,6 +438,7 @@ int process_arguments(int argc, char **argv) {
424 db_port = atoi(optarg); 438 db_port = atoi(optarg);
425 break; 439 break;
426 case 'S': 440 case 'S':
441 case CHECK_REPLICA_OPT:
427 check_replica = true; /* check-slave */ 442 check_replica = true; /* check-slave */
428 break; 443 break;
429 case 'n': 444 case 'n':
@@ -532,12 +547,15 @@ void print_help(void) {
532 printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!")); 547 printf(" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
533 printf(" %s\n", _("Your clear-text password could be visible as a process table entry")); 548 printf(" %s\n", _("Your clear-text password could be visible as a process table entry"));
534 printf(" %s\n", "-S, --check-slave"); 549 printf(" %s\n", "-S, --check-slave");
535 printf(" %s\n", _("Check if the slave thread is running properly.")); 550 printf(" %s\n",
551 _("Check if the slave thread is running properly. This option is deprecated in favour of check-replica, which does the same"));
552 printf(" %s\n", "--check-replica");
553 printf(" %s\n", _("Check if the replica thread is running properly."));
536 printf(" %s\n", "-w, --warning"); 554 printf(" %s\n", "-w, --warning");
537 printf(" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds")); 555 printf(" %s\n", _("Exit with WARNING status if replica server is more than INTEGER seconds"));
538 printf(" %s\n", _("behind master")); 556 printf(" %s\n", _("behind master"));
539 printf(" %s\n", "-c, --critical"); 557 printf(" %s\n", "-c, --critical");
540 printf(" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds")); 558 printf(" %s\n", _("Exit with CRITICAL status if replica server is more then INTEGER seconds"));
541 printf(" %s\n", _("behind master")); 559 printf(" %s\n", _("behind master"));
542 printf(" %s\n", "-l, --ssl"); 560 printf(" %s\n", "-l, --ssl");
543 printf(" %s\n", _("Use ssl encryption")); 561 printf(" %s\n", _("Use ssl encryption"));