check_time.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. int option_index = 0;
  133. static struct option long_options[] = {
  134. {"hostname", required_argument, 0, 'H'},
  135. {"warning-variance", required_argument, 0, 'w'},
  136. {"critical-variance", required_argument, 0, 'c'},
  137. {"warning-connect", required_argument, 0, 'W'},
  138. {"critical-connect", required_argument, 0, 'C'},
  139. {"port", required_argument, 0, 'p'},
  140. {"timeout", required_argument, 0, 't'},
  141. {"version", no_argument, 0, 'V'},
  142. {"help", no_argument, 0, 'h'},
  143. {0, 0, 0, 0}
  144. };
  145. if (argc < 2)
  146. usage ("\n");
  147. for (c = 1; c < argc; c++) {
  148. if (strcmp ("-to", argv[c]) == 0)
  149. strcpy (argv[c], "-t");
  150. else if (strcmp ("-wd", argv[c]) == 0)
  151. strcpy (argv[c], "-w");
  152. else if (strcmp ("-cd", argv[c]) == 0)
  153. strcpy (argv[c], "-c");
  154. else if (strcmp ("-wt", argv[c]) == 0)
  155. strcpy (argv[c], "-W");
  156. else if (strcmp ("-ct", argv[c]) == 0)
  157. strcpy (argv[c], "-C");
  158. }
  159. while (1) {
  160. c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:", long_options,
  161. &option_index);
  162. if (c == -1 || c == EOF)
  163. break;
  164. switch (c) {
  165. case '?': /* print short usage statement if args not parsable */
  166. usage3 ("Unknown argument", optopt);
  167. case 'h': /* help */
  168. print_help ();
  169. exit (STATE_OK);
  170. case 'V': /* version */
  171. print_revision (progname, REVISION);
  172. exit (STATE_OK);
  173. case 'H': /* hostname */
  174. if (is_host (optarg) == FALSE)
  175. usage ("Invalid host name/address\n");
  176. server_address = optarg;
  177. break;
  178. case 'w': /* warning-variance */
  179. if (is_intnonneg (optarg)) {
  180. warning_diff = strtoul (optarg, NULL, 10);
  181. check_warning_diff = TRUE;
  182. }
  183. else if (strspn (optarg, "0123456789:,") > 0) {
  184. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  185. check_warning_diff = TRUE;
  186. check_warning_time = TRUE;
  187. }
  188. else {
  189. usage ("Warning thresholds must be a nonnegative integer\n");
  190. }
  191. }
  192. else {
  193. usage ("Warning threshold must be a nonnegative integer\n");
  194. }
  195. break;
  196. case 'c': /* critical-variance */
  197. if (is_intnonneg (optarg)) {
  198. critical_diff = strtoul (optarg, NULL, 10);
  199. check_critical_diff = TRUE;
  200. }
  201. else if (strspn (optarg, "0123456789:,") > 0) {
  202. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  203. 2) {
  204. check_critical_diff = TRUE;
  205. check_critical_time = TRUE;
  206. }
  207. else {
  208. usage ("Critical thresholds must be a nonnegative integer\n");
  209. }
  210. }
  211. else {
  212. usage ("Critical threshold must be a nonnegative integer\n");
  213. }
  214. break;
  215. case 'W': /* warning-connect */
  216. if (!is_intnonneg (optarg))
  217. usage ("Warning threshold must be a nonnegative integer\n");
  218. warning_time = atoi (optarg);
  219. check_warning_time = TRUE;
  220. break;
  221. case 'C': /* critical-connect */
  222. if (!is_intnonneg (optarg))
  223. usage ("Critical threshold must be a nonnegative integer\n");
  224. critical_time = atoi (optarg);
  225. check_critical_time = TRUE;
  226. break;
  227. case 'p': /* port */
  228. if (!is_intnonneg (optarg))
  229. usage ("Serevr port must be a nonnegative integer\n");
  230. server_port = atoi (optarg);
  231. break;
  232. case 't': /* timeout */
  233. if (!is_intnonneg (optarg))
  234. usage ("Timeout interval must be a nonnegative integer\n");
  235. socket_timeout = atoi (optarg);
  236. break;
  237. }
  238. }
  239. c = optind;
  240. if (server_address == NULL) {
  241. if (argc > c) {
  242. if (is_host (argv[c]) == FALSE)
  243. usage ("Invalid host name/address\n");
  244. server_address = argv[c];
  245. }
  246. else {
  247. usage ("Host name was not supplied\n");
  248. }
  249. }
  250. return OK;
  251. }
  252. void
  253. print_usage (void)
  254. {
  255. printf
  256. ("Usage:\n"
  257. " %s -H <host_address> [-p port] [-w variance] [-c variance]\n"
  258. " [-W connect_time] [-C connect_time] [-t timeout]\n"
  259. " %s (-h | --help) for detailed help\n"
  260. " %s (-V | --version) for version information\n",
  261. progname, progname, progname);
  262. }
  263. void
  264. print_help (void)
  265. {
  266. print_revision (progname, REVISION);
  267. printf
  268. ("Copyright (c) %s %s <%s>\n\n%s\n",
  269. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  270. print_usage ();
  271. printf
  272. ("Options:\n"
  273. " -H, --hostname=ADDRESS\n"
  274. " Host name argument for servers using host headers (use numeric\n"
  275. " address if possible to bypass DNS lookup).\n"
  276. " -w, --warning-variance=INTEGER\n"
  277. " Time difference (sec.) necessary to result in a warning status\n"
  278. " -c, --critical-variance=INTEGER\n"
  279. " Time difference (sec.) necessary to result in a critical status\n"
  280. " -W, --warning-connect=INTEGER\n"
  281. " Response time (sec.) necessary to result in warning status\n"
  282. " -C, --critical-connect=INTEGER\n"
  283. " Response time (sec.) necessary to result in critical status\n"
  284. " -t, --timeout=INTEGER\n"
  285. " Seconds before connection times out (default: %d)\n"
  286. " -p, --port=INTEGER\n"
  287. " Port number (default: %d)\n"
  288. " -h, --help\n"
  289. " Print detailed help screen\n"
  290. " -V, --version\n"
  291. " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT, TIME_PORT);
  292. support ();
  293. }