check_mysql.c 13 KB

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