check_mysql.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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) 1999-2006 nagios-plugins team
  8. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  9. *
  10. * Last Modified: $Date$
  11. *
  12. * Description:
  13. *
  14. * This file contains the check_mysql plugin
  15. *
  16. * This program tests connections to a mysql server
  17. *
  18. *
  19. * License Information:
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. *
  35. * $Id$
  36. *
  37. ******************************************************************************/
  38. const char *progname = "check_mysql";
  39. const char *revision = "$Revision$";
  40. const char *copyright = "1999-2006";
  41. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  42. #define SLAVERESULTSIZE 70
  43. #include "common.h"
  44. #include "utils.h"
  45. #include "utils_base.h"
  46. #include "netutils.h"
  47. #include <mysql.h>
  48. #include <errmsg.h>
  49. char *db_user = NULL;
  50. char *db_host = NULL;
  51. char *db_pass = NULL;
  52. char *db = NULL;
  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. if (process_arguments (argc, argv) == ERROR)
  75. usage4 (_("Could not parse arguments"));
  76. /* initialize mysql */
  77. mysql_init (&mysql);
  78. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
  79. /* establish a connection to the server and error checking */
  80. if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
  81. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
  82. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  83. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  84. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  85. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  86. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  87. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  88. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  89. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  90. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  91. else
  92. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  93. }
  94. /* get the server stats */
  95. result = strdup (mysql_stat (&mysql));
  96. /* error checking once more */
  97. if (mysql_error (&mysql)) {
  98. if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
  99. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  100. else if (mysql_errno (&mysql) == CR_SERVER_LOST)
  101. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  102. else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
  103. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  104. }
  105. if(check_slave) {
  106. /* check the slave status */
  107. if (mysql_query (&mysql, "show slave status") != 0) {
  108. error = strdup(mysql_error(&mysql));
  109. mysql_close (&mysql);
  110. die (STATE_CRITICAL, _("slave query error: %s\n"), error);
  111. }
  112. /* store the result */
  113. if ( (res = mysql_store_result (&mysql)) == NULL) {
  114. error = strdup(mysql_error(&mysql));
  115. mysql_close (&mysql);
  116. die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
  117. }
  118. /* Check there is some data */
  119. if (mysql_num_rows(res) == 0) {
  120. mysql_close(&mysql);
  121. die (STATE_WARNING, "%s\n", _("No slaves defined"));
  122. }
  123. /* fetch the first row */
  124. if ( (row = mysql_fetch_row (res)) == NULL) {
  125. error = strdup(mysql_error(&mysql));
  126. mysql_free_result (res);
  127. mysql_close (&mysql);
  128. die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
  129. }
  130. if (mysql_field_count (&mysql) == 12) {
  131. /* mysql 3.23.x */
  132. snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
  133. if (strcmp (row[6], "Yes") != 0) {
  134. mysql_free_result (res);
  135. mysql_close (&mysql);
  136. die (STATE_CRITICAL, "%s\n", slaveresult);
  137. }
  138. } else {
  139. /* mysql 4.x.x */
  140. int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
  141. MYSQL_FIELD* fields;
  142. num_fields = mysql_num_fields(res);
  143. fields = mysql_fetch_fields(res);
  144. for(i = 0; i < num_fields; i++) {
  145. if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
  146. slave_io_field = i;
  147. continue;
  148. }
  149. if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
  150. slave_sql_field = i;
  151. continue;
  152. }
  153. if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
  154. seconds_behind_field = i;
  155. continue;
  156. }
  157. }
  158. if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
  159. mysql_free_result (res);
  160. mysql_close (&mysql);
  161. die (STATE_CRITICAL, "Slave status unavailable\n");
  162. }
  163. snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], row[seconds_behind_field]);
  164. if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
  165. mysql_free_result (res);
  166. mysql_close (&mysql);
  167. die (STATE_CRITICAL, "%s\n", slaveresult);
  168. }
  169. if (verbose >=3) {
  170. if (seconds_behind_field == -1) {
  171. printf("seconds_behind_field not found\n");
  172. } else {
  173. printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
  174. }
  175. }
  176. if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
  177. double value = atof(row[seconds_behind_field]);
  178. int status;
  179. status = get_status(value, my_threshold);
  180. if (status == STATE_WARNING) {
  181. printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
  182. exit(STATE_WARNING);
  183. } else if (status == STATE_CRITICAL) {
  184. printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
  185. exit(STATE_CRITICAL);
  186. }
  187. }
  188. }
  189. /* free the result */
  190. mysql_free_result (res);
  191. }
  192. /* close the connection */
  193. mysql_close (&mysql);
  194. /* print out the result of stats */
  195. if (check_slave) {
  196. printf ("%s %s\n", result, slaveresult);
  197. } else {
  198. printf ("%s\n", result);
  199. }
  200. return STATE_OK;
  201. }
  202. /* process command-line arguments */
  203. int
  204. process_arguments (int argc, char **argv)
  205. {
  206. int c;
  207. char *warning = NULL;
  208. char *critical = NULL;
  209. int option = 0;
  210. static struct option longopts[] = {
  211. {"hostname", required_argument, 0, 'H'},
  212. {"database", required_argument, 0, 'd'},
  213. {"username", required_argument, 0, 'u'},
  214. {"password", required_argument, 0, 'p'},
  215. {"port", required_argument, 0, 'P'},
  216. {"critical", required_argument, 0, 'c'},
  217. {"warning", required_argument, 0, 'w'},
  218. {"check-slave", no_argument, 0, 'S'},
  219. {"verbose", no_argument, 0, 'v'},
  220. {"version", no_argument, 0, 'V'},
  221. {"help", no_argument, 0, 'h'},
  222. {0, 0, 0, 0}
  223. };
  224. if (argc < 1)
  225. return ERROR;
  226. while (1) {
  227. c = getopt_long (argc, argv, "hvVSP:p:u:d:H:c:w:", longopts, &option);
  228. if (c == -1 || c == EOF)
  229. break;
  230. switch (c) {
  231. case 'H': /* hostname */
  232. if (is_host (optarg)) {
  233. db_host = optarg;
  234. }
  235. else {
  236. usage2 (_("Invalid hostname/address"), optarg);
  237. }
  238. break;
  239. case 'd': /* hostname */
  240. db = optarg;
  241. break;
  242. case 'u': /* username */
  243. db_user = optarg;
  244. break;
  245. case 'p': /* authentication information: password */
  246. db_pass = optarg;
  247. break;
  248. case 'P': /* critical time threshold */
  249. db_port = atoi (optarg);
  250. break;
  251. case 'S':
  252. check_slave = 1; /* check-slave */
  253. break;
  254. case 'w':
  255. warning = optarg;
  256. break;
  257. case 'c':
  258. critical = optarg;
  259. break;
  260. case 'V': /* version */
  261. print_revision (progname, revision);
  262. exit (STATE_OK);
  263. case 'h': /* help */
  264. print_help ();
  265. exit (STATE_OK);
  266. case 'v':
  267. verbose++;
  268. break;
  269. case '?': /* help */
  270. usage5 ();
  271. }
  272. }
  273. c = optind;
  274. set_thresholds(&my_threshold, warning, critical);
  275. while ( argc > c ) {
  276. if (strlen(db_host) == 0)
  277. if (is_host (argv[c])) {
  278. db_host = argv[c++];
  279. }
  280. else {
  281. usage2 (_("Invalid hostname/address"), optarg);
  282. }
  283. else if (strlen(db_user) == 0)
  284. db_user = argv[c++];
  285. else if (strlen(db_pass) == 0)
  286. db_pass = argv[c++];
  287. else if (strlen(db) == 0)
  288. db = argv[c++];
  289. else if (is_intnonneg (argv[c]))
  290. db_port = atoi (argv[c++]);
  291. else
  292. break;
  293. }
  294. return validate_arguments ();
  295. }
  296. int
  297. validate_arguments (void)
  298. {
  299. if (db_user == NULL)
  300. db_user = strdup("");
  301. if (db_host == NULL)
  302. db_host = strdup("");
  303. if (db_pass == NULL)
  304. db_pass == strdup("");
  305. if (db == NULL)
  306. db = strdup("");
  307. return OK;
  308. }
  309. void
  310. print_help (void)
  311. {
  312. char *myport;
  313. asprintf (&myport, "%d", MYSQL_PORT);
  314. print_revision (progname, revision);
  315. printf (_(COPYRIGHT), copyright, email);
  316. printf ("%s\n", _("This program tests connections to a mysql server"));
  317. printf ("\n\n");
  318. print_usage ();
  319. printf (_(UT_HELP_VRSN));
  320. printf (_(UT_HOST_PORT), 'P', myport);
  321. printf (" %s\n", "-d, --database=STRING");
  322. printf (" %s\n", _("Check database with indicated name"));
  323. printf (" %s\n", "-u, --username=STRING");
  324. printf (" %s\n", _("Connect using the indicated username"));
  325. printf (" %s\n", "-p, --password=STRING");
  326. printf (" %s\n", _("Use the indicated password to authenticate the connection"));
  327. printf (" %s\n", _("==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <=="));
  328. printf (" %s\n", _("Your clear-text password will be visible as a process table entry"));
  329. printf (" %s\n", "-S, --check-slave");
  330. printf (" %s\n", _("Check if the slave thread is running properly."));
  331. printf (" %s\n", "-w, --warning");
  332. printf (" %s\n", _("Exit with WARNING status if slave server is more then INTEGER seconds behind master"));
  333. printf (" %s\n", "-c, --critical");
  334. printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds behind master"));
  335. printf (" %s\n", _("There are no required arguments. By default, the local database with"));
  336. printf (_("a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
  337. printf (_(UT_SUPPORT));
  338. }
  339. void
  340. print_usage (void)
  341. {
  342. printf (_("Usage:"));
  343. printf ("%s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",progname);
  344. }