check_mysql_query.c 8.0 KB

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