check_mysql_query.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. unsigned int db_port = MYSQL_PORT;
  46. int process_arguments (int, char **);
  47. int validate_arguments (void);
  48. void print_help (void);
  49. void print_usage (void);
  50. char *sql_query = NULL;
  51. int verbose = 0;
  52. thresholds *my_thresholds = NULL;
  53. int
  54. main (int argc, char **argv)
  55. {
  56. MYSQL mysql;
  57. MYSQL_RES *res;
  58. MYSQL_ROW row;
  59. double value;
  60. char *error = NULL;
  61. int status;
  62. setlocale (LC_ALL, "");
  63. bindtextdomain (PACKAGE, LOCALEDIR);
  64. textdomain (PACKAGE);
  65. /* Parse extra opts if any */
  66. argv=np_extra_opts (&argc, argv, progname);
  67. if (process_arguments (argc, argv) == ERROR)
  68. usage4 (_("Could not parse arguments"));
  69. /* initialize mysql */
  70. mysql_init (&mysql);
  71. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
  72. /* establish a connection to the server and error checking */
  73. if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
  74. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
  75. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  76. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  77. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  78. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  79. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  80. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  81. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  82. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  83. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  84. else
  85. die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
  86. }
  87. if (mysql_query (&mysql, sql_query) != 0) {
  88. error = strdup(mysql_error(&mysql));
  89. mysql_close (&mysql);
  90. die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
  91. }
  92. /* store the result */
  93. if ( (res = mysql_store_result (&mysql)) == NULL) {
  94. error = strdup(mysql_error(&mysql));
  95. mysql_close (&mysql);
  96. die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
  97. }
  98. /* Check there is some data */
  99. if (mysql_num_rows(res) == 0) {
  100. mysql_close(&mysql);
  101. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
  102. }
  103. /* fetch the first row */
  104. if ( (row = mysql_fetch_row (res)) == NULL) {
  105. error = strdup(mysql_error(&mysql));
  106. mysql_free_result (res);
  107. mysql_close (&mysql);
  108. die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
  109. }
  110. /* free the result */
  111. mysql_free_result (res);
  112. /* close the connection */
  113. mysql_close (&mysql);
  114. if (! is_numeric(row[0])) {
  115. die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
  116. }
  117. value = strtod(row[0], NULL);
  118. if (verbose >= 3)
  119. printf("mysql result: %f\n", value);
  120. status = get_status(value, my_thresholds);
  121. if (status == STATE_OK) {
  122. printf("QUERY %s: ", _("OK"));
  123. } else if (status == STATE_WARNING) {
  124. printf("QUERY %s: ", _("WARNING"));
  125. } else if (status == STATE_CRITICAL) {
  126. printf("QUERY %s: ", _("CRITICAL"));
  127. }
  128. printf(_("'%s' returned %f | %s"), sql_query, value,
  129. fperfdata("result", value, "",
  130. my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
  131. my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
  132. FALSE, 0,
  133. FALSE, 0)
  134. );
  135. printf("\n");
  136. return status;
  137. }
  138. /* process command-line arguments */
  139. int
  140. process_arguments (int argc, char **argv)
  141. {
  142. int c;
  143. char *warning = NULL;
  144. char *critical = NULL;
  145. int option = 0;
  146. static struct option longopts[] = {
  147. {"hostname", required_argument, 0, 'H'},
  148. {"socket", required_argument, 0, 's'},
  149. {"database", required_argument, 0, 'd'},
  150. {"username", required_argument, 0, 'u'},
  151. {"password", required_argument, 0, 'p'},
  152. {"port", required_argument, 0, 'P'},
  153. {"verbose", no_argument, 0, 'v'},
  154. {"version", no_argument, 0, 'V'},
  155. {"help", no_argument, 0, 'h'},
  156. {"query", required_argument, 0, 'q'},
  157. {"warning", required_argument, 0, 'w'},
  158. {"critical", required_argument, 0, 'c'},
  159. {0, 0, 0, 0}
  160. };
  161. if (argc < 1)
  162. return ERROR;
  163. while (1) {
  164. c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:", longopts, &option);
  165. if (c == -1 || c == EOF)
  166. break;
  167. switch (c) {
  168. case 'H': /* hostname */
  169. if (is_host (optarg)) {
  170. db_host = optarg;
  171. }
  172. else {
  173. usage2 (_("Invalid hostname/address"), optarg);
  174. }
  175. break;
  176. case 's': /* socket */
  177. db_socket = optarg;
  178. break;
  179. case 'd': /* database */
  180. db = optarg;
  181. break;
  182. case 'u': /* username */
  183. db_user = optarg;
  184. break;
  185. case 'p': /* authentication information: password */
  186. db_pass = strdup(optarg);
  187. /* Delete the password from process list */
  188. while (*optarg != '\0') {
  189. *optarg = 'X';
  190. optarg++;
  191. }
  192. break;
  193. case 'P': /* critical time threshold */
  194. db_port = atoi (optarg);
  195. break;
  196. case 'v':
  197. verbose++;
  198. break;
  199. case 'V': /* version */
  200. print_revision (progname, NP_VERSION);
  201. exit (STATE_OK);
  202. case 'h': /* help */
  203. print_help ();
  204. exit (STATE_OK);
  205. case 'q':
  206. xasprintf(&sql_query, "%s", optarg);
  207. break;
  208. case 'w':
  209. warning = optarg;
  210. break;
  211. case 'c':
  212. critical = optarg;
  213. break;
  214. case '?': /* help */
  215. usage5 ();
  216. }
  217. }
  218. c = optind;
  219. set_thresholds(&my_thresholds, warning, critical);
  220. return validate_arguments ();
  221. }
  222. int
  223. validate_arguments (void)
  224. {
  225. if (sql_query == NULL)
  226. usage("Must specify a SQL query to run");
  227. if (db_user == NULL)
  228. db_user = strdup("");
  229. if (db_host == NULL)
  230. db_host = strdup("");
  231. if (db == NULL)
  232. db = strdup("");
  233. return OK;
  234. }
  235. void
  236. print_help (void)
  237. {
  238. char *myport;
  239. xasprintf (&myport, "%d", MYSQL_PORT);
  240. print_revision (progname, NP_VERSION);
  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. printf ("\n");
  265. printf ("%s\n", _("Notes:"));
  266. printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
  267. printf (" %s\n", _("overriding any my.cnf settings."));
  268. printf (UT_SUPPORT);
  269. }
  270. void
  271. print_usage (void)
  272. {
  273. printf ("%s\n", _("Usage:"));
  274. printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
  275. printf (" [-d database] [-u user] [-p password]\n");
  276. }