check_mysql.c 7.9 KB

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