check_time.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. if (process_arguments (argc, argv) != OK)
  47. usage (_("Invalid command arguments supplied\n"));
  48. /* initialize alarm signal handling */
  49. signal (SIGALRM, socket_timeout_alarm_handler);
  50. /* set socket timeout */
  51. alarm (socket_timeout);
  52. time (&start_time);
  53. /* try to connect to the host at the given port number */
  54. if (use_udp) {
  55. result = my_udp_connect (server_address, server_port, &sd);
  56. } else {
  57. result = my_tcp_connect (server_address, server_port, &sd);
  58. }
  59. if (result != STATE_OK) {
  60. if (check_critical_time == TRUE)
  61. result = STATE_CRITICAL;
  62. else if (check_warning_time == TRUE)
  63. result = STATE_WARNING;
  64. else
  65. result = STATE_UNKNOWN;
  66. die (result,
  67. _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
  68. server_address, server_port);
  69. }
  70. if (use_udp) {
  71. if (send (sd, "", 0, 0) < 0) {
  72. if (check_critical_time == TRUE)
  73. result = STATE_CRITICAL;
  74. else if (check_warning_time == TRUE)
  75. result = STATE_WARNING;
  76. else
  77. result = STATE_UNKNOWN;
  78. die (result,
  79. _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
  80. server_address, server_port);
  81. }
  82. }
  83. /* watch for the connection string */
  84. result = recv (sd, (void *)&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. die (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. die (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 > (unsigned long)end_time)
  112. diff_time = server_time - (unsigned long)end_time;
  113. else
  114. diff_time = (unsigned long)end_time - server_time;
  115. if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
  116. result = STATE_CRITICAL;
  117. else if (check_warning_diff == TRUE && diff_time > (time_t)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. int option = 0;
  129. static struct option longopts[] = {
  130. {"hostname", required_argument, 0, 'H'},
  131. {"warning-variance", required_argument, 0, 'w'},
  132. {"critical-variance", required_argument, 0, 'c'},
  133. {"warning-connect", required_argument, 0, 'W'},
  134. {"critical-connect", required_argument, 0, 'C'},
  135. {"port", required_argument, 0, 'p'},
  136. {"udp", no_argument, 0, 'u'},
  137. {"timeout", required_argument, 0, 't'},
  138. {"version", no_argument, 0, 'V'},
  139. {"help", no_argument, 0, 'h'},
  140. {0, 0, 0, 0}
  141. };
  142. if (argc < 2)
  143. usage ("\n");
  144. for (c = 1; c < argc; c++) {
  145. if (strcmp ("-to", argv[c]) == 0)
  146. strcpy (argv[c], "-t");
  147. else if (strcmp ("-wd", argv[c]) == 0)
  148. strcpy (argv[c], "-w");
  149. else if (strcmp ("-cd", argv[c]) == 0)
  150. strcpy (argv[c], "-c");
  151. else if (strcmp ("-wt", argv[c]) == 0)
  152. strcpy (argv[c], "-W");
  153. else if (strcmp ("-ct", argv[c]) == 0)
  154. strcpy (argv[c], "-C");
  155. }
  156. while (1) {
  157. c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
  158. &option);
  159. if (c == -1 || c == EOF)
  160. break;
  161. switch (c) {
  162. case '?': /* print short usage statement if args not parsable */
  163. usage3 (_("Unknown argument"), optopt);
  164. case 'h': /* help */
  165. print_help ();
  166. exit (STATE_OK);
  167. case 'V': /* version */
  168. print_revision (progname, revision);
  169. exit (STATE_OK);
  170. case 'H': /* hostname */
  171. if (is_host (optarg) == FALSE)
  172. usage (_("Invalid host name/address\n"));
  173. server_address = optarg;
  174. break;
  175. case 'w': /* warning-variance */
  176. if (is_intnonneg (optarg)) {
  177. warning_diff = strtoul (optarg, NULL, 10);
  178. check_warning_diff = TRUE;
  179. }
  180. else if (strspn (optarg, "0123456789:,") > 0) {
  181. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  182. check_warning_diff = TRUE;
  183. check_warning_time = TRUE;
  184. }
  185. else {
  186. usage (_("Warning thresholds must be a nonnegative integer\n"));
  187. }
  188. }
  189. else {
  190. usage (_("Warning threshold must be a nonnegative integer\n"));
  191. }
  192. break;
  193. case 'c': /* critical-variance */
  194. if (is_intnonneg (optarg)) {
  195. critical_diff = strtoul (optarg, NULL, 10);
  196. check_critical_diff = TRUE;
  197. }
  198. else if (strspn (optarg, "0123456789:,") > 0) {
  199. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  200. 2) {
  201. check_critical_diff = TRUE;
  202. check_critical_time = TRUE;
  203. }
  204. else {
  205. usage (_("Critical thresholds must be a nonnegative integer\n"));
  206. }
  207. }
  208. else {
  209. usage (_("Critical threshold must be a nonnegative integer\n"));
  210. }
  211. break;
  212. case 'W': /* warning-connect */
  213. if (!is_intnonneg (optarg))
  214. usage (_("Warning threshold must be a nonnegative integer\n"));
  215. else
  216. warning_time = atoi (optarg);
  217. check_warning_time = TRUE;
  218. break;
  219. case 'C': /* critical-connect */
  220. if (!is_intnonneg (optarg))
  221. usage (_("Critical threshold must be a nonnegative integer\n"));
  222. else
  223. critical_time = atoi (optarg);
  224. check_critical_time = TRUE;
  225. break;
  226. case 'p': /* port */
  227. if (!is_intnonneg (optarg))
  228. usage (_("Server port must be a nonnegative integer\n"));
  229. else
  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. else
  236. socket_timeout = atoi (optarg);
  237. break;
  238. case 'u': /* udp */
  239. use_udp = TRUE;
  240. }
  241. }
  242. c = optind;
  243. if (server_address == NULL) {
  244. if (argc > c) {
  245. if (is_host (argv[c]) == FALSE)
  246. usage (_("Invalid host name/address\n"));
  247. server_address = argv[c];
  248. }
  249. else {
  250. usage (_("Host name was not supplied\n"));
  251. }
  252. }
  253. return OK;
  254. }
  255. void
  256. print_help (void)
  257. {
  258. char *myport;
  259. asprintf (&myport, "%d", TIME_PORT);
  260. print_revision (progname, revision);
  261. printf (_("Copyright (c) 1999 Ethan Galstad\n"));
  262. printf (_(COPYRIGHT), copyright, email);
  263. printf (_("\
  264. This plugin will check the time on the specified host.\n\n"));
  265. print_usage ();
  266. printf (_(UT_HELP_VRSN));
  267. printf (_(UT_HOST_PORT), 'p', myport);
  268. printf (_("\
  269. -u, --udp\n\
  270. Use UDP to connect, not TCP\n\
  271. -w, --warning-variance=INTEGER\n\
  272. Time difference (sec.) necessary to result in a warning status\n\
  273. -c, --critical-variance=INTEGER\n\
  274. Time difference (sec.) necessary to result in a critical status\n\
  275. -W, --warning-connect=INTEGER\n\
  276. Response time (sec.) necessary to result in warning status\n\
  277. -C, --critical-connect=INTEGER\n\
  278. Response time (sec.) necessary to result in critical status\n"));
  279. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  280. printf (_(UT_SUPPORT));
  281. }
  282. void
  283. print_usage (void)
  284. {
  285. printf (_("\
  286. Usage: %s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n\
  287. [-W connect_time] [-C connect_time] [-t timeout]\n"), progname);
  288. printf (_(UT_HLP_VRS), progname, progname);
  289. }