check_mysql_query.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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"), sql_query, value);
  129. printf("\n");
  130. return status;
  131. }
  132. /* process command-line arguments */
  133. int
  134. process_arguments (int argc, char **argv)
  135. {
  136. int c;
  137. char *warning = NULL;
  138. char *critical = NULL;
  139. int option = 0;
  140. static struct option longopts[] = {
  141. {"hostname", required_argument, 0, 'H'},
  142. {"socket", required_argument, 0, 's'},
  143. {"database", required_argument, 0, 'd'},
  144. {"username", required_argument, 0, 'u'},
  145. {"password", required_argument, 0, 'p'},
  146. {"port", required_argument, 0, 'P'},
  147. {"verbose", no_argument, 0, 'v'},
  148. {"version", no_argument, 0, 'V'},
  149. {"help", no_argument, 0, 'h'},
  150. {"query", required_argument, 0, 'q'},
  151. {"warning", required_argument, 0, 'w'},
  152. {"critical", required_argument, 0, 'c'},
  153. {0, 0, 0, 0}
  154. };
  155. if (argc < 1)
  156. return ERROR;
  157. while (1) {
  158. c = getopt_long (argc, argv, "hvVP:p:u:d:H:s:q:w:c:", longopts, &option);
  159. if (c == -1 || c == EOF)
  160. break;
  161. switch (c) {
  162. case 'H': /* hostname */
  163. if (is_host (optarg)) {
  164. db_host = optarg;
  165. }
  166. else {
  167. usage2 (_("Invalid hostname/address"), optarg);
  168. }
  169. break;
  170. case 's': /* socket */
  171. db_socket = optarg;
  172. break;
  173. case 'd': /* database */
  174. db = optarg;
  175. break;
  176. case 'u': /* username */
  177. db_user = optarg;
  178. break;
  179. case 'p': /* authentication information: password */
  180. db_pass = strdup(optarg);
  181. /* Delete the password from process list */
  182. while (*optarg != '\0') {
  183. *optarg = 'X';
  184. optarg++;
  185. }
  186. break;
  187. case 'P': /* critical time threshold */
  188. db_port = atoi (optarg);
  189. break;
  190. case 'v':
  191. verbose++;
  192. break;
  193. case 'V': /* version */
  194. print_revision (progname, NP_VERSION);
  195. exit (STATE_OK);
  196. case 'h': /* help */
  197. print_help ();
  198. exit (STATE_OK);
  199. case 'q':
  200. xasprintf(&sql_query, "%s", optarg);
  201. break;
  202. case 'w':
  203. warning = optarg;
  204. break;
  205. case 'c':
  206. critical = optarg;
  207. break;
  208. case '?': /* help */
  209. usage5 ();
  210. }
  211. }
  212. c = optind;
  213. set_thresholds(&my_thresholds, warning, critical);
  214. return validate_arguments ();
  215. }
  216. int
  217. validate_arguments (void)
  218. {
  219. if (sql_query == NULL)
  220. usage("Must specify a SQL query to run");
  221. if (db_user == NULL)
  222. db_user = strdup("");
  223. if (db_host == NULL)
  224. db_host = strdup("");
  225. if (db == NULL)
  226. db = strdup("");
  227. return OK;
  228. }
  229. void
  230. print_help (void)
  231. {
  232. char *myport;
  233. xasprintf (&myport, "%d", MYSQL_PORT);
  234. print_revision (progname, NP_VERSION);
  235. printf (_(COPYRIGHT), copyright, email);
  236. printf ("%s\n", _("This program checks a query result against threshold levels"));
  237. printf ("\n\n");
  238. print_usage ();
  239. printf (UT_HELP_VRSN);
  240. printf (UT_EXTRA_OPTS);
  241. printf (" -q, --query=STRING\n");
  242. printf (" %s\n", _("SQL query to run. Only first column in first row will be read"));
  243. printf (UT_WARN_CRIT_RANGE);
  244. printf (UT_HOST_PORT, 'P', myport);
  245. printf (" %s\n", "-s, --socket=STRING");
  246. printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
  247. printf (" -d, --database=STRING\n");
  248. printf (" %s\n", _("Database to check"));
  249. printf (" -u, --username=STRING\n");
  250. printf (" %s\n", _("Username to login with"));
  251. printf (" -p, --password=STRING\n");
  252. printf (" %s\n", _("Password to login with"));
  253. printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
  254. printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
  255. printf ("\n");
  256. printf (" %s\n", _("A query is required. The result from the query should be numeric."));
  257. printf (" %s\n", _("For extra security, create a user with minimal access."));
  258. printf ("\n");
  259. printf ("%s\n", _("Notes:"));
  260. printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
  261. printf (" %s\n", _("overriding any my.cnf settings."));
  262. printf (UT_SUPPORT);
  263. }
  264. void
  265. print_usage (void)
  266. {
  267. printf ("%s\n", _("Usage:"));
  268. printf (" %s -q SQL_query [-w warn] [-c crit] [-H host] [-P port] [-s socket]\n",progname);
  269. printf (" [-d database] [-u user] [-p password]\n");
  270. }