check_mysql.c 5.5 KB

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