check_time.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. ******************************************************************************/
  14. #include "common.h"
  15. #include "netutils.h"
  16. #include "utils.h"
  17. const char *progname = "check_time";
  18. const char *revision = "$Revision$";
  19. const char *copyright = "1999-2003";
  20. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  21. enum {
  22. TIME_PORT = 37
  23. };
  24. #define UNIX_EPOCH 2208988800UL
  25. unsigned long server_time, raw_server_time;
  26. time_t diff_time;
  27. int warning_time = 0;
  28. int check_warning_time = FALSE;
  29. int critical_time = 0;
  30. int check_critical_time = FALSE;
  31. unsigned long warning_diff = 0;
  32. int check_warning_diff = FALSE;
  33. unsigned long critical_diff = 0;
  34. int check_critical_diff = FALSE;
  35. int server_port = TIME_PORT;
  36. char *server_address = NULL;
  37. int use_udp = FALSE;
  38. int process_arguments (int, char **);
  39. void print_help (void);
  40. void print_usage (void);
  41. int
  42. main (int argc, char **argv)
  43. {
  44. int sd;
  45. int result;
  46. time_t conntime;
  47. setlocale (LC_ALL, "");
  48. bindtextdomain (PACKAGE, LOCALEDIR);
  49. textdomain (PACKAGE);
  50. if (process_arguments (argc, argv) != OK)
  51. usage (_("Invalid command arguments supplied\n"));
  52. /* initialize alarm signal handling */
  53. signal (SIGALRM, socket_timeout_alarm_handler);
  54. /* set socket timeout */
  55. alarm (socket_timeout);
  56. time (&start_time);
  57. /* try to connect to the host at the given port number */
  58. if (use_udp) {
  59. result = my_udp_connect (server_address, server_port, &sd);
  60. } else {
  61. result = my_tcp_connect (server_address, server_port, &sd);
  62. }
  63. if (result != STATE_OK) {
  64. if (check_critical_time == TRUE)
  65. result = STATE_CRITICAL;
  66. else if (check_warning_time == TRUE)
  67. result = STATE_WARNING;
  68. else
  69. result = STATE_UNKNOWN;
  70. die (result,
  71. _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
  72. server_address, server_port);
  73. }
  74. if (use_udp) {
  75. if (send (sd, "", 0, 0) < 0) {
  76. if (check_critical_time == TRUE)
  77. result = STATE_CRITICAL;
  78. else if (check_warning_time == TRUE)
  79. result = STATE_WARNING;
  80. else
  81. result = STATE_UNKNOWN;
  82. die (result,
  83. _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
  84. server_address, server_port);
  85. }
  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. die (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. conntime = (end_time - start_time);
  108. if (check_critical_time == TRUE && conntime > critical_time)
  109. result = STATE_CRITICAL;
  110. else if (check_warning_time == TRUE && conntime > warning_time)
  111. result = STATE_WARNING;
  112. if (result != STATE_OK)
  113. die (result, _("TIME %s - %d second response time|%s\n"),
  114. state_text (result), (int)conntime,
  115. perfdata ("time", (long)conntime, "s",
  116. check_warning_time, (long)warning_time,
  117. check_critical_time, (long)critical_time,
  118. TRUE, 0, FALSE, 0));
  119. server_time = ntohl (raw_server_time) - UNIX_EPOCH;
  120. if (server_time > (unsigned long)end_time)
  121. diff_time = server_time - (unsigned long)end_time;
  122. else
  123. diff_time = (unsigned long)end_time - server_time;
  124. if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
  125. result = STATE_CRITICAL;
  126. else if (check_warning_diff == TRUE && diff_time > (time_t)warning_diff)
  127. result = STATE_WARNING;
  128. printf (_("TIME %s - %lu second time difference|%s %s\n"),
  129. state_text (result), diff_time,
  130. perfdata ("time", (long)conntime, "s",
  131. check_warning_time, (long)warning_time,
  132. check_critical_time, (long)critical_time,
  133. TRUE, 0, FALSE, 0),
  134. perfdata ("offset", (long)diff_time, "s",
  135. check_warning_diff, (long)warning_diff,
  136. check_critical_diff, (long)critical_diff,
  137. TRUE, 0, FALSE, 0));
  138. return result;
  139. }
  140. /* process command-line arguments */
  141. int
  142. process_arguments (int argc, char **argv)
  143. {
  144. int c;
  145. int option = 0;
  146. static struct option longopts[] = {
  147. {"hostname", required_argument, 0, 'H'},
  148. {"warning-variance", required_argument, 0, 'w'},
  149. {"critical-variance", required_argument, 0, 'c'},
  150. {"warning-connect", required_argument, 0, 'W'},
  151. {"critical-connect", required_argument, 0, 'C'},
  152. {"port", required_argument, 0, 'p'},
  153. {"udp", no_argument, 0, 'u'},
  154. {"timeout", required_argument, 0, 't'},
  155. {"version", no_argument, 0, 'V'},
  156. {"help", no_argument, 0, 'h'},
  157. {0, 0, 0, 0}
  158. };
  159. if (argc < 2)
  160. usage ("\n");
  161. for (c = 1; c < argc; c++) {
  162. if (strcmp ("-to", argv[c]) == 0)
  163. strcpy (argv[c], "-t");
  164. else if (strcmp ("-wd", argv[c]) == 0)
  165. strcpy (argv[c], "-w");
  166. else if (strcmp ("-cd", argv[c]) == 0)
  167. strcpy (argv[c], "-c");
  168. else if (strcmp ("-wt", argv[c]) == 0)
  169. strcpy (argv[c], "-W");
  170. else if (strcmp ("-ct", argv[c]) == 0)
  171. strcpy (argv[c], "-C");
  172. }
  173. while (1) {
  174. c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
  175. &option);
  176. if (c == -1 || c == EOF)
  177. break;
  178. switch (c) {
  179. case '?': /* print short usage statement if args not parsable */
  180. usage3 (_("Unknown argument"), optopt);
  181. case 'h': /* help */
  182. print_help ();
  183. exit (STATE_OK);
  184. case 'V': /* version */
  185. print_revision (progname, revision);
  186. exit (STATE_OK);
  187. case 'H': /* hostname */
  188. if (is_host (optarg) == FALSE)
  189. usage (_("Invalid host name/address\n"));
  190. server_address = optarg;
  191. break;
  192. case 'w': /* warning-variance */
  193. if (is_intnonneg (optarg)) {
  194. warning_diff = strtoul (optarg, NULL, 10);
  195. check_warning_diff = TRUE;
  196. }
  197. else if (strspn (optarg, "0123456789:,") > 0) {
  198. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  199. check_warning_diff = TRUE;
  200. check_warning_time = TRUE;
  201. }
  202. else {
  203. usage (_("Warning thresholds must be a nonnegative integer\n"));
  204. }
  205. }
  206. else {
  207. usage (_("Warning threshold must be a nonnegative integer\n"));
  208. }
  209. break;
  210. case 'c': /* critical-variance */
  211. if (is_intnonneg (optarg)) {
  212. critical_diff = strtoul (optarg, NULL, 10);
  213. check_critical_diff = TRUE;
  214. }
  215. else if (strspn (optarg, "0123456789:,") > 0) {
  216. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  217. 2) {
  218. check_critical_diff = TRUE;
  219. check_critical_time = TRUE;
  220. }
  221. else {
  222. usage (_("Critical thresholds must be a nonnegative integer\n"));
  223. }
  224. }
  225. else {
  226. usage (_("Critical threshold must be a nonnegative integer\n"));
  227. }
  228. break;
  229. case 'W': /* warning-connect */
  230. if (!is_intnonneg (optarg))
  231. usage (_("Warning threshold must be a nonnegative integer\n"));
  232. else
  233. warning_time = atoi (optarg);
  234. check_warning_time = TRUE;
  235. break;
  236. case 'C': /* critical-connect */
  237. if (!is_intnonneg (optarg))
  238. usage (_("Critical threshold must be a nonnegative integer\n"));
  239. else
  240. critical_time = atoi (optarg);
  241. check_critical_time = TRUE;
  242. break;
  243. case 'p': /* port */
  244. if (!is_intnonneg (optarg))
  245. usage (_("Server port must be a nonnegative integer\n"));
  246. else
  247. server_port = atoi (optarg);
  248. break;
  249. case 't': /* timeout */
  250. if (!is_intnonneg (optarg))
  251. usage (_("Timeout interval must be a nonnegative integer\n"));
  252. else
  253. socket_timeout = atoi (optarg);
  254. break;
  255. case 'u': /* udp */
  256. use_udp = TRUE;
  257. }
  258. }
  259. c = optind;
  260. if (server_address == NULL) {
  261. if (argc > c) {
  262. if (is_host (argv[c]) == FALSE)
  263. usage (_("Invalid host name/address\n"));
  264. server_address = argv[c];
  265. }
  266. else {
  267. usage (_("Host name was not supplied\n"));
  268. }
  269. }
  270. return OK;
  271. }
  272. void
  273. print_help (void)
  274. {
  275. char *myport;
  276. asprintf (&myport, "%d", TIME_PORT);
  277. print_revision (progname, revision);
  278. printf (_("Copyright (c) 1999 Ethan Galstad\n"));
  279. printf (_(COPYRIGHT), copyright, email);
  280. printf (_("\
  281. 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. printf (_(UT_HLP_VRS), progname, progname);
  306. }