check_mysql.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "common.h"
  21. #include "utils.h"
  22. #include "netutils.h"
  23. #include <mysql/mysql.h>
  24. #include <mysql/errmsg.h>
  25. unsigned int db_port = MYSQL_PORT;
  26. void
  27. print_usage (void)
  28. {
  29. printf (_("\
  30. Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"),
  31. progname);
  32. printf (_(UT_HLP_VRS), progname, progname);
  33. }
  34. void
  35. print_help (void)
  36. {
  37. print_revision (progname, revision);
  38. printf (_(COPYRIGHT), copyright, email);
  39. printf (_("This program tests connections to a mysql server\n"));
  40. print_usage ();
  41. printf (_(UT_HELP_VRSN));
  42. printf (_(UT_HOST_PORT), 'P', atoi(MYSQL_PORT));
  43. printf (_("\
  44. -d, --database=STRING\n\
  45. Check database with indicated name\n\
  46. -u, --username=STRING\n\
  47. Connect using the indicated username\n\
  48. -p, --password=STRING\n\
  49. Use the indicated password to authenticate the connection\n\
  50. ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
  51. Your clear-text password will be visible as a process table entry\n"));
  52. printf (_("\n\
  53. There are no required arguments. By default, the local database with\n\
  54. a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
  55. printf (_(UT_SUPPORT));
  56. }
  57. char *db_user = "";
  58. char *db_host = "";
  59. char *db_pass = "";
  60. char *db = "";
  61. int process_arguments (int, char **);
  62. int validate_arguments (void);
  63. int
  64. main (int argc, char **argv)
  65. {
  66. MYSQL mysql;
  67. char result[1024];
  68. if (process_arguments (argc, argv) != OK)
  69. usage (_("Invalid command arguments supplied\n"));
  70. /* initialize mysql */
  71. mysql_init (&mysql);
  72. /* establish a connection to the server and error checking */
  73. if (!mysql_real_connect
  74. (&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) {
  75. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
  76. printf ("%s\n", mysql_error (&mysql));
  77. return STATE_WARNING;
  78. }
  79. else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
  80. printf ("%s\n", mysql_error (&mysql));
  81. return STATE_WARNING;
  82. }
  83. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
  84. printf ("%s\n", mysql_error (&mysql));
  85. return STATE_WARNING;
  86. }
  87. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
  88. printf ("%s\n", mysql_error (&mysql));
  89. return STATE_WARNING;
  90. }
  91. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
  92. printf ("%s\n", mysql_error (&mysql));
  93. return STATE_WARNING;
  94. }
  95. else {
  96. printf ("%s\n", mysql_error (&mysql));
  97. return STATE_CRITICAL;
  98. }
  99. }
  100. /* get the server stats */
  101. sprintf (result, mysql_stat (&mysql));
  102. /* error checking once more */
  103. if (mysql_error (&mysql)) {
  104. if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
  105. printf ("%s\n", mysql_error (&mysql));
  106. return STATE_CRITICAL;
  107. }
  108. else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
  109. printf ("%s\n", mysql_error (&mysql));
  110. return STATE_CRITICAL;
  111. }
  112. else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
  113. printf ("%s\n", mysql_error (&mysql));
  114. return STATE_UNKNOWN;
  115. }
  116. }
  117. /* close the connection */
  118. mysql_close (&mysql);
  119. /* print out the result of stats */
  120. printf ("%s\n", result);
  121. return STATE_OK;
  122. }
  123. /* process command-line arguments */
  124. int
  125. process_arguments (int argc, char **argv)
  126. {
  127. int c;
  128. int option_index = 0;
  129. static struct option long_options[] = {
  130. {"hostname", required_argument, 0, 'H'},
  131. {"database", required_argument, 0, 'd'},
  132. {"username", required_argument, 0, 'u'},
  133. {"password", required_argument, 0, 'p'},
  134. {"port", required_argument, 0, 'P'},
  135. {"verbose", no_argument, 0, 'v'},
  136. {"version", no_argument, 0, 'V'},
  137. {"help", no_argument, 0, 'h'},
  138. {0, 0, 0, 0}
  139. };
  140. if (argc < 1)
  141. return ERROR;
  142. while (1) {
  143. c = getopt_long (argc, argv, "hVP:p:u:d:H:", long_options, &option_index);
  144. if (c == -1 || c == EOF)
  145. break;
  146. switch (c) {
  147. case 'H': /* hostname */
  148. if (is_host (optarg)) {
  149. db_host = optarg;
  150. }
  151. else {
  152. usage (_("Invalid host name\n"));
  153. }
  154. break;
  155. case 'd': /* hostname */
  156. db = optarg;
  157. break;
  158. case 'u': /* username */
  159. db_user = optarg;
  160. break;
  161. case 'p': /* authentication information: password */
  162. db_pass = optarg;
  163. break;
  164. case 'P': /* critical time threshold */
  165. db_port = atoi (optarg);
  166. break;
  167. case 'V': /* version */
  168. print_revision (progname, revision);
  169. exit (STATE_OK);
  170. case 'h': /* help */
  171. print_help ();
  172. exit (STATE_OK);
  173. case '?': /* help */
  174. usage (_("Invalid argument\n"));
  175. }
  176. }
  177. c = optind;
  178. while ( argc > c ) {
  179. if (strlen(db_host) == 0)
  180. if (is_host (argv[c])) {
  181. db_host = argv[c++];
  182. }
  183. else {
  184. usage ("Invalid host name");
  185. }
  186. else if (strlen(db_user) == 0)
  187. db_user = argv[c++];
  188. else if (strlen(db_pass) == 0)
  189. db_pass = argv[c++];
  190. else if (strlen(db) == 0)
  191. db = argv[c++];
  192. else if (is_intnonneg (argv[c]))
  193. db_port = atoi (argv[c++]);
  194. else
  195. break;
  196. }
  197. return validate_arguments ();
  198. }
  199. int
  200. validate_arguments (void)
  201. {
  202. return OK;
  203. }