check_mysql.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #define PROGNAME "check_mysql"
  17. #include "common.h"
  18. #include "utils.h"
  19. #include <mysql/mysql.h>
  20. #include <mysql/errmsg.h>
  21. char *db_user = NULL;
  22. char *db_host = NULL;
  23. char *db_pass = NULL;
  24. char *db = NULL;
  25. unsigned int db_port = MYSQL_PORT;
  26. int process_arguments (int, char **);
  27. int call_getopt (int, char **);
  28. int validate_arguments (void);
  29. int check_disk (int usp, int free_disk);
  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. if (argc < 1)
  98. return ERROR;
  99. c = 0;
  100. while ((c += (call_getopt (argc - c, &argv[c]))) < argc) {
  101. if (is_option (argv[c]))
  102. continue;
  103. if (db_host == NULL)
  104. if (is_host (argv[c])) {
  105. db_host = argv[c];
  106. }
  107. else {
  108. usage ("Invalid host name");
  109. }
  110. else if (db_user == NULL)
  111. db_user = argv[c];
  112. else if (db_pass == NULL)
  113. db_pass = argv[c];
  114. else if (db == NULL)
  115. db = argv[c];
  116. else if (is_intnonneg (argv[c]))
  117. db_port = atoi (argv[c]);
  118. }
  119. if (db_host == NULL)
  120. db_host = strscpy (db_host, "127.0.0.1");
  121. return validate_arguments ();
  122. }
  123. int
  124. call_getopt (int argc, char **argv)
  125. {
  126. int c, i = 0;
  127. #ifdef HAVE_GETOPT_H
  128. int option_index = 0;
  129. static struct option long_options[] = {
  130. {"hostname", required_argument, 0, 'H'},
  131. {"database", required_argument, 0, 'd'},
  132. {"username", required_argument, 0, 'u'},
  133. {"password", required_argument, 0, 'p'},
  134. {"port", required_argument, 0, 'P'},
  135. {"verbose", no_argument, 0, 'v'},
  136. {"version", no_argument, 0, 'V'},
  137. {"help", no_argument, 0, 'h'},
  138. {0, 0, 0, 0}
  139. };
  140. #endif
  141. while (1) {
  142. #ifdef HAVE_GETOPT_H
  143. c =
  144. getopt_long (argc, argv, "+hVP:p:u:d:H:", long_options, &option_index);
  145. #else
  146. c = getopt (argc, argv, "+?hVP:p:u:d:H:");
  147. #endif
  148. i++;
  149. if (c == -1 || c == EOF || c == 1)
  150. break;
  151. switch (c) {
  152. case 'P':
  153. case 'p':
  154. case 'u':
  155. case 'd':
  156. case 'H':
  157. i++;
  158. }
  159. switch (c) {
  160. case 'H': /* hostname */
  161. if (is_host (optarg)) {
  162. db_host = optarg;
  163. }
  164. else {
  165. usage ("Invalid host name\n");
  166. }
  167. break;
  168. case 'd': /* hostname */
  169. db = optarg;
  170. break;
  171. case 'u': /* username */
  172. db_user = optarg;
  173. break;
  174. case 'p': /* authentication information: password */
  175. db_pass = optarg;
  176. break;
  177. case 'P': /* critical time threshold */
  178. db_port = atoi (optarg);
  179. break;
  180. case 'V': /* version */
  181. print_revision (my_basename (argv[0]), "$Revision$");
  182. exit (STATE_OK);
  183. case 'h': /* help */
  184. print_help ();
  185. exit (STATE_OK);
  186. case '?': /* help */
  187. usage ("Invalid argument\n");
  188. }
  189. }
  190. return i;
  191. }
  192. int
  193. validate_arguments (void)
  194. {
  195. return OK;
  196. }
  197. void
  198. print_help (void)
  199. {
  200. print_revision (PROGNAME, "$Revision$");
  201. printf
  202. ("Copyright (c) 2000 Didi Rieder/Karl DeBisschop\n\n"
  203. "This plugin is for testing a mysql server.\n");
  204. print_usage ();
  205. printf
  206. ("\nThere are no required arguments. By default, the local database with\n"
  207. "a server listening on MySQL standard port %d will be checked\n\n"
  208. "Options:\n"
  209. " -d, --database=STRING\n"
  210. " Check database with indicated name\n"
  211. " -H, --hostname=STRING or IPADDRESS\n"
  212. " Check server on the indicated host\n"
  213. " -P, --port=INTEGER\n"
  214. " Make connection on the indicated port\n"
  215. " -u, --username=STRING\n"
  216. " Connect using the indicated username\n"
  217. " -p, --password=STRING\n"
  218. " Use the indicated password to authenticate the connection\n"
  219. " ==> IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!! <==\n"
  220. " Your clear-text password will be visible as a process table entry\n"
  221. " -h, --help\n"
  222. " Print detailed help screen\n"
  223. " -V, --version\n" " Print version information\n\n", MYSQL_PORT);
  224. support ();
  225. }
  226. void
  227. print_usage (void)
  228. {
  229. printf
  230. ("Usage: %s [-d database] [-H host] [-P port] [-u user] [-p password]\n"
  231. " %s --help\n"
  232. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  233. }