check_mysql.c 11 KB

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