check_mysql_query.c 8.5 KB

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