check_mysql.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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-2009 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. char *opt_file = NULL;
  48. char *opt_group = NULL;
  49. unsigned int db_port = MYSQL_PORT;
  50. int check_slave = 0, warn_sec = 0, crit_sec = 0;
  51. int verbose = 0;
  52. thresholds *my_threshold = NULL;
  53. int process_arguments (int, char **);
  54. int validate_arguments (void);
  55. void print_help (void);
  56. void print_usage (void);
  57. int
  58. main (int argc, char **argv)
  59. {
  60. MYSQL mysql;
  61. MYSQL_RES *res;
  62. MYSQL_ROW row;
  63. /* should be status */
  64. char *result = NULL;
  65. char *error = NULL;
  66. char slaveresult[SLAVERESULTSIZE];
  67. setlocale (LC_ALL, "");
  68. bindtextdomain (PACKAGE, LOCALEDIR);
  69. textdomain (PACKAGE);
  70. /* Parse extra opts if any */
  71. argv=np_extra_opts (&argc, argv, progname);
  72. if (process_arguments (argc, argv) == ERROR)
  73. usage4 (_("Could not parse arguments"));
  74. /* initialize mysql */
  75. mysql_init (&mysql);
  76. if (opt_file != NULL)
  77. mysql_options(&mysql,MYSQL_READ_DEFAULT_FILE,opt_file);
  78. if (opt_group != NULL)
  79. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,opt_group);
  80. else
  81. mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"client");
  82. /* establish a connection to the server and error checking */
  83. if (!mysql_real_connect(&mysql,db_host,db_user,db_pass,db,db_port,db_socket,0)) {
  84. if (mysql_errno (&mysql) == CR_UNKNOWN_HOST)
  85. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  86. else if (mysql_errno (&mysql) == CR_VERSION_ERROR)
  87. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  88. else if (mysql_errno (&mysql) == CR_OUT_OF_MEMORY)
  89. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  90. else if (mysql_errno (&mysql) == CR_IPSOCK_ERROR)
  91. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  92. else if (mysql_errno (&mysql) == CR_SOCKET_CREATE_ERROR)
  93. die (STATE_WARNING, "%s\n", mysql_error (&mysql));
  94. else
  95. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  96. }
  97. /* get the server stats */
  98. result = strdup (mysql_stat (&mysql));
  99. /* error checking once more */
  100. if (mysql_error (&mysql)) {
  101. if (mysql_errno (&mysql) == CR_SERVER_GONE_ERROR)
  102. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  103. else if (mysql_errno (&mysql) == CR_SERVER_LOST)
  104. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  105. else if (mysql_errno (&mysql) == CR_UNKNOWN_ERROR)
  106. die (STATE_CRITICAL, "%s\n", mysql_error (&mysql));
  107. }
  108. if(check_slave) {
  109. /* check the slave status */
  110. if (mysql_query (&mysql, "show slave status") != 0) {
  111. error = strdup(mysql_error(&mysql));
  112. mysql_close (&mysql);
  113. die (STATE_CRITICAL, _("slave query error: %s\n"), error);
  114. }
  115. /* store the result */
  116. if ( (res = mysql_store_result (&mysql)) == NULL) {
  117. error = strdup(mysql_error(&mysql));
  118. mysql_close (&mysql);
  119. die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
  120. }
  121. /* Check there is some data */
  122. if (mysql_num_rows(res) == 0) {
  123. mysql_close(&mysql);
  124. die (STATE_WARNING, "%s\n", _("No slaves defined"));
  125. }
  126. /* fetch the first row */
  127. if ( (row = mysql_fetch_row (res)) == NULL) {
  128. error = strdup(mysql_error(&mysql));
  129. mysql_free_result (res);
  130. mysql_close (&mysql);
  131. die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
  132. }
  133. if (mysql_field_count (&mysql) == 12) {
  134. /* mysql 3.23.x */
  135. snprintf (slaveresult, SLAVERESULTSIZE, _("Slave running: %s"), row[6]);
  136. if (strcmp (row[6], "Yes") != 0) {
  137. mysql_free_result (res);
  138. mysql_close (&mysql);
  139. die (STATE_CRITICAL, "%s\n", slaveresult);
  140. }
  141. } else {
  142. /* mysql 4.x.x */
  143. int slave_io_field = -1 , slave_sql_field = -1, seconds_behind_field = -1, i, num_fields;
  144. MYSQL_FIELD* fields;
  145. num_fields = mysql_num_fields(res);
  146. fields = mysql_fetch_fields(res);
  147. for(i = 0; i < num_fields; i++) {
  148. if (strcmp(fields[i].name, "Slave_IO_Running") == 0) {
  149. slave_io_field = i;
  150. continue;
  151. }
  152. if (strcmp(fields[i].name, "Slave_SQL_Running") == 0) {
  153. slave_sql_field = i;
  154. continue;
  155. }
  156. if (strcmp(fields[i].name, "Seconds_Behind_Master") == 0) {
  157. seconds_behind_field = i;
  158. continue;
  159. }
  160. }
  161. if ((slave_io_field < 0) || (slave_sql_field < 0) || (num_fields == 0)) {
  162. mysql_free_result (res);
  163. mysql_close (&mysql);
  164. die (STATE_CRITICAL, "Slave status unavailable\n");
  165. }
  166. snprintf (slaveresult, SLAVERESULTSIZE, "Slave IO: %s Slave SQL: %s Seconds Behind Master: %s", row[slave_io_field], row[slave_sql_field], seconds_behind_field!=-1?row[seconds_behind_field]:"Unknown");
  167. if (strcmp (row[slave_io_field], "Yes") != 0 || strcmp (row[slave_sql_field], "Yes") != 0) {
  168. mysql_free_result (res);
  169. mysql_close (&mysql);
  170. die (STATE_CRITICAL, "%s\n", slaveresult);
  171. }
  172. if (verbose >=3) {
  173. if (seconds_behind_field == -1) {
  174. printf("seconds_behind_field not found\n");
  175. } else {
  176. printf ("seconds_behind_field(index %d)=%s\n", seconds_behind_field, row[seconds_behind_field]);
  177. }
  178. }
  179. if ((seconds_behind_field != -1) && (strcmp (row[seconds_behind_field], "NULL") != 0)) {
  180. double value = atof(row[seconds_behind_field]);
  181. int status;
  182. status = get_status(value, my_threshold);
  183. if (status == STATE_WARNING) {
  184. printf("SLOW_SLAVE %s: %s\n", _("WARNING"), slaveresult);
  185. exit(STATE_WARNING);
  186. } else if (status == STATE_CRITICAL) {
  187. printf("SLOW_SLAVE %s: %s\n", _("CRITICAL"), slaveresult);
  188. exit(STATE_CRITICAL);
  189. }
  190. }
  191. }
  192. /* free the result */
  193. mysql_free_result (res);
  194. }
  195. /* close the connection */
  196. mysql_close (&mysql);
  197. /* print out the result of stats */
  198. if (check_slave) {
  199. printf ("%s %s\n", result, slaveresult);
  200. } else {
  201. printf ("%s\n", result);
  202. }
  203. return STATE_OK;
  204. }
  205. /* process command-line arguments */
  206. int
  207. process_arguments (int argc, char **argv)
  208. {
  209. int c;
  210. char *warning = NULL;
  211. char *critical = NULL;
  212. int option = 0;
  213. static struct option longopts[] = {
  214. {"hostname", required_argument, 0, 'H'},
  215. {"socket", required_argument, 0, 's'},
  216. {"database", required_argument, 0, 'd'},
  217. {"username", required_argument, 0, 'u'},
  218. {"password", required_argument, 0, 'p'},
  219. {"file", required_argument, 0, 'f'},
  220. {"group", required_argument, 0, 'g'},
  221. {"port", required_argument, 0, 'P'},
  222. {"critical", required_argument, 0, 'c'},
  223. {"warning", required_argument, 0, 'w'},
  224. {"check-slave", no_argument, 0, 'S'},
  225. {"verbose", no_argument, 0, 'v'},
  226. {"version", no_argument, 0, 'V'},
  227. {"help", no_argument, 0, 'h'},
  228. {0, 0, 0, 0}
  229. };
  230. if (argc < 1)
  231. return ERROR;
  232. while (1) {
  233. c = getopt_long (argc, argv, "hvVSP:p:u:d:f:g:H:s:c:w:", longopts, &option);
  234. if (c == -1 || c == EOF)
  235. break;
  236. switch (c) {
  237. case 'H': /* hostname */
  238. if (is_host (optarg)) {
  239. db_host = optarg;
  240. }
  241. else {
  242. usage2 (_("Invalid hostname/address"), optarg);
  243. }
  244. break;
  245. case 's': /* socket */
  246. db_socket = optarg;
  247. break;
  248. case 'd': /* database */
  249. db = optarg;
  250. break;
  251. case 'u': /* username */
  252. db_user = optarg;
  253. break;
  254. case 'p': /* authentication information: password */
  255. db_pass = strdup(optarg);
  256. /* Delete the password from process list */
  257. while (*optarg != '\0') {
  258. *optarg = 'X';
  259. optarg++;
  260. }
  261. break;
  262. case 'f': /* username */
  263. opt_file = optarg;
  264. break;
  265. case 'g': /* username */
  266. opt_group = optarg;
  267. break;
  268. case 'P': /* critical time threshold */
  269. db_port = atoi (optarg);
  270. break;
  271. case 'S':
  272. check_slave = 1; /* check-slave */
  273. break;
  274. case 'w':
  275. warning = optarg;
  276. break;
  277. case 'c':
  278. critical = optarg;
  279. break;
  280. case 'V': /* version */
  281. print_revision (progname, NP_VERSION);
  282. exit (STATE_OK);
  283. case 'h': /* help */
  284. print_help ();
  285. exit (STATE_OK);
  286. case 'v':
  287. verbose++;
  288. break;
  289. case '?': /* help */
  290. usage5 ();
  291. }
  292. }
  293. c = optind;
  294. set_thresholds(&my_threshold, warning, critical);
  295. while ( argc > c ) {
  296. if (db_host == NULL)
  297. if (is_host (argv[c])) {
  298. db_host = argv[c++];
  299. }
  300. else {
  301. usage2 (_("Invalid hostname/address"), argv[c]);
  302. }
  303. else if (db_user == NULL)
  304. db_user = argv[c++];
  305. else if (opt_file == NULL)
  306. opt_file = argv[c++];
  307. else if (opt_group == NULL)
  308. opt_group = argv[c++];
  309. else if (db_pass == NULL)
  310. db_pass = argv[c++];
  311. else if (db == NULL)
  312. db = argv[c++];
  313. else if (is_intnonneg (argv[c]))
  314. db_port = atoi (argv[c++]);
  315. else
  316. break;
  317. }
  318. return validate_arguments ();
  319. }
  320. int
  321. validate_arguments (void)
  322. {
  323. if (db_user == NULL)
  324. db_user = strdup("");
  325. if (opt_file == NULL)
  326. opt_file = strdup("");
  327. if (opt_group == NULL)
  328. opt_group = strdup("");
  329. if (db_host == NULL)
  330. db_host = strdup("");
  331. if (db == NULL)
  332. db = strdup("");
  333. return OK;
  334. }
  335. void
  336. print_help (void)
  337. {
  338. char *myport;
  339. asprintf (&myport, "%d", MYSQL_PORT);
  340. print_revision (progname, NP_VERSION);
  341. printf (_(COPYRIGHT), copyright, email);
  342. printf ("%s\n", _("This program tests connections to a MySQL server"));
  343. printf ("\n\n");
  344. print_usage ();
  345. printf (UT_HELP_VRSN);
  346. printf (UT_EXTRA_OPTS);
  347. printf (UT_HOST_PORT, 'P', myport);
  348. printf (" %s\n", "-s, --socket=STRING");
  349. printf (" %s\n", _("Use the specified socket (has no effect if -H is used)"));
  350. printf (" %s\n", "-d, --database=STRING");
  351. printf (" %s\n", _("Check database with indicated name"));
  352. printf (" %s\n", "-f, --file=STRING");
  353. printf (" %s\n", _("Read from the specified client options file"));
  354. printf (" %s\n", "-g, --group=STRING");
  355. printf (" %s\n", _("Use a client options group"));
  356. printf (" %s\n", "-u, --username=STRING");
  357. printf (" %s\n", _("Connect using the indicated username"));
  358. printf (" %s\n", "-p, --password=STRING");
  359. printf (" %s\n", _("Use the indicated password to authenticate the connection"));
  360. printf (" ==> %s <==\n", _("IMPORTANT: THIS FORM OF AUTHENTICATION IS NOT SECURE!!!"));
  361. printf (" %s\n", _("Your clear-text password could be visible as a process table entry"));
  362. printf (" %s\n", "-S, --check-slave");
  363. printf (" %s\n", _("Check if the slave thread is running properly."));
  364. printf (" %s\n", "-w, --warning");
  365. printf (" %s\n", _("Exit with WARNING status if slave server is more than INTEGER seconds"));
  366. printf (" %s\n", _("behind master"));
  367. printf (" %s\n", "-c, --critical");
  368. printf (" %s\n", _("Exit with CRITICAL status if slave server is more then INTEGER seconds"));
  369. printf (" %s\n", _("behind master"));
  370. printf ("\n");
  371. printf (" %s\n", _("There are no required arguments. By default, the local database is checked"));
  372. printf (" %s\n", _("using the default unix socket. You can force TCP on localhost by using an"));
  373. printf (" %s\n", _("IP address or FQDN ('localhost' will use the socket as well)."));
  374. printf ("\n");
  375. printf ("%s\n", _("Notes:"));
  376. printf (" %s\n", _("You must specify -p with an empty string to force an empty password,"));
  377. printf (" %s\n", _("overriding any my.cnf settings."));
  378. printf (UT_SUPPORT);
  379. }
  380. void
  381. print_usage (void)
  382. {
  383. printf ("%s\n", _("Usage:"));
  384. printf (" %s [-d database] [-H host] [-P port] [-s socket]\n",progname);
  385. printf (" [-u user] [-p password] [-S] [-f optfile]\n");
  386. printf (" [-g group]\n");
  387. }