check_mysql.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*****************************************************************************
  2. *
  3. * Nagios check_mysql plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999 Didi Rieder (adrieder@sbox.tu-graz.ac.at)
  7. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  8. * Copyright (c) 1999-2007 Nagios Plugins Development Team
  9. *
  10. * Last Modified: $Date$
  11. *
  12. * Description:
  13. *
  14. * This file contains the check_mysql plugin
  15. *
  16. * This program tests connections to a mysql server
  17. *
  18. *
  19. * This program is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation, either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. *
  32. * $Id$
  33. *
  34. *****************************************************************************/
  35. const char *progname = "check_mysql";
  36. const char *revision = "$Revision$";
  37. const char *copyright = "1999-2007";
  38. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  39. #define SLAVERESULTSIZE 70
  40. #include "common.h"
  41. #include "utils.h"
  42. #include "utils_base.h"
  43. #include "netutils.h"
  44. #include <mysql.h>
  45. #include <errmsg.h>
  46. char *db_user = NULL;
  47. char *db_host = NULL;
  48. char *db_socket = NULL;
  49. char *db_pass = NULL;
  50. char *db = NULL;
  51. unsigned int db_port = MYSQL_PORT;
  52. int check_slave = 0, warn_sec = 0, crit_sec = 0;
  53. int verbose = 0;
  54. thresholds *my_threshold = NULL;
  55. int process_arguments (int, char **);
  56. int validate_arguments (void);
  57. void print_help (void);
  58. void print_usage (void);
  59. int
  60. main (int argc, char **argv)
  61. {
  62. MYSQL mysql;
  63. MYSQL_RES *res;
  64. MYSQL_ROW row;
  65. /* should be status */
  66. char *result = NULL;
  67. char *error = NULL;
  68. char slaveresult[SLAVERESULTSIZE];
  69. setlocale (LC_ALL, "");
  70. bindtextdomain (PACKAGE, LOCALEDIR);
  71. textdomain (PACKAGE);
  72. /* Parse extra opts if any */
  73. argv=np_extra_opts (&argc, argv, progname);
  74. if (process_arguments (argc, argv) == ERROR)
  75. usage4 (_("Could not parse arguments"));
  76. /* initialize mysql */
  77. mysql_init (&mysql);
  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, "%s\n", mysql_error (&mysql));
  83. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  84. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  85. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  86. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  87. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  88. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  89. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  90. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  91. else
  92. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  93. }
  94. /* get the server stats */
  95. result = strdup (mysql_stat (&mysql));
  96. /* error checking once more */
  97. if (mysql_error (&mysql)) {
  98. if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
  99. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  100. else if (mysql_errno (&mysql) == CR_SERVER_LOST)
  101. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  102. else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
  103. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  104. }
  105. if(check_slave) {
  106. /* check the slave status */
  107. if (mysql_query (&mysql, "show slave status") != 0) {
  108. error = strdup(mysql_error(&mysql));
  109. mysql_close (&mysql);
  110. die (STATE_CRITICAL, _("slave query error: %s\n"), error);
  111. }
  112. /* store the result */
  113. if ( (res = mysql_store_result (&mysql)) == NULL) {
  114. error = strdup(mysql_error(&mysql));
  115. mysql_close (&mysql);
  116. die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
  117. }
  118. /* Check there is some data */
  119. if (mysql_num_rows(res) == 0) {
  120. mysql_close(&mysql);
  121. die (STATE_WARNING, "%s\n", _("No slaves defined"));
  122. }
  123. /* fetch the first row */
  124. if ( (row = mysql_fetch_row (res)) == NULL) {
  125. error = strdup(mysql_error(&mysql));
  126. mysql_free_result (res);
  127. mysql_close (&mysql);
  128. die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
  129. }
  130. if (mysql_field_count (&mysql) == 12) {
  131. /* mysql 3.23.x */
  132. snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
  133. if (strcmp (row[6], "Yes") != 0) {
  134. mysql_free_result (res);
  135. mysql_close (&mysql);
  136. die (STATE_CRITICAL, "%s\n", slaveresult);
  137. }
  138. } else {
  139. /* mysql 4.x.x */
  140. int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
  141. MYSQL_FIELD* fields;
  142. num_fields = mysql_num_fields(res);
  143. fields = mysql_fetch_fields(res);
  144. for(i = 0; i < num_fields; i++) {
  145. if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
  146. slave_io_field = i;
  147. continue;
  148. }
  149. if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
  150. slave_sql_field = i;
  151. continue;
  152. }
  153. if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
  154. seconds_behind_field = i;
  155. continue;
  156. }
  157. }
  158. if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
  159. mysql_free_result (res);
  160. mysql_close (&mysql);
  161. die (STATE_CRITICAL, "Slave status unavailable\n");
  162. }
  163. snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], row[seconds_behind_field]);
  164. if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
  165. mysql_free_result (res);
  166. mysql_close (&mysql);
  167. die (STATE_CRITICAL, "%s\n", slaveresult);
  168. }
  169. if (verbose >=3) {
  170. if (seconds_behind_field == -1) {
  171. printf("seconds_behind_field not found\n");
  172. } else {
  173. printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
  174. }
  175. }
  176. if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
  177. double value = atof(row[seconds_behind_field]);
  178. int status;
  179. status = get_status(value, my_threshold);
  180. if (status == STATE_WARNING) {
  181. printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
  182. exit(STATE_WARNING);
  183. } else if (status == STATE_CRITICAL) {
  184. printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
  185. exit(STATE_CRITICAL);
  186. }
  187. }
  188. }
  189. /* free the result */
  190. mysql_free_result (res);
  191. }
  192. /* close the connection */
  193. mysql_close (&mysql);
  194. /* print out the result of stats */
  195. if (check_slave) {
  196. printf ("%s %s\n", result, slaveresult);
  197. } else {
  198. printf ("%s\n", result);
  199. }
  200. return STATE_OK;
  201. }
  202. /* process command-line arguments */
  203. int
  204. process_arguments (int argc, char **argv)
  205. {
  206. int c;
  207. char *warning = NULL;
  208. char *critical = NULL;
  209. int option = 0;
  210. static struct option longopts[] = {
  211. {"hostname", required_argument, 0, 'H'},
  212. {"socket", required_argument, 0, 's'},
  213. {"database", required_argument, 0, 'd'},
  214. {"username", required_argument, 0, 'u'},
  215. {"password", required_argument, 0, 'p'},
  216. {"port", required_argument, 0, 'P'},
  217. {"critical", required_argument, 0, 'c'},
  218. {"warning", required_argument, 0, 'w'},
  219. {"check-slave", no_argument, 0, 'S'},
  220. {"verbose", no_argument, 0, 'v'},
  221. {"version", no_argument, 0, 'V'},
  222. {"help", no_argument, 0, 'h'},
  223. {0, 0, 0, 0}
  224. };
  225. if (argc < 1)
  226. return ERROR;
  227. while (1) {
  228. c = getopt_long (argc, argv, "hvVSP:p:u:d:H:s:c:w:", longopts, &option);
  229. if (c == -1 || c == EOF)
  230. break;
  231. switch (c) {
  232. case 'H': /* hostname */
  233. if (is_host (optarg)) {
  234. db_host = optarg;
  235. }
  236. else {
  237. usage2 (_("Invalid hostname/address"), optarg);
  238. }
  239. break;
  240. case 's': /* socket */
  241. db_socket = optarg;
  242. break;
  243. case 'd': /* database */
  244. db = optarg;
  245. break;
  246. case 'u': /* username */
  247. db_user = optarg;
  248. break;
  249. case 'p': /* authentication information: password */
  250. db_pass = strdup(optarg);
  251. /* Delete the password from process list */
  252. while (*optarg != '\0') {
  253. *optarg = 'X';
  254. optarg++;
  255. }
  256. break;
  257. case 'P': /* critical time threshold */
  258. db_port = atoi (optarg);
  259. break;
  260. case 'S':
  261. check_slave = 1; /* check-slave */
  262. break;
  263. case 'w':
  264. warning = optarg;
  265. break;
  266. case 'c':
  267. critical = optarg;
  268. break;
  269. case 'V': /* version */
  270. print_revision (progname, revision);
  271. exit (STATE_OK);
  272. case 'h': /* help */
  273. print_help ();
  274. exit (STATE_OK);
  275. case 'v':
  276. verbose++;
  277. break;
  278. case '?': /* help */
  279. usage5 ();
  280. }
  281. }
  282. c = optind;
  283. set_thresholds(&my_threshold, warning, critical);
  284. while ( argc > c ) {
  285. if (db_host == NULL)
  286. if (is_host (argv[c])) {
  287. db_host = argv[c++];
  288. }
  289. else {
  290. usage2 (_("Invalid hostname/address"), argv[c]);
  291. }
  292. else if (db_user == NULL)
  293. db_user = argv[c++];
  294. else if (db_pass == NULL)
  295. db_pass = argv[c++];
  296. else if (db == NULL)
  297. db = argv[c++];
  298. else if (is_intnonneg (argv[c]))
  299. db_port = atoi (argv[c++]);
  300. else
  301. break;
  302. }
  303. return validate_arguments ();
  304. }
  305. int
  306. validate_arguments (void)
  307. {
  308. if (db_user == NULL)
  309. db_user = strdup("");
  310. if (db_host == NULL)
  311. db_host = strdup("");
  312. if (db_pass == NULL)
  313. db_pass == strdup("");
  314. if (db == NULL)
  315. db = strdup("");
  316. return OK;
  317. }
  318. void
  319. print_help (void)
  320. {
  321. char *myport;
  322. asprintf (&myport, "%d", MYSQL_PORT);
  323. print_revision (progname, revision);
  324. printf (_(COPYRIGHT), copyright, email);
  325. printf ("%s\n", _("This program tests connections to a mysql server"));
  326. printf ("\n\n");
  327. print_usage ();
  328. printf (_(UT_HELP_VRSN));
  329. printf (_(UT_EXTRA_OPTS));
  330. printf (_(UT_HOST_PORT), 'P', myport);
  331. printf (" %s\n", "-s, --socket=STRING");
  332. printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
  333. printf (" %s\n", "-d, --database=STRING");
  334. printf (" %s\n", _("Check database with indicated name"));
  335. printf (" %s\n", "-u, --username=STRING");
  336. printf (" %s\n", _("Connect using the indicated username"));
  337. printf (" %s\n", "-p, --password=STRING");
  338. printf (" %s\n", _("Use the indicated password to authenticate the connection"));
  339. printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
  340. printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
  341. printf (" %s\n", "-S, --check-slave");
  342. printf (" %s\n", _("Check if the slave thread is running properly."));
  343. printf (" %s\n", "-w, --warning");
  344. printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
  345. printf (" %s\n", _("behind master"));
  346. printf (" %s\n", "-c, --critical");
  347. printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
  348. printf (" %s\n", _("behind master"));
  349. printf ("\n");
  350. printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
  351. printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
  352. printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
  353. #ifdef NP_EXTRA_OPTS
  354. printf ("\n");
  355. printf ("%s\n", _("Notes:"));
  356. printf (_(UT_EXTRA_OPTS_NOTES));
  357. #endif
  358. printf (_(UT_SUPPORT));
  359. }
  360. void
  361. print_usage (void)
  362. {
  363. printf (_("Usage:"));
  364. printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
  365. printf (" [-u user] [-p password] [-S]\n");
  366. }