check_time.c 10 KB

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