check_mysql.c 7.9 KB

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