check_mysql.c 5.6 KB

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