check_mysql.c 10.0 KB

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