check_mysql_query.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mysql_query plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2006-2014 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-2014";
  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_char_set = NULL;
  45. char *db = NULL;
  46. char *opt_file = NULL;
  47. char *opt_group = 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. /* Parse extra opts if any */
  69. argv=np_extra_opts (&argc, argv, progname);
  70. if (process_arguments (argc, argv) == ERROR)
  71. usage4 (_("Could not parse arguments"));
  72. /* initialize mysql */
  73. mysql_init (&mysql);
  74. if (opt_file != NULL)
  75. mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
  76. if (opt_group != NULL)
  77. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
  78. else
  79. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
  80. /* establish a connection to the server and error checking */
  81. if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
  82. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
  83. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  84. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  85. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  86. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  87. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  88. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  89. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  90. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  91. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), mysql_error (&mysql));
  92. else
  93. die (STATE_CRITICAL, "QUERY %s: %s\n", _("CRITICAL"), mysql_error (&mysql));
  94. }
  95. if (db_char_set != NULL && mysql_set_character_set(&mysql, db_char_set)) { // mysql_set_character_set() returns nonzero on error
  96. error = strdup(mysql_error(&mysql));
  97. mysql_close(&mysql);
  98. die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Could not set character set"), error);
  99. }
  100. if (mysql_query (&mysql, sql_query) != 0) {
  101. error = strdup(mysql_error(&mysql));
  102. mysql_close (&mysql);
  103. die (STATE_CRITICAL, "QUERY %s: %s - %s\n", _("CRITICAL"), _("Error with query"), error);
  104. }
  105. /* store the result */
  106. if ( (res = mysql_store_result (&mysql)) == NULL) {
  107. error = strdup(mysql_error(&mysql));
  108. mysql_close (&mysql);
  109. die (STATE_CRITICAL, "QUERY %s: Error with store_result - %s\n", _("CRITICAL"), error);
  110. }
  111. /* Check there is some data */
  112. if (mysql_num_rows(res) == 0) {
  113. mysql_close(&mysql);
  114. die (STATE_WARNING, "QUERY %s: %s\n", _("WARNING"), _("No rows returned"));
  115. }
  116. /* fetch the first row */
  117. if ( (row = mysql_fetch_row (res)) == NULL) {
  118. error = strdup(mysql_error(&mysql));
  119. mysql_free_result (res);
  120. mysql_close (&mysql);
  121. die (STATE_CRITICAL, "QUERY %s: Fetch row error - %s\n", _("CRITICAL"), error);
  122. }
  123. /* free the result */
  124. mysql_free_result (res);
  125. /* close the connection */
  126. mysql_close (&mysql);
  127. if (! is_numeric(row[0])) {
  128. die (STATE_CRITICAL, "QUERY %s: %s - '%s'\n", _("CRITICAL"), _("Is not a numeric"), row[0]);
  129. }
  130. value = strtod(row[0], NULL);
  131. if (verbose >= 3)
  132. printf("mysql result: %f\n", value);
  133. status = get_status(value, my_thresholds);
  134. if (status == STATE_OK) {
  135. printf("QUERY %s: ", _("OK"));
  136. } else if (status == STATE_WARNING) {
  137. printf("QUERY %s: ", _("WARNING"));
  138. } else if (status == STATE_CRITICAL) {
  139. printf("QUERY %s: ", _("CRITICAL"));
  140. }
  141. printf(_("'%s' returned %f | %s"), sql_query, value,
  142. fperfdata("result", value, "",
  143. my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
  144. my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
  145. FALSE, 0,
  146. FALSE, 0)
  147. );
  148. printf("\n");
  149. return status;
  150. }
  151. /* process command-line arguments */
  152. int
  153. process_arguments (int argc, char **argv)
  154. {
  155. int c;
  156. char *warning = NULL;
  157. char *critical = NULL;
  158. int option = 0;
  159. static struct option longopts[] = {
  160. {"hostname", required_argument, 0, 'H'},
  161. {"socket", required_argument, 0, 's'},
  162. {"database", required_argument, 0, 'd'},
  163. {"username", required_argument, 0, 'u'},
  164. {"password", required_argument, 0, 'p'},
  165. {"character-set", required_argument, 0, 'a'},
  166. {"file", required_argument, 0, 'f'},
  167. {"group", required_argument, 0, 'g'},
  168. {"port", required_argument, 0, 'P'},
  169. {"verbose", no_argument, 0, 'v'},
  170. {"version", no_argument, 0, 'V'},
  171. {"help", no_argument, 0, 'h'},
  172. {"query", required_argument, 0, 'q'},
  173. {"warning", required_argument, 0, 'w'},
  174. {"critical", required_argument, 0, 'c'},
  175. {0, 0, 0, 0}
  176. };
  177. if (argc < 1)
  178. return ERROR;
  179. while (1) {
  180. c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:f:g:a:", longopts, &option);
  181. if (c == -1 || c == EOF)
  182. break;
  183. switch (c) {
  184. case 'a': /* character-set */
  185. db_char_set = optarg;
  186. break;
  187. case 'H': /* hostname */
  188. if (is_host (optarg)) {
  189. db_host = optarg;
  190. }
  191. else {
  192. usage2 (_("Invalid hostname/address"), optarg);
  193. }
  194. break;
  195. case 's': /* socket */
  196. db_socket = optarg;
  197. break;
  198. case 'd': /* database */
  199. db = optarg;
  200. break;
  201. case 'u': /* username */
  202. db_user = optarg;
  203. break;
  204. case 'p': /* authentication information: password */
  205. db_pass = strdup(optarg);
  206. /* Delete the password from process list */
  207. while (*optarg != '\0') {
  208. *optarg = 'X';
  209. optarg++;
  210. }
  211. break;
  212. case 'f': /* client options file */
  213. opt_file = optarg;
  214. break;
  215. case 'g': /* client options group */
  216. opt_group = optarg;
  217. break;
  218. case 'P': /* critical time threshold */
  219. db_port = atoi (optarg);
  220. break;
  221. case 'v':
  222. verbose++;
  223. break;
  224. case 'V': /* version */
  225. print_revision (progname, NP_VERSION);
  226. exit (STATE_OK);
  227. case 'h': /* help */
  228. print_help ();
  229. exit (STATE_OK);
  230. case 'q':
  231. xasprintf(&sql_query, "%s", optarg);
  232. break;
  233. case 'w':
  234. warning = optarg;
  235. break;
  236. case 'c':
  237. critical = optarg;
  238. break;
  239. case '?': /* help */
  240. usage5 ();
  241. }
  242. }
  243. c = optind;
  244. set_thresholds(&my_thresholds, warning, critical);
  245. return validate_arguments ();
  246. }
  247. int
  248. validate_arguments (void)
  249. {
  250. if (sql_query == NULL)
  251. usage("Must specify a SQL query to run");
  252. if (db_user == NULL)
  253. db_user = strdup("");
  254. if (db_host == NULL)
  255. db_host = strdup("");
  256. if (db == NULL)
  257. db = strdup("");
  258. return OK;
  259. }
  260. void
  261. print_help (void)
  262. {
  263. char *myport;
  264. xasprintf (&myport, "%d", MYSQL_PORT);
  265. print_revision (progname, NP_VERSION);
  266. printf (_(COPYRIGHT), copyright, email);
  267. printf ("%s\n", _("This program checks a query result against threshold levels"));
  268. printf ("\n\n");
  269. print_usage ();
  270. printf (UT_HELP_VRSN);
  271. printf (UT_EXTRA_OPTS);
  272. printf (" -q, --query=STRING\n");
  273. printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
  274. printf (UT_WARN_CRIT_RANGE);
  275. printf (UT_HOST_PORT, 'P', myport);
  276. printf (" %s\n", "-s, --socket=STRING");
  277. printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
  278. printf (" -d, --database=STRING\n");
  279. printf (" %s\n", _("Database to check"));
  280. printf (" -a, --character-set=STRING\n");
  281. printf (" %s\n", _("Use a specific character set when querying, e.g. latin1 or utf8"));
  282. printf (" %s\n", "-f, --file=STRING");
  283. printf (" %s\n", _("Read from the specified client options file"));
  284. printf (" %s\n", "-g, --group=STRING");
  285. printf (" %s\n", _("Use a client options group"));
  286. printf (" -u, --username=STRING\n");
  287. printf (" %s\n", _("Username to login with"));
  288. printf (" -p, --password=STRING\n");
  289. printf (" %s\n", _("Password to login with"));
  290. printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
  291. printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
  292. printf ("\n");
  293. printf (" %s\n", _("A query is required. The result from the query should be numeric."));
  294. printf (" %s\n", _("For extra security, create a user with minimal access."));
  295. printf ("\n");
  296. printf ("%s\n", _("Notes:"));
  297. printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
  298. printf (" %s\n", _("overriding any my.cnf settings."));
  299. printf (UT_SUPPORT);
  300. }
  301. void
  302. print_usage (void)
  303. {
  304. printf ("%s\n", _("Usage:"));
  305. printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
  306. printf (" [-d database] [-u user] [-p password] [-f optfile] [-g group] [-a character-set]\n");
  307. }