check_mysql_query.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /******************************************************************************
  2. *
  3. * CHECK_MYSQL_QUERY.C
  4. *
  5. * Program: Mysql plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 2006 Nagios Plugins Team, after Didi Rieder (check_mysql)
  8. *
  9. * $Id$
  10. *
  11. * Description:
  12. * This plugin is for running arbitrary SQL and checking the results
  13. *
  14. ******************************************************************************/
  15. const char *progname = "check_mysql_query";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2006";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "utils.h"
  21. #include "netutils.h"
  22. #include <mysql.h>
  23. #include <errmsg.h>
  24. char *db_user = NULL;
  25. char *db_host = NULL;
  26. char *db_pass = NULL;
  27. char *db = NULL;
  28. unsigned int db_port = MYSQL_PORT;
  29. int process_arguments (int, char **);
  30. int validate_arguments (void);
  31. void print_help (void);
  32. void print_usage (void);
  33. char *sql_query = NULL;
  34. int verbose = 0;
  35. thresholds *my_thresholds = NULL;
  36. int
  37. main (int argc, char **argv)
  38. {
  39. MYSQL mysql;
  40. MYSQL_RES *res;
  41. MYSQL_ROW row;
  42. double value;
  43. char *error = NULL;
  44. int status;
  45. setlocale (LC_ALL, "");
  46. bindtextdomain (PACKAGE, LOCALEDIR);
  47. textdomain (PACKAGE);
  48. if (process_arguments (argc, argv) == ERROR)
  49. usage4 (_("Could not parse arguments"));
  50. /* initialize mysql */
  51. mysql_init (&mysql);
  52. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
  53. /* establish a connection to the server and error checking */
  54. if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,NULL,0)) {
  55. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
  56. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  57. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  58. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  59. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  60. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  61. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  62. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  63. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  64. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  65. else
  66. die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
  67. }
  68. if (mysql_query (&mysql, sql_query) != 0) {
  69. error = strdup(mysql_error(&mysql));
  70. mysql_close (&mysql);
  71. die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
  72. }
  73. /* store the result */
  74. if ( (res = mysql_store_result (&mysql)) == NULL) {
  75. error = strdup(mysql_error(&mysql));
  76. mysql_close (&mysql);
  77. die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
  78. }
  79. /* Check there is some data */
  80. if (mysql_num_rows(res) == 0) {
  81. mysql_close(&mysql);
  82. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
  83. }
  84. /* fetch the first row */
  85. if ( (row = mysql_fetch_row (res)) == NULL) {
  86. error = strdup(mysql_error(&mysql));
  87. mysql_free_result (res);
  88. mysql_close (&mysql);
  89. die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
  90. }
  91. /* free the result */
  92. mysql_free_result (res);
  93. /* close the connection */
  94. mysql_close (&mysql);
  95. if (! is_numeric(row[0])) {
  96. die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
  97. }
  98. value = strtod(row[0], NULL);
  99. if (verbose >= 3)
  100. printf("mysql result: %f\n", value);
  101. status = get_status(value, my_thresholds);
  102. if (status == STATE_OK) {
  103. printf("QUERY %s: ", _("OK"));
  104. } else if (status == STATE_WARNING) {
  105. printf("QUERY %s: ", _("WARNING"));
  106. } else if (status == STATE_CRITICAL) {
  107. printf("QUERY %s: ", _("CRITICAL"));
  108. }
  109. printf(_("'%s' returned %f"), sql_query, value);
  110. printf("\n");
  111. return status;
  112. }
  113. /* process command-line arguments */
  114. int
  115. process_arguments (int argc, char **argv)
  116. {
  117. int c;
  118. char *warning = NULL;
  119. char *critical = NULL;
  120. int option = 0;
  121. static struct option longopts[] = {
  122. {"hostname", required_argument, 0, 'H'},
  123. {"database", required_argument, 0, 'd'},
  124. {"username", required_argument, 0, 'u'},
  125. {"password", required_argument, 0, 'p'},
  126. {"port", required_argument, 0, 'P'},
  127. {"verbose", no_argument, 0, 'v'},
  128. {"version", no_argument, 0, 'V'},
  129. {"help", no_argument, 0, 'h'},
  130. {"query", required_argument, 0, 'q'},
  131. {"warning", required_argument, 0, 'w'},
  132. {"critical", required_argument, 0, 'c'},
  133. {0, 0, 0, 0}
  134. };
  135. if (argc < 1)
  136. return ERROR;
  137. while (1) {
  138. c = getopt_long (argc, argv, "hvVSP:p:u:d:H:q:w:c:", longopts, &option);
  139. if (c == -1 || c == EOF)
  140. break;
  141. switch (c) {
  142. case 'H': /* hostname */
  143. if (is_host (optarg)) {
  144. db_host = optarg;
  145. }
  146. else {
  147. usage2 (_("Invalid hostname/address"), optarg);
  148. }
  149. break;
  150. case 'd': /* hostname */
  151. db = optarg;
  152. break;
  153. case 'u': /* username */
  154. db_user = optarg;
  155. break;
  156. case 'p': /* authentication information: password */
  157. asprintf(&db_pass, "%s", optarg);
  158. /* Delete the password from process list */
  159. while (*optarg != '\0') {
  160. *optarg = 'X';
  161. optarg++;
  162. }
  163. break;
  164. case 'P': /* critical time threshold */
  165. db_port = atoi (optarg);
  166. break;
  167. case 'v':
  168. verbose++;
  169. break;
  170. case 'V': /* version */
  171. print_revision (progname, revision);
  172. exit (STATE_OK);
  173. case 'h': /* help */
  174. print_help ();
  175. exit (STATE_OK);
  176. case 'q':
  177. asprintf(&sql_query, "%s", optarg);
  178. break;
  179. case 'w':
  180. warning = optarg;
  181. break;
  182. case 'c':
  183. critical = optarg;
  184. break;
  185. case '?': /* help */
  186. usage2 (_("Unknown argument"), optarg);
  187. }
  188. }
  189. c = optind;
  190. set_thresholds(&my_thresholds, warning, critical);
  191. return validate_arguments ();
  192. }
  193. int
  194. validate_arguments (void)
  195. {
  196. if (sql_query == NULL)
  197. usage("Must specify a SQL query to run");
  198. if (db_user == NULL)
  199. db_user = strdup("");
  200. if (db_host == NULL)
  201. db_host = strdup("");
  202. if (db_pass == NULL)
  203. db_pass == strdup("");
  204. if (db == NULL)
  205. db = strdup("");
  206. return OK;
  207. }
  208. void
  209. print_help (void)
  210. {
  211. char *myport;
  212. asprintf (&myport, "%d", MYSQL_PORT);
  213. print_revision (progname, revision);
  214. printf (_(COPYRIGHT), copyright, email);
  215. printf ("%s\n", _("This program checks a query result against threshold levels"));
  216. printf ("\n\n");
  217. print_usage ();
  218. printf (_(UT_HELP_VRSN));
  219. printf (" -q, --query=STRING\n");
  220. printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
  221. printf (_(UT_WARN_CRIT_RANGE));
  222. printf (_(UT_HOST_PORT), 'P', myport);
  223. printf (" -d, --database=STRING\n");
  224. printf (" %s\n", _("Database to check"));
  225. printf (" -u, --username=STRING\n");
  226. printf (" %s\n", _("Username to login with"));
  227. printf (" -p, --password=STRING\n");
  228. printf (" %s\n", _("Password to login with"));
  229. printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
  230. printf ("\n");
  231. printf ("%s\n", _("A query is required. The result from the query should be numeric."));
  232. printf ("%s\n", _("For extra security, create a user with minimal access."));
  233. printf (_(UT_SUPPORT));
  234. }
  235. void
  236. print_usage (void)
  237. {
  238. printf (_("Usage:"));
  239. printf ("%s -q SQL_query [-w warn] [-c crit]\n",progname);
  240. printf ("[-d database] [-H host] [-P port] [-u user] [-p password]\n");
  241. }