check_time.c 8.5 KB

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