check_mysql.c 5.7 KB

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