check_mysql.c 15 KB

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