check_time.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. const char *progname = "check_time";
  41. #define REVISION "$Revision$"
  42. #define COPYRIGHT "1999-2002"
  43. #define AUTHOR "Ethan Galstad"
  44. #define EMAIL "nagios@nagios.org"
  45. #define SUMMARY "Check time on the specified host.\n"
  46. #define TIME_PORT 37
  47. #define UNIX_EPOCH 2208988800UL
  48. unsigned long server_time, raw_server_time;
  49. time_t diff_time;
  50. int warning_time = 0;
  51. int check_warning_time = FALSE;
  52. int critical_time = 0;
  53. int check_critical_time = FALSE;
  54. unsigned long warning_diff = 0;
  55. int check_warning_diff = FALSE;
  56. unsigned long critical_diff = 0;
  57. int check_critical_diff = FALSE;
  58. int server_port = TIME_PORT;
  59. char *server_address = NULL;
  60. int process_arguments (int, char **);
  61. void print_usage (void);
  62. void print_help (void);
  63. int
  64. main (int argc, char **argv)
  65. {
  66. int sd;
  67. int result;
  68. if (process_arguments (argc, argv) != OK)
  69. usage ("Invalid command arguments supplied\n");
  70. /* initialize alarm signal handling */
  71. signal (SIGALRM, socket_timeout_alarm_handler);
  72. /* set socket timeout */
  73. alarm (socket_timeout);
  74. time (&start_time);
  75. /* try to connect to the host at the given port number */
  76. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) {
  77. if (check_critical_time == TRUE)
  78. result = STATE_CRITICAL;
  79. else if (check_warning_time == TRUE)
  80. result = STATE_WARNING;
  81. else
  82. result = STATE_UNKNOWN;
  83. terminate (result,
  84. "TIME UNKNOWN - could not connect to server %s, port %d\n",
  85. server_address, server_port);
  86. }
  87. /* watch for the connection string */
  88. result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
  89. /* close the connection */
  90. close (sd);
  91. /* reset the alarm */
  92. time (&end_time);
  93. alarm (0);
  94. /* return a WARNING status if we couldn't read any data */
  95. if (result <= 0) {
  96. if (check_critical_time == TRUE)
  97. result = STATE_CRITICAL;
  98. else if (check_warning_time == TRUE)
  99. result = STATE_WARNING;
  100. else
  101. result = STATE_UNKNOWN;
  102. terminate (result,
  103. "TIME UNKNOWN - no data on recv() from server %s, port %d\n",
  104. server_address, server_port);
  105. }
  106. result = STATE_OK;
  107. if (check_critical_time == TRUE && (end_time - start_time) > critical_time)
  108. result = STATE_CRITICAL;
  109. else if (check_warning_time == TRUE
  110. && (end_time - start_time) > warning_time) result = STATE_WARNING;
  111. if (result != STATE_OK)
  112. terminate (result, "TIME %s - %d second response time\n",
  113. state_text (result), (int) (end_time - start_time));
  114. server_time = ntohl (raw_server_time) - UNIX_EPOCH;
  115. if (server_time > end_time)
  116. diff_time = server_time - end_time;
  117. else
  118. diff_time = end_time - server_time;
  119. if (check_critical_diff == TRUE && diff_time > critical_diff)
  120. result = STATE_CRITICAL;
  121. else if (check_warning_diff == TRUE && diff_time > warning_diff)
  122. result = STATE_WARNING;
  123. printf ("TIME %s - %lu second time difference\n", state_text (result),
  124. diff_time);
  125. return result;
  126. }
  127. /* process command-line arguments */
  128. int
  129. process_arguments (int argc, char **argv)
  130. {
  131. int c;
  132. #ifdef HAVE_GETOPT_H
  133. int option_index = 0;
  134. static struct option long_options[] = {
  135. {"hostname", required_argument, 0, 'H'},
  136. {"warning-variance", required_argument, 0, 'w'},
  137. {"critical-variance", required_argument, 0, 'c'},
  138. {"warning-connect", required_argument, 0, 'W'},
  139. {"critical-connect", required_argument, 0, 'C'},
  140. {"port", required_argument, 0, 'p'},
  141. {"timeout", required_argument, 0, 't'},
  142. {"version", no_argument, 0, 'V'},
  143. {"help", no_argument, 0, 'h'},
  144. {0, 0, 0, 0}
  145. };
  146. #endif
  147. if (argc < 2)
  148. usage ("\n");
  149. for (c = 1; c < argc; c++) {
  150. if (strcmp ("-to", argv[c]) == 0)
  151. strcpy (argv[c], "-t");
  152. else if (strcmp ("-wd", argv[c]) == 0)
  153. strcpy (argv[c], "-w");
  154. else if (strcmp ("-cd", argv[c]) == 0)
  155. strcpy (argv[c], "-c");
  156. else if (strcmp ("-wt", argv[c]) == 0)
  157. strcpy (argv[c], "-W");
  158. else if (strcmp ("-ct", argv[c]) == 0)
  159. strcpy (argv[c], "-C");
  160. }
  161. while (1) {
  162. #ifdef HAVE_GETOPT_H
  163. c =
  164. getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
  165. &option_index);
  166. #else
  167. c = getopt (argc, argv, "hVH:w:c:W:C:p:t:");
  168. #endif
  169. if (c == -1 || c == EOF)
  170. break;
  171. switch (c) {
  172. case '?': /* print short usage statement if args not parsable */
  173. usage3 ("Unknown argument", optopt);
  174. case 'h': /* help */
  175. print_help ();
  176. exit (STATE_OK);
  177. case 'V': /* version */
  178. print_revision (progname, REVISION);
  179. exit (STATE_OK);
  180. case 'H': /* hostname */
  181. if (is_host (optarg) == FALSE)
  182. usage ("Invalid host name/address\n");
  183. server_address = optarg;
  184. break;
  185. case 'w': /* warning-variance */
  186. if (is_intnonneg (optarg)) {
  187. warning_diff = strtoul (optarg, NULL, 10);
  188. check_warning_diff = TRUE;
  189. }
  190. else if (strspn (optarg, "0123456789:,") > 0) {
  191. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  192. check_warning_diff = TRUE;
  193. check_warning_time = TRUE;
  194. }
  195. else {
  196. usage ("Warning thresholds must be a nonnegative integer\n");
  197. }
  198. }
  199. else {
  200. usage ("Warning threshold must be a nonnegative integer\n");
  201. }
  202. break;
  203. case 'c': /* critical-variance */
  204. if (is_intnonneg (optarg)) {
  205. critical_diff = strtoul (optarg, NULL, 10);
  206. check_critical_diff = TRUE;
  207. }
  208. else if (strspn (optarg, "0123456789:,") > 0) {
  209. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  210. 2) {
  211. check_critical_diff = TRUE;
  212. check_critical_time = TRUE;
  213. }
  214. else {
  215. usage ("Critical thresholds must be a nonnegative integer\n");
  216. }
  217. }
  218. else {
  219. usage ("Critical threshold must be a nonnegative integer\n");
  220. }
  221. break;
  222. case 'W': /* warning-connect */
  223. if (!is_intnonneg (optarg))
  224. usage ("Warning threshold must be a nonnegative integer\n");
  225. warning_time = atoi (optarg);
  226. check_warning_time = TRUE;
  227. break;
  228. case 'C': /* critical-connect */
  229. if (!is_intnonneg (optarg))
  230. usage ("Critical threshold must be a nonnegative integer\n");
  231. critical_time = atoi (optarg);
  232. check_critical_time = TRUE;
  233. break;
  234. case 'p': /* port */
  235. if (!is_intnonneg (optarg))
  236. usage ("Serevr port must be a nonnegative integer\n");
  237. server_port = atoi (optarg);
  238. break;
  239. case 't': /* timeout */
  240. if (!is_intnonneg (optarg))
  241. usage ("Timeout interval must be a nonnegative integer\n");
  242. socket_timeout = atoi (optarg);
  243. break;
  244. }
  245. }
  246. c = optind;
  247. if (server_address == NULL) {
  248. if (argc > c) {
  249. if (is_host (argv[c]) == FALSE)
  250. usage ("Invalid host name/address\n");
  251. server_address = argv[c];
  252. }
  253. else {
  254. usage ("Host name was not supplied\n");
  255. }
  256. }
  257. return OK;
  258. }
  259. void
  260. print_usage (void)
  261. {
  262. printf
  263. ("Usage:\n"
  264. " %s -H <host_address> [-p port] [-w variance] [-c variance]\n"
  265. " [-W connect_time] [-C connect_time] [-t timeout]\n"
  266. " %s (-h | --help) for detailed help\n"
  267. " %s (-V | --version) for version information\n",
  268. progname, progname, progname);
  269. }
  270. void
  271. print_help (void)
  272. {
  273. print_revision (progname, REVISION);
  274. printf
  275. ("Copyright (c) %s %s <%s>\n\n%s\n",
  276. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  277. print_usage ();
  278. printf
  279. ("Options:\n"
  280. " -H, --hostname=ADDRESS\n"
  281. " Host name argument for servers using host headers (use numeric\n"
  282. " address if possible to bypass DNS lookup).\n"
  283. " -w, --warning-variance=INTEGER\n"
  284. " Time difference (sec.) necessary to result in a warning status\n"
  285. " -c, --critical-variance=INTEGER\n"
  286. " Time difference (sec.) necessary to result in a critical status\n"
  287. " -W, --warning-connect=INTEGER\n"
  288. " Response time (sec.) necessary to result in warning status\n"
  289. " -C, --critical-connect=INTEGER\n"
  290. " Response time (sec.) necessary to result in critical status\n"
  291. " -t, --timeout=INTEGER\n"
  292. " Seconds before connection times out (default: %d)\n"
  293. " -p, --port=INTEGER\n"
  294. " Port number (default: %d)\n"
  295. " -h, --help\n"
  296. " Print detailed help screen\n"
  297. " -V, --version\n"
  298. " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT, TIME_PORT);
  299. support ();
  300. }