check_time.c 9.7 KB

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