check_mysql.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s", row[9], row[10]);
  106. if (strcmp (row[9], "Yes") != 0 || strcmp (row[10], "Yes") != 0) {
  107. mysql_free_result (res);
  108. mysql_close (&mysql);
  109. die (STATE_CRITICAL, "%s\n", slaveresult);
  110. }
  111. }
  112. /* free the result */
  113. mysql_free_result (res);
  114. }
  115. /* close the connection */
  116. mysql_close (&mysql);
  117. /* print out the result of stats */
  118. if (check_slave) {
  119. printf ("%s %s\n", result, slaveresult);
  120. } else {
  121. printf ("%s\n", result);
  122. }
  123. return STATE_OK;
  124. }
  125. /* process command-line arguments */
  126. int
  127. process_arguments (int argc, char **argv)
  128. {
  129. int c;
  130. int option = 0;
  131. static struct option longopts[] = {
  132. {"hostname", required_argument, 0, 'H'},
  133. {"database", required_argument, 0, 'd'},
  134. {"username", required_argument, 0, 'u'},
  135. {"password", required_argument, 0, 'p'},
  136. {"port", required_argument, 0, 'P'},
  137. {"check-slave", no_argument, 0, 'S'},
  138. {"verbose", no_argument, 0, 'v'},
  139. {"version", no_argument, 0, 'V'},
  140. {"help", no_argument, 0, 'h'},
  141. {0, 0, 0, 0}
  142. };
  143. if (argc < 1)
  144. return ERROR;
  145. while (1) {
  146. c = getopt_long (argc, argv, "hVSP:p:u:d:H:", longopts, &option);
  147. if (c == -1 || c == EOF)
  148. break;
  149. switch (c) {
  150. case 'H': /* hostname */
  151. if (is_host (optarg)) {
  152. db_host = optarg;
  153. }
  154. else {
  155. usage2 (_("Invalid hostname/address"), optarg);
  156. }
  157. break;
  158. case 'd': /* hostname */
  159. db = optarg;
  160. break;
  161. case 'u': /* username */
  162. db_user = optarg;
  163. break;
  164. case 'p': /* authentication information: password */
  165. db_pass = optarg;
  166. break;
  167. case 'P': /* critical time threshold */
  168. db_port = atoi (optarg);
  169. break;
  170. case 'S':
  171. check_slave = 1; /* check-slave */
  172. break;
  173. case 'V': /* version */
  174. print_revision (progname, revision);
  175. exit (STATE_OK);
  176. case 'h': /* help */
  177. print_help ();
  178. exit (STATE_OK);
  179. case '?': /* help */
  180. usage2 (_("Unknown argument"), optarg);
  181. }
  182. }
  183. c = optind;
  184. while ( argc > c ) {
  185. if (strlen(db_host) == 0)
  186. if (is_host (argv[c])) {
  187. db_host = argv[c++];
  188. }
  189. else {
  190. usage2 (_("Invalid hostname/address"), optarg);
  191. }
  192. else if (strlen(db_user) == 0)
  193. db_user = argv[c++];
  194. else if (strlen(db_pass) == 0)
  195. db_pass = argv[c++];
  196. else if (strlen(db) == 0)
  197. db = argv[c++];
  198. else if (is_intnonneg (argv[c]))
  199. db_port = atoi (argv[c++]);
  200. else
  201. break;
  202. }
  203. return validate_arguments ();
  204. }
  205. int
  206. validate_arguments (void)
  207. {
  208. if (db_user == NULL)
  209. db_user = strdup("");
  210. if (db_host == NULL)
  211. db_host = strdup("");
  212. if (db_pass == NULL)
  213. db_pass == strdup("");
  214. if (db == NULL)
  215. db = strdup("");
  216. return OK;
  217. }
  218. void
  219. print_help (void)
  220. {
  221. char *myport;
  222. asprintf (&myport, "%d", MYSQL_PORT);
  223. print_revision (progname, revision);
  224. printf (_(COPYRIGHT), copyright, email);
  225. printf (_("This program tests connections to a mysql server\n"));
  226. print_usage ();
  227. printf (_(UT_HELP_VRSN));
  228. printf (_(UT_HOST_PORT), 'P', myport);
  229. printf (_("\
  230. -d, --database=STRING\n\
  231. Check database with indicated name\n\
  232. -u, --username=STRING\n\
  233. Connect using the indicated username\n\
  234. -p, --password=STRING\n\
  235. Use the indicated password to authenticate the connection\n\
  236. ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
  237. Your clear-text password will be visible as a process table entry\n\
  238. -S, --check-slave\n\
  239. Check if the slave thread is running properly.\n"));
  240. printf (_("\n\
  241. There are no required arguments. By default, the local database with\n\
  242. a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
  243. printf (_(UT_SUPPORT));
  244. }
  245. void
  246. print_usage (void)
  247. {
  248. printf ("\
  249. Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password] [-S]\n",
  250. progname);
  251. }