check_mysql.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. void
  26. print_usage (void)
  27. {
  28. printf (_("\
  29. Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"),
  30. progname);
  31. printf (_(UT_HLP_VRS), progname, progname);
  32. }
  33. void
  34. print_help (void)
  35. {
  36. char *myport;
  37. asprintf (&myport, "%d", MYSQL_PORT);
  38. print_revision (progname, revision);
  39. printf (_(COPYRIGHT), copyright, email);
  40. printf (_("This program tests connections to a mysql server\n"));
  41. print_usage ();
  42. printf (_(UT_HELP_VRSN));
  43. printf (_(UT_HOST_PORT), 'P', myport);
  44. printf (_("\
  45. -d, --database=STRING\n\
  46. Check database with indicated name\n\
  47. -u, --username=STRING\n\
  48. Connect using the indicated username\n\
  49. -p, --password=STRING\n\
  50. Use the indicated password to authenticate the connection\n\
  51. ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n\
  52. Your clear-text password will be visible as a process table entry\n"));
  53. printf (_("\n\
  54. There are no required arguments. By default, the local database with\n\
  55. a server listening on MySQL standard port %d will be checked\n"), MYSQL_PORT);
  56. printf (_(UT_SUPPORT));
  57. }
  58. char *db_user = "";
  59. char *db_host = "";
  60. char *db_pass = "";
  61. char *db = "";
  62. unsigned int db_port = MYSQL_PORT;
  63. int process_arguments (int, char **);
  64. int validate_arguments (void);
  65. int
  66. main (int argc, char **argv)
  67. {
  68. MYSQL mysql;
  69. char result[1024];
  70. if (process_arguments (argc, argv) != OK)
  71. usage (_("Invalid command arguments supplied\n"));
  72. /* initialize mysql */
  73. mysql_init (&mysql);
  74. /* establish a connection to the server and error checking */
  75. if (!mysql_real_connect
  76. (&mysql, db_host, db_user, db_pass, db, db_port, NULL, 0)) {
  77. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST) {
  78. printf ("%s\n", mysql_error (&mysql));
  79. return STATE_WARNING;
  80. }
  81. else if (mysql_errno (&mysql) == CR_VERSION_ERROR) {
  82. printf ("%s\n", mysql_error (&mysql));
  83. return STATE_WARNING;
  84. }
  85. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY) {
  86. printf ("%s\n", mysql_error (&mysql));
  87. return STATE_WARNING;
  88. }
  89. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR) {
  90. printf ("%s\n", mysql_error (&mysql));
  91. return STATE_WARNING;
  92. }
  93. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR) {
  94. printf ("%s\n", mysql_error (&mysql));
  95. return STATE_WARNING;
  96. }
  97. else {
  98. printf ("%s\n", mysql_error (&mysql));
  99. return STATE_CRITICAL;
  100. }
  101. }
  102. /* get the server stats */
  103. sprintf (result, mysql_stat (&mysql));
  104. /* error checking once more */
  105. if (mysql_error (&mysql)) {
  106. if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR) {
  107. printf ("%s\n", mysql_error (&mysql));
  108. return STATE_CRITICAL;
  109. }
  110. else if (mysql_errno (&mysql) == CR_SERVER_LOST) {
  111. printf ("%s\n", mysql_error (&mysql));
  112. return STATE_CRITICAL;
  113. }
  114. else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR) {
  115. printf ("%s\n", mysql_error (&mysql));
  116. return STATE_UNKNOWN;
  117. }
  118. }
  119. /* close the connection */
  120. mysql_close (&mysql);
  121. /* print out the result of stats */
  122. printf ("%s\n", result);
  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_index = 0;
  131. static struct option long_options[] = {
  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. {"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, "hVP:p:u:d:H:", long_options, &option_index);
  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. usage (_("Invalid host name\n"));
  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 'V': /* version */
  170. print_revision (progname, revision);
  171. exit (STATE_OK);
  172. case 'h': /* help */
  173. print_help ();
  174. exit (STATE_OK);
  175. case '?': /* help */
  176. usage (_("Invalid argument\n"));
  177. }
  178. }
  179. c = optind;
  180. while ( argc > c ) {
  181. if (strlen(db_host) == 0)
  182. if (is_host (argv[c])) {
  183. db_host = argv[c++];
  184. }
  185. else {
  186. usage ("Invalid host name");
  187. }
  188. else if (strlen(db_user) == 0)
  189. db_user = argv[c++];
  190. else if (strlen(db_pass) == 0)
  191. db_pass = argv[c++];
  192. else if (strlen(db) == 0)
  193. db = argv[c++];
  194. else if (is_intnonneg (argv[c]))
  195. db_port = atoi (argv[c++]);
  196. else
  197. break;
  198. }
  199. return validate_arguments ();
  200. }
  201. int
  202. validate_arguments (void)
  203. {
  204. return OK;
  205. }