4
0

check_time.c 9.1 KB

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