check_mysql.c 8.2 KB

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