check_mysql.c 9.6 KB

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