check_time.c 10 KB

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