check_mysql.c 5.6 KB

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