check_mysql_query.c 7.9 KB

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