check_mysql.c 7.2 KB

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