check_mysql.c 11 KB

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