check_mysql.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mysql plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
  7. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  8. * Copyright (c) 1999-2014 Nagios Plugins Development Team
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_mysql plugin
  13. *
  14. * This program tests connections to a mysql server
  15. *
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. *
  31. *****************************************************************************/
  32. const char *progname = "check_mysql";
  33. const char *copyright = "1999-2014";
  34. const char *email = "devel@nagios-plugins.org";
  35. #define SLAVERESULTSIZE 70
  36. #include "common.h"
  37. #include "utils.h"
  38. #include "utils_base.h"
  39. #include "netutils.h"
  40. #include <mysql.h>
  41. #include <mysqld_error.h>
  42. #include <errmsg.h>
  43. char *db_user = NULL;
  44. char *db_host = NULL;
  45. char *db_socket = NULL;
  46. char *db_pass = NULL;
  47. char *db = NULL;
  48. char *ca_cert = NULL;
  49. char *ca_dir = NULL;
  50. char *cert = NULL;
  51. char *key = NULL;
  52. char *ciphers = NULL;
  53. bool ssl = false;
  54. char *opt_file = NULL;
  55. char *opt_group = NULL;
  56. unsigned int db_port = MYSQL_PORT;
  57. int check_slave = 0, warn_sec = 0, crit_sec = 0;
  58. int ignore_auth = 0;
  59. int verbose = 0;
  60. static double warning_time = 0;
  61. static double critical_time = 0;
  62. #define LENGTH_METRIC_UNIT 6
  63. static const char *metric_unit[LENGTH_METRIC_UNIT] = {
  64. "Open_files",
  65. "Open_tables",
  66. "Qcache_free_memory",
  67. "Qcache_queries_in_cache",
  68. "Threads_connected",
  69. "Threads_running"
  70. };
  71. #define LENGTH_METRIC_COUNTER 9
  72. static const char *metric_counter[LENGTH_METRIC_COUNTER] = {
  73. "Connections",
  74. "Qcache_hits",
  75. "Qcache_inserts",
  76. "Qcache_lowmem_prunes",
  77. "Qcache_not_cached",
  78. "Queries",
  79. "Questions",
  80. "Table_locks_waited",
  81. "Uptime"
  82. };
  83. thresholds *my_threshold = NULL;
  84. int process_arguments (int, char **);
  85. int validate_arguments (void);
  86. void print_help (void);
  87. void print_usage (void);
  88. int
  89. main (int argc, char **argv)
  90. {
  91. MYSQL mysql;
  92. MYSQL_RES *res;
  93. MYSQL_ROW row;
  94. /* should be status */
  95. char *result = NULL;
  96. char *error = NULL;
  97. char slaveresult[SLAVERESULTSIZE];
  98. char* perf;
  99. perf = strdup ("");
  100. setlocale (LC_ALL, "");
  101. bindtextdomain (PACKAGE, LOCALEDIR);
  102. textdomain (PACKAGE);
  103. /* Parse extra opts if any */
  104. argv=np_extra_opts (&argc, argv, progname);
  105. if (process_arguments (argc, argv) == ERROR)
  106. usage4 (_("Could not parse arguments"));
  107. /* initialize mysql */
  108. mysql_init (&mysql);
  109. if (opt_file != NULL)
  110. mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
  111. if (opt_group != NULL)
  112. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
  113. else
  114. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
  115. if (ssl)
  116. mysql_ssl_set(&mysql,key,cert,ca_cert,ca_dir,ciphers);
  117. /* establish a connection to the server and error checking */
  118. if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
  119. if (ignore_auth && mysql_errno (&mysql) == ER_ACCESS_DENIED_ERROR)
  120. {
  121. printf("MySQL OK - Version: %s (protocol %d)\n",
  122. mysql_get_server_info(&mysql),
  123. mysql_get_proto_info(&mysql)
  124. );
  125. mysql_close (&mysql);
  126. return STATE_OK;
  127. }
  128. else if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
  129. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  130. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  131. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  132. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  133. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  134. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  135. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  136. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  137. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  138. else
  139. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  140. }
  141. /* get the server stats */
  142. result = strdup (mysql_stat (&mysql));
  143. /* error checking once more */
  144. if (mysql_error (&mysql)) {
  145. if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
  146. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  147. else if (mysql_errno (&mysql) == CR_SERVER_LOST)
  148. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  149. else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
  150. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  151. }
  152. /* try to fetch some perf data */
  153. if (mysql_query (&mysql, "show global status") == 0) {
  154. if ( (res = mysql_store_result (&mysql)) == NULL) {
  155. error = strdup(mysql_error(&mysql));
  156. mysql_close (&mysql);
  157. die (STATE_CRITICAL, _("status store_result error: %s\n"), error);
  158. }
  159. while ( (row = mysql_fetch_row (res)) != NULL) {
  160. int i;
  161. for(i = 0; i < LENGTH_METRIC_UNIT; i++) {
  162. if (strcmp(row[0], metric_unit[i]) == 0) {
  163. xasprintf(&perf, "%s%s ", perf, perfdata(metric_unit[i],
  164. atol(row[1]), "", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
  165. continue;
  166. }
  167. }
  168. for(i = 0; i < LENGTH_METRIC_COUNTER; i++) {
  169. if (strcmp(row[0], metric_counter[i]) == 0) {
  170. xasprintf(&perf, "%s%s ", perf, perfdata(metric_counter[i],
  171. atol(row[1]), "c", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0));
  172. continue;
  173. }
  174. }
  175. }
  176. /* remove trailing space */
  177. if (strlen(perf) > 0)
  178. perf[strlen(perf) - 1] = '\0';
  179. }
  180. if(check_slave) {
  181. /* check the slave status */
  182. if (mysql_query (&mysql, "show slave status") != 0) {
  183. error = strdup(mysql_error(&mysql));
  184. mysql_close (&mysql);
  185. die (STATE_CRITICAL, _("slave query error: %s\n"), error);
  186. }
  187. /* store the result */
  188. if ( (res = mysql_store_result (&mysql)) == NULL) {
  189. error = strdup(mysql_error(&mysql));
  190. mysql_close (&mysql);
  191. die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
  192. }
  193. /* Check there is some data */
  194. if (mysql_num_rows(res) == 0) {
  195. mysql_close(&mysql);
  196. die (STATE_WARNING, "%s\n", _("No slaves defined"));
  197. }
  198. /* fetch the first row */
  199. if ( (row = mysql_fetch_row (res)) == NULL) {
  200. error = strdup(mysql_error(&mysql));
  201. mysql_free_result (res);
  202. mysql_close (&mysql);
  203. die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
  204. }
  205. if (mysql_field_count (&mysql) == 12) {
  206. /* mysql 3.23.x */
  207. snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
  208. if (strcmp (row[6], "Yes") != 0) {
  209. mysql_free_result (res);
  210. mysql_close (&mysql);
  211. die (STATE_CRITICAL, "%s\n", slaveresult);
  212. }
  213. } else {
  214. /* mysql 4.x.x and mysql 5.x.x */
  215. int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
  216. MYSQL_FIELD* fields;
  217. num_fields = mysql_num_fields(res);
  218. fields = mysql_fetch_fields(res);
  219. for(i = 0; i < num_fields; i++) {
  220. if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
  221. slave_io_field = i;
  222. continue;
  223. }
  224. if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
  225. slave_sql_field = i;
  226. continue;
  227. }
  228. if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
  229. seconds_behind_field = i;
  230. continue;
  231. }
  232. }
  233. /* Check if slave status is available */
  234. if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
  235. mysql_free_result (res);
  236. mysql_close (&mysql);
  237. die (STATE_CRITICAL, "Slave status unavailable\n");
  238. }
  239. /* Save slave status in slaveresult */
  240. 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");
  241. /* Raise critical error if SQL THREAD or IO THREAD are stopped */
  242. if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
  243. mysql_free_result (res);
  244. mysql_close (&mysql);
  245. die (STATE_CRITICAL, "%s\n", slaveresult);
  246. }
  247. if (verbose >=3) {
  248. if (seconds_behind_field == -1) {
  249. printf("seconds_behind_field not found\n");
  250. } else {
  251. printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
  252. }
  253. }
  254. /* Check Seconds Behind against threshold */
  255. if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
  256. double value = atof(row[seconds_behind_field]);
  257. int status;
  258. status = get_status(value, my_threshold);
  259. xasprintf (&perf, "%s %s", perf, fperfdata ("seconds behind master", value, "s",
  260. TRUE, (double) warning_time,
  261. TRUE, (double) critical_time,
  262. FALSE, 0,
  263. FALSE, 0));
  264. if (status == STATE_WARNING) {
  265. printf("SLOW_SLAVE %s: %s|%s\n", _("WARNING"), slaveresult, perf);
  266. exit(STATE_WARNING);
  267. } else if (status == STATE_CRITICAL) {
  268. printf("SLOW_SLAVE %s: %s|%s\n", _("CRITICAL"), slaveresult, perf);
  269. exit(STATE_CRITICAL);
  270. }
  271. }
  272. }
  273. /* free the result */
  274. mysql_free_result (res);
  275. }
  276. /* close the connection */
  277. mysql_close (&mysql);
  278. /* print out the result of stats */
  279. if (check_slave) {
  280. printf ("%s %s|%s\n", result, slaveresult, perf);
  281. } else {
  282. printf ("%s|%s\n", result, perf);
  283. }
  284. return STATE_OK;
  285. }
  286. /* process command-line arguments */
  287. int
  288. process_arguments (int argc, char **argv)
  289. {
  290. int c;
  291. char *warning = NULL;
  292. char *critical = NULL;
  293. int option = 0;
  294. static struct option longopts[] = {
  295. {"hostname", required_argument, 0, 'H'},
  296. {"socket", required_argument, 0, 's'},
  297. {"database", required_argument, 0, 'd'},
  298. {"username", required_argument, 0, 'u'},
  299. {"password", required_argument, 0, 'p'},
  300. {"file", required_argument, 0, 'f'},
  301. {"group", required_argument, 0, 'g'},
  302. {"port", required_argument, 0, 'P'},
  303. {"critical", required_argument, 0, 'c'},
  304. {"warning", required_argument, 0, 'w'},
  305. {"check-slave", no_argument, 0, 'S'},
  306. {"ignore-auth", no_argument, 0, 'n'},
  307. {"verbose", no_argument, 0, 'v'},
  308. {"version", no_argument, 0, 'V'},
  309. {"help", no_argument, 0, 'h'},
  310. {"ssl", no_argument, 0, 'l'},
  311. {"ca-cert", optional_argument, 0, 'C'},
  312. {"key", required_argument,0,'k'},
  313. {"cert", required_argument,0,'a'},
  314. {"ca-dir", required_argument, 0, 'D'},
  315. {"ciphers", required_argument, 0, 'L'},
  316. {0, 0, 0, 0}
  317. };
  318. if (argc < 1)
  319. return ERROR;
  320. while (1) {
  321. c = getopt_long (argc, argv, "hlvVnSP:p:u:d:H:s:c:w:a:k:C:D:L:f:g:", longopts, &option);
  322. if (c == -1 || c == EOF)
  323. break;
  324. switch (c) {
  325. case 'H': /* hostname */
  326. if (is_host (optarg)) {
  327. db_host = optarg;
  328. }
  329. else {
  330. usage2 (_("Invalid hostname/address"), optarg);
  331. }
  332. break;
  333. case 's': /* socket */
  334. db_socket = optarg;
  335. break;
  336. case 'd': /* database */
  337. db = optarg;
  338. break;
  339. case 'l':
  340. ssl = true;
  341. break;
  342. case 'C':
  343. ca_cert = optarg;
  344. break;
  345. case 'a':
  346. cert = optarg;
  347. break;
  348. case 'k':
  349. key = optarg;
  350. break;
  351. case 'D':
  352. ca_dir = optarg;
  353. break;
  354. case 'L':
  355. ciphers = optarg;
  356. break;
  357. case 'u': /* username */
  358. db_user = optarg;
  359. break;
  360. case 'p': /* authentication information: password */
  361. db_pass = strdup(optarg);
  362. /* Delete the password from process list */
  363. while (*optarg != '\0') {
  364. *optarg = 'X';
  365. optarg++;
  366. }
  367. break;
  368. case 'f': /* client options file */
  369. opt_file = optarg;
  370. break;
  371. case 'g': /* client options group */
  372. opt_group = optarg;
  373. break;
  374. case 'P': /* critical time threshold */
  375. db_port = atoi (optarg);
  376. break;
  377. case 'S':
  378. check_slave = 1; /* check-slave */
  379. break;
  380. case 'n':
  381. ignore_auth = 1; /* ignore-auth */
  382. break;
  383. case 'w':
  384. warning = optarg;
  385. warning_time = strtod (warning, NULL);
  386. break;
  387. case 'c':
  388. critical = optarg;
  389. critical_time = strtod (critical, NULL);
  390. break;
  391. case 'V': /* version */
  392. print_revision (progname, NP_VERSION);
  393. exit (STATE_OK);
  394. case 'h': /* help */
  395. print_help ();
  396. exit (STATE_OK);
  397. case 'v':
  398. verbose++;
  399. break;
  400. case '?': /* help */
  401. usage5 ();
  402. }
  403. }
  404. c = optind;
  405. set_thresholds(&my_threshold, warning, critical);
  406. while ( argc > c ) {
  407. if (db_host == NULL)
  408. if (is_host (argv[c])) {
  409. db_host = argv[c++];
  410. }
  411. else {
  412. usage2 (_("Invalid hostname/address"), argv[c]);
  413. }
  414. else if (db_user == NULL)
  415. db_user = argv[c++];
  416. else if (db_pass == NULL)
  417. db_pass = argv[c++];
  418. else if (db == NULL)
  419. db = argv[c++];
  420. else if (is_intnonneg (argv[c]))
  421. db_port = atoi (argv[c++]);
  422. else
  423. break;
  424. }
  425. return validate_arguments ();
  426. }
  427. int
  428. validate_arguments (void)
  429. {
  430. if (db_user == NULL)
  431. db_user = strdup("");
  432. if (db_host == NULL)
  433. db_host = strdup("");
  434. if (db == NULL)
  435. db = strdup("");
  436. return OK;
  437. }
  438. void
  439. print_help (void)
  440. {
  441. char *myport;
  442. xasprintf (&myport, "%d", MYSQL_PORT);
  443. print_revision (progname, NP_VERSION);
  444. printf (_(COPYRIGHT), copyright, email);
  445. printf ("%s\n", _("This program tests connections to a MySQL server"));
  446. printf ("\n\n");
  447. print_usage ();
  448. printf (UT_HELP_VRSN);
  449. printf (UT_EXTRA_OPTS);
  450. printf (UT_HOST_PORT, 'P', myport);
  451. printf (" %s\n", "-n, --ignore-auth");
  452. printf (" %s\n", _("Ignore authentication failure and check for mysql connectivity only"));
  453. printf (" %s\n", "-s, --socket=STRING");
  454. printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
  455. printf (" %s\n", "-d, --database=STRING");
  456. printf (" %s\n", _("Check database with indicated name"));
  457. printf (" %s\n", "-f, --file=STRING");
  458. printf (" %s\n", _("Read from the specified client options file"));
  459. printf (" %s\n", "-g, --group=STRING");
  460. printf (" %s\n", _("Use a client options group"));
  461. printf (" %s\n", "-u, --username=STRING");
  462. printf (" %s\n", _("Connect using the indicated username"));
  463. printf (" %s\n", "-p, --password=STRING");
  464. printf (" %s\n", _("Use the indicated password to authenticate the connection"));
  465. printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
  466. printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
  467. printf (" %s\n", "-S, --check-slave");
  468. printf (" %s\n", _("Check if the slave thread is running properly."));
  469. printf (" %s\n", "-w, --warning");
  470. printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
  471. printf (" %s\n", _("behind master"));
  472. printf (" %s\n", "-c, --critical");
  473. printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
  474. printf (" %s\n", _("behind master"));
  475. printf (" %s\n", "-l, --ssl");
  476. printf (" %s\n", _("Use ssl encryptation"));
  477. printf (" %s\n", "-C, --ca-cert=STRING");
  478. printf (" %s\n", _("Path to CA signing the cert"));
  479. printf (" %s\n", "-a, --cert=STRING");
  480. printf (" %s\n", _("Path to SSL certificate"));
  481. printf (" %s\n", "-k, --key=STRING");
  482. printf (" %s\n", _("Path to private SSL key"));
  483. printf (" %s\n", "-D, --ca-dir=STRING");
  484. printf (" %s\n", _("Path to CA directory"));
  485. printf (" %s\n", "-L, --ciphers=STRING");
  486. printf (" %s\n", _("List of valid SSL ciphers"));
  487. printf ("\n");
  488. printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
  489. printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
  490. printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
  491. printf ("\n");
  492. printf ("%s\n", _("Notes:"));
  493. printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
  494. printf (" %s\n", _("overriding any my.cnf settings."));
  495. printf (UT_SUPPORT);
  496. }
  497. void
  498. print_usage (void)
  499. {
  500. printf ("%s\n", _("Usage:"));
  501. printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
  502. printf (" [-u user] [-p password] [-S] [-l] [-a cert] [-k key]\n");
  503. printf (" [-C ca-cert] [-D ca-dir] [-L ciphers] [-f optfile] [-g group]\n");
  504. }