check_mysql_query.c 8.3 KB

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