check_time.c 10 KB

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