4
0

check_mysql.c 15 KB

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