check_time.c 10 KB

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