4
0

check_mysql_query.c 9.4 KB

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