check_mysql.c 14 KB

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