check_mysql.c 16 KB

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