check_time.c 8.4 KB

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