check_time.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /******************************************************************************
  2. *
  3. * CHECK_TIME.C
  4. *
  5. * Program: Network time server plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. * Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
  9. *
  10. * $Id$
  11. *
  12. * Description:
  13. *
  14. * This plugin will attempt to connect to the specified port
  15. * on the host. Successul connects return STATE_OK, refusals
  16. * and timeouts return STATE_CRITICAL, other errors return
  17. * STATE_UNKNOWN.
  18. *
  19. * License Information:
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. *
  35. *****************************************************************************/
  36. #include "config.h"
  37. #include "common.h"
  38. #include "netutils.h"
  39. #include "utils.h"
  40. #define PROGNAME "check_time"
  41. #define TIME_PORT 37
  42. #define UNIX_EPOCH 2208988800UL
  43. unsigned long server_time, raw_server_time;
  44. time_t diff_time;
  45. int warning_time = 0;
  46. int check_warning_time = FALSE;
  47. int critical_time = 0;
  48. int check_critical_time = FALSE;
  49. unsigned long warning_diff = 0;
  50. int check_warning_diff = FALSE;
  51. unsigned long critical_diff = 0;
  52. int check_critical_diff = FALSE;
  53. int server_port = TIME_PORT;
  54. char *server_address = NULL;
  55. int process_arguments (int, char **);
  56. int call_getopt (int, char **);
  57. void print_usage (void);
  58. void print_help (void);
  59. int
  60. main (int argc, char **argv)
  61. {
  62. int sd;
  63. int result;
  64. if (process_arguments (argc, argv) != OK)
  65. usage ("Invalid command arguments supplied\n");
  66. /* initialize alarm signal handling */
  67. signal (SIGALRM, socket_timeout_alarm_handler);
  68. /* set socket timeout */
  69. alarm (socket_timeout);
  70. time (&start_time);
  71. /* try to connect to the host at the given port number */
  72. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) {
  73. if (check_critical_time == TRUE)
  74. result = STATE_CRITICAL;
  75. else if (check_warning_time == TRUE)
  76. result = STATE_WARNING;
  77. else
  78. result = STATE_UNKNOWN;
  79. terminate (result,
  80. "TIME UNKNOWN - could not connect to server %s, port %d\n",
  81. server_address, server_port);
  82. }
  83. /* watch for the FTP connection string */
  84. result = recv (sd, &raw_server_time, sizeof (raw_server_time), 0);
  85. /* close the connection */
  86. close (sd);
  87. /* reset the alarm */
  88. time (&end_time);
  89. alarm (0);
  90. /* return a WARNING status if we couldn't read any data */
  91. if (result <= 0) {
  92. if (check_critical_time == TRUE)
  93. result = STATE_CRITICAL;
  94. else if (check_warning_time == TRUE)
  95. result = STATE_WARNING;
  96. else
  97. result = STATE_UNKNOWN;
  98. terminate (result,
  99. "TIME UNKNOWN - no data on recv() from server %s, port %d\n",
  100. server_address, server_port);
  101. }
  102. result = STATE_OK;
  103. if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
  104. result = STATE_CRITICAL;
  105. else if (check_warning_time == TRUE
  106. && (end_time - start_time) > warning_time) result = STATE_WARNING;
  107. if (result != STATE_OK)
  108. terminate (result, "TIME %s - %d second response time\n",
  109. state_text (result), (int) (end_time - start_time));
  110. server_time = ntohl (raw_server_time) - UNIX_EPOCH;
  111. if (server_time > end_time)
  112. diff_time = server_time - end_time;
  113. else
  114. diff_time = end_time - server_time;
  115. if (check_critical_diff == TRUE && diff_time > critical_diff)
  116. result = STATE_CRITICAL;
  117. else if (check_warning_diff == TRUE && diff_time > warning_diff)
  118. result = STATE_WARNING;
  119. printf ("TIME %s - %lu second time difference\n", state_text (result),
  120. diff_time);
  121. return result;
  122. }
  123. /* process command-line arguments */
  124. int
  125. process_arguments (int argc, char **argv)
  126. {
  127. int c;
  128. if (argc < 2)
  129. usage ("\n");
  130. for (c = 1; c < argc; c++) {
  131. if (strcmp ("-to", argv[c]) == 0)
  132. strcpy (argv[c], "-t");
  133. else if (strcmp ("-wd", argv[c]) == 0)
  134. strcpy (argv[c], "-w");
  135. else if (strcmp ("-cd", argv[c]) == 0)
  136. strcpy (argv[c], "-c");
  137. else if (strcmp ("-wt", argv[c]) == 0)
  138. strcpy (argv[c], "-W");
  139. else if (strcmp ("-ct", argv[c]) == 0)
  140. strcpy (argv[c], "-C");
  141. }
  142. c = 0;
  143. while ((c += call_getopt (argc - c, &argv[c])) < argc) {
  144. if (is_option (argv[c]))
  145. continue;
  146. if (server_address == NULL) {
  147. if (argc > c) {
  148. if (is_host (argv[c]) == FALSE)
  149. usage ("Invalid host name/address\n");
  150. server_address = argv[c];
  151. }
  152. else {
  153. usage ("Host name was not supplied\n");
  154. }
  155. }
  156. }
  157. return OK;
  158. }
  159. int
  160. call_getopt (int argc, char **argv)
  161. {
  162. int c, i = 0;
  163. #ifdef HAVE_GETOPT_H
  164. int option_index = 0;
  165. static struct option long_options[] = {
  166. {"hostname", required_argument, 0, 'H'},
  167. {"warning-variance", required_argument, 0, 'w'},
  168. {"critical-variance", required_argument, 0, 'c'},
  169. {"warning-connect", required_argument, 0, 'W'},
  170. {"critical-connect", required_argument, 0, 'C'},
  171. {"port", required_argument, 0, 'p'},
  172. {"timeout", required_argument, 0, 't'},
  173. {"version", no_argument, 0, 'V'},
  174. {"help", no_argument, 0, 'h'},
  175. {0, 0, 0, 0}
  176. };
  177. #endif
  178. while (1) {
  179. #ifdef HAVE_GETOPT_H
  180. c =
  181. getopt_long (argc, argv, "+hVH:w:c:W:C:p:t:", long_options,
  182. &option_index);
  183. #else
  184. c = getopt (argc, argv, "+hVH:w:c:W:C:p:t:");
  185. #endif
  186. i++;
  187. if (c == -1 || c == EOF || c == 1)
  188. break;
  189. switch (c) {
  190. case 'H':
  191. case 'w':
  192. case 'c':
  193. case 'W':
  194. case 'C':
  195. case 'p':
  196. case 't':
  197. i++;
  198. }
  199. switch (c) {
  200. case '?': /* print short usage statement if args not parsable */
  201. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  202. print_usage ();
  203. exit (STATE_UNKNOWN);
  204. case 'h': /* help */
  205. print_help ();
  206. exit (STATE_OK);
  207. case 'V': /* version */
  208. print_revision (my_basename (argv[0]), "$Revision$");
  209. exit (STATE_OK);
  210. case 'H': /* hostname */
  211. if (is_host (optarg) == FALSE)
  212. usage ("Invalid host name/address\n");
  213. server_address = optarg;
  214. break;
  215. case 'w': /* warning-variance */
  216. if (is_intnonneg (optarg)) {
  217. warning_diff = strtoul (optarg, NULL, 10);
  218. check_warning_diff = TRUE;
  219. }
  220. else if (strspn (optarg, "0123456789:,") > 0) {
  221. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  222. check_warning_diff = TRUE;
  223. check_warning_time = TRUE;
  224. }
  225. else {
  226. usage ("Warning thresholds must be a nonnegative integer\n");
  227. }
  228. }
  229. else {
  230. usage ("Warning threshold must be a nonnegative integer\n");
  231. }
  232. break;
  233. case 'c': /* critical-variance */
  234. if (is_intnonneg (optarg)) {
  235. critical_diff = strtoul (optarg, NULL, 10);
  236. check_critical_diff = TRUE;
  237. }
  238. else if (strspn (optarg, "0123456789:,") > 0) {
  239. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  240. 2) {
  241. check_critical_diff = TRUE;
  242. check_critical_time = TRUE;
  243. }
  244. else {
  245. usage ("Critical thresholds must be a nonnegative integer\n");
  246. }
  247. }
  248. else {
  249. usage ("Critical threshold must be a nonnegative integer\n");
  250. }
  251. break;
  252. case 'W': /* warning-connect */
  253. if (!is_intnonneg (optarg))
  254. usage ("Warning threshold must be a nonnegative integer\n");
  255. warning_time = atoi (optarg);
  256. check_warning_time = TRUE;
  257. break;
  258. case 'C': /* critical-connect */
  259. if (!is_intnonneg (optarg))
  260. usage ("Critical threshold must be a nonnegative integer\n");
  261. critical_time = atoi (optarg);
  262. check_critical_time = TRUE;
  263. break;
  264. case 'p': /* port */
  265. if (!is_intnonneg (optarg))
  266. usage ("Serevr port must be a nonnegative integer\n");
  267. server_port = atoi (optarg);
  268. break;
  269. case 't': /* timeout */
  270. if (!is_intnonneg (optarg))
  271. usage ("Timeout interval must be a nonnegative integer\n");
  272. socket_timeout = atoi (optarg);
  273. break;
  274. }
  275. }
  276. return i;
  277. }
  278. void
  279. print_usage (void)
  280. {
  281. printf
  282. ("Usage: check_time -H <host_address> [-p port] [-w variance] [-c variance]\n"
  283. " [-W connect_time] [-C connect_time] [-t timeout]\n");
  284. }
  285. void
  286. print_help (void)
  287. {
  288. print_revision (PROGNAME, "$Revision$");
  289. printf
  290. ("Copyright (c) 2000 Ethan Galstad/Karl DeBisschop\n\n"
  291. "This plugin connects to a time port on the specified host.\n\n");
  292. print_usage ();
  293. printf
  294. ("Options:\n"
  295. " -H, --hostname=ADDRESS\n"
  296. " Host name argument for servers using host headers (use numeric\n"
  297. " address if possible to bypass DNS lookup).\n"
  298. " -w, --warning-variance=INTEGER\n"
  299. " Time difference (sec.) necessary to result in a warning status\n"
  300. " -c, --critical-variance=INTEGER\n"
  301. " Time difference (sec.) necessary to result in a critical status\n"
  302. " -W, --warning-connect=INTEGER\n"
  303. " Response time (sec.) necessary to result in warning status\n"
  304. " -C, --critical-connect=INTEGER\n"
  305. " Response time (sec.) necessary to result in critical status\n"
  306. " -t, --timeout=INTEGER\n"
  307. " Seconds before connection times out (default: %d)\n"
  308. " -p, --port=INTEGER\n"
  309. " Port number (default: %d)\n"
  310. " -h, --help\n"
  311. " Print detailed help screen\n"
  312. " -V, --version\n"
  313. " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT, TIME_PORT);
  314. support ();
  315. }