check_mysql_query.c 7.2 KB

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