4
0

check_time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /******************************************************************************
  2. *
  3. * Nagios check_time plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_time plugin
  13. *
  14. * This plugin will check the time on the specified host.
  15. *
  16. * License Information:
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. *
  32. *
  33. * $Id$
  34. *
  35. ******************************************************************************/
  36. const char *progname = "check_time";
  37. const char *revision = "$Revision$";
  38. const char *copyright = "1999-2006";
  39. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  40. #include "common.h"
  41. #include "netutils.h"
  42. #include "utils.h"
  43. enum {
  44. TIME_PORT = 37
  45. };
  46. #define UNIX_EPOCH 2208988800UL
  47. uint32_t server_time, raw_server_time;
  48. time_t diff_time;
  49. int warning_time = 0;
  50. int check_warning_time = FALSE;
  51. int critical_time = 0;
  52. int check_critical_time = FALSE;
  53. unsigned long warning_diff = 0;
  54. int check_warning_diff = FALSE;
  55. unsigned long critical_diff = 0;
  56. int check_critical_diff = FALSE;
  57. int server_port = TIME_PORT;
  58. char *server_address = NULL;
  59. int use_udp = FALSE;
  60. int process_arguments (int, char **);
  61. void print_help (void);
  62. void print_usage (void);
  63. int
  64. main (int argc, char **argv)
  65. {
  66. int sd;
  67. int result = STATE_UNKNOWN;
  68. time_t conntime;
  69. setlocale (LC_ALL, "");
  70. bindtextdomain (PACKAGE, LOCALEDIR);
  71. textdomain (PACKAGE);
  72. if (process_arguments (argc, argv) == ERROR)
  73. usage4 (_("Could not parse arguments"));
  74. /* initialize alarm signal handling */
  75. signal (SIGALRM, socket_timeout_alarm_handler);
  76. /* set socket timeout */
  77. alarm (socket_timeout);
  78. time (&start_time);
  79. /* try to connect to the host at the given port number */
  80. if (use_udp) {
  81. result = my_udp_connect (server_address, server_port, &sd);
  82. } else {
  83. result = my_tcp_connect (server_address, server_port, &sd);
  84. }
  85. if (result != STATE_OK) {
  86. if (check_critical_time == TRUE)
  87. result = STATE_CRITICAL;
  88. else if (check_warning_time == TRUE)
  89. result = STATE_WARNING;
  90. else
  91. result = STATE_UNKNOWN;
  92. die (result,
  93. _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
  94. server_address, server_port);
  95. }
  96. if (use_udp) {
  97. if (send (sd, "", 0, 0) < 0) {
  98. if (check_critical_time == TRUE)
  99. result = STATE_CRITICAL;
  100. else if (check_warning_time == TRUE)
  101. result = STATE_WARNING;
  102. else
  103. result = STATE_UNKNOWN;
  104. die (result,
  105. _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
  106. server_address, server_port);
  107. }
  108. }
  109. /* watch for the connection string */
  110. result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
  111. /* close the connection */
  112. close (sd);
  113. /* reset the alarm */
  114. time (&end_time);
  115. alarm (0);
  116. /* return a WARNING status if we couldn't read any data */
  117. if (result <= 0) {
  118. if (check_critical_time == TRUE)
  119. result = STATE_CRITICAL;
  120. else if (check_warning_time == TRUE)
  121. result = STATE_WARNING;
  122. else
  123. result = STATE_UNKNOWN;
  124. die (result,
  125. _("TIME UNKNOWN - no data received from server %s, port %d\n"),
  126. server_address, server_port);
  127. }
  128. result = STATE_OK;
  129. conntime = (end_time - start_time);
  130. if (check_critical_time == TRUE && conntime > critical_time)
  131. result = STATE_CRITICAL;
  132. else if (check_warning_time == TRUE && conntime > warning_time)
  133. result = STATE_WARNING;
  134. if (result != STATE_OK)
  135. die (result, _("TIME %s - %d second response time|%s\n"),
  136. state_text (result), (int)conntime,
  137. perfdata ("time", (long)conntime, "s",
  138. check_warning_time, (long)warning_time,
  139. check_critical_time, (long)critical_time,
  140. TRUE, 0, FALSE, 0));
  141. server_time = ntohl (raw_server_time) - UNIX_EPOCH;
  142. if (server_time > (unsigned long)end_time)
  143. diff_time = server_time - (unsigned long)end_time;
  144. else
  145. diff_time = (unsigned long)end_time - server_time;
  146. if (check_critical_diff == TRUE && diff_time > (time_t)critical_diff)
  147. result = STATE_CRITICAL;
  148. else if (check_warning_diff == TRUE && diff_time > (time_t)warning_diff)
  149. result = STATE_WARNING;
  150. printf (_("TIME %s - %lu second time difference|%s %s\n"),
  151. state_text (result), diff_time,
  152. perfdata ("time", (long)conntime, "s",
  153. check_warning_time, (long)warning_time,
  154. check_critical_time, (long)critical_time,
  155. TRUE, 0, FALSE, 0),
  156. perfdata ("offset", (long)diff_time, "s",
  157. check_warning_diff, (long)warning_diff,
  158. check_critical_diff, (long)critical_diff,
  159. TRUE, 0, FALSE, 0));
  160. return result;
  161. }
  162. /* process command-line arguments */
  163. int
  164. process_arguments (int argc, char **argv)
  165. {
  166. int c;
  167. int option = 0;
  168. static struct option longopts[] = {
  169. {"hostname", required_argument, 0, 'H'},
  170. {"warning-variance", required_argument, 0, 'w'},
  171. {"critical-variance", required_argument, 0, 'c'},
  172. {"warning-connect", required_argument, 0, 'W'},
  173. {"critical-connect", required_argument, 0, 'C'},
  174. {"port", required_argument, 0, 'p'},
  175. {"udp", no_argument, 0, 'u'},
  176. {"timeout", required_argument, 0, 't'},
  177. {"version", no_argument, 0, 'V'},
  178. {"help", no_argument, 0, 'h'},
  179. {0, 0, 0, 0}
  180. };
  181. if (argc < 2)
  182. usage ("\n");
  183. for (c = 1; c < argc; c++) {
  184. if (strcmp ("-to", argv[c]) == 0)
  185. strcpy (argv[c], "-t");
  186. else if (strcmp ("-wd", argv[c]) == 0)
  187. strcpy (argv[c], "-w");
  188. else if (strcmp ("-cd", argv[c]) == 0)
  189. strcpy (argv[c], "-c");
  190. else if (strcmp ("-wt", argv[c]) == 0)
  191. strcpy (argv[c], "-W");
  192. else if (strcmp ("-ct", argv[c]) == 0)
  193. strcpy (argv[c], "-C");
  194. }
  195. while (1) {
  196. c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
  197. &option);
  198. if (c == -1 || c == EOF)
  199. break;
  200. switch (c) {
  201. case '?': /* print short usage statement if args not parsable */
  202. usage2 (_("Unknown argument"), optarg);
  203. case 'h': /* help */
  204. print_help ();
  205. exit (STATE_OK);
  206. case 'V': /* version */
  207. print_revision (progname, revision);
  208. exit (STATE_OK);
  209. case 'H': /* hostname */
  210. if (is_host (optarg) == FALSE)
  211. usage2 (_("Invalid hostname/address"), optarg);
  212. server_address = optarg;
  213. break;
  214. case 'w': /* warning-variance */
  215. if (is_intnonneg (optarg)) {
  216. warning_diff = strtoul (optarg, NULL, 10);
  217. check_warning_diff = TRUE;
  218. }
  219. else if (strspn (optarg, "0123456789:,") > 0) {
  220. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  221. check_warning_diff = TRUE;
  222. check_warning_time = TRUE;
  223. }
  224. else {
  225. usage4 (_("Warning thresholds must be a positive integer"));
  226. }
  227. }
  228. else {
  229. usage4 (_("Warning threshold must be a positive integer"));
  230. }
  231. break;
  232. case 'c': /* critical-variance */
  233. if (is_intnonneg (optarg)) {
  234. critical_diff = strtoul (optarg, NULL, 10);
  235. check_critical_diff = TRUE;
  236. }
  237. else if (strspn (optarg, "0123456789:,") > 0) {
  238. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  239. 2) {
  240. check_critical_diff = TRUE;
  241. check_critical_time = TRUE;
  242. }
  243. else {
  244. usage4 (_("Critical thresholds must be a positive integer"));
  245. }
  246. }
  247. else {
  248. usage4 (_("Critical threshold must be a positive integer"));
  249. }
  250. break;
  251. case 'W': /* warning-connect */
  252. if (!is_intnonneg (optarg))
  253. usage4 (_("Warning threshold must be a positive integer"));
  254. else
  255. warning_time = atoi (optarg);
  256. check_warning_time = TRUE;
  257. break;
  258. case 'C': /* critical-connect */
  259. if (!is_intnonneg (optarg))
  260. usage4 (_("Critical threshold must be a positive integer"));
  261. else
  262. critical_time = atoi (optarg);
  263. check_critical_time = TRUE;
  264. break;
  265. case 'p': /* port */
  266. if (!is_intnonneg (optarg))
  267. usage4 (_("Port must be a positive integer"));
  268. else
  269. server_port = atoi (optarg);
  270. break;
  271. case 't': /* timeout */
  272. if (!is_intnonneg (optarg))
  273. usage2 (_("Timeout interval must be a positive integer"), optarg);
  274. else
  275. socket_timeout = atoi (optarg);
  276. break;
  277. case 'u': /* udp */
  278. use_udp = TRUE;
  279. }
  280. }
  281. c = optind;
  282. if (server_address == NULL) {
  283. if (argc > c) {
  284. if (is_host (argv[c]) == FALSE)
  285. usage2 (_("Invalid hostname/address"), optarg);
  286. server_address = argv[c];
  287. }
  288. else {
  289. usage4 (_("Hostname was not supplied"));
  290. }
  291. }
  292. return OK;
  293. }
  294. void
  295. print_help (void)
  296. {
  297. char *myport;
  298. asprintf (&myport, "%d", TIME_PORT);
  299. print_revision (progname, revision);
  300. printf ("Copyright (c) 1999 Ethan Galstad\n");
  301. printf (COPYRIGHT, copyright, email);
  302. printf ("%s\n", _("This plugin will check the time on the specified host."));
  303. printf ("\n\n");
  304. print_usage ();
  305. printf (_(UT_HELP_VRSN));
  306. printf (_(UT_HOST_PORT), 'p', myport);
  307. printf (" %s\n", "-u, --udp");
  308. printf (" %s\n", _("Use UDP to connect, not TCP"));
  309. printf (" %s\n", "-w, --warning-variance=INTEGER");
  310. printf (" %s\n", _("Time difference (sec.) necessary to result in a warning status"));
  311. printf (" %s\n", "-c, --critical-variance=INTEGER");
  312. printf (" %s\n", _("Time difference (sec.) necessary to result in a critical status"));
  313. printf (" %s\n", "-W, --warning-connect=INTEGER");
  314. printf (" %s\n", _("Response time (sec.) necessary to result in warning status"));
  315. printf (" %s\n", "-C, --critical-connect=INTEGER");
  316. printf (" %s\n", _("Response time (sec.) necessary to result in critical status"));
  317. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  318. printf (_(UT_SUPPORT));
  319. }
  320. void
  321. print_usage (void)
  322. {
  323. printf (_("Usage:"));
  324. printf ("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n",progname);
  325. printf (" [-W connect_time] [-C connect_time] [-t timeout]\n");
  326. }