check_time.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*****************************************************************************
  2. *
  3. * Nagios check_time plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2007 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-2007";
  32. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  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. {"version", no_argument, 0, 'V'},
  173. {"help", no_argument, 0, 'h'},
  174. {0, 0, 0, 0}
  175. };
  176. if (argc < 2)
  177. usage ("\n");
  178. for (c = 1; c < argc; c++) {
  179. if (strcmp ("-to", argv[c]) == 0)
  180. strcpy (argv[c], "-t");
  181. else if (strcmp ("-wd", argv[c]) == 0)
  182. strcpy (argv[c], "-w");
  183. else if (strcmp ("-cd", argv[c]) == 0)
  184. strcpy (argv[c], "-c");
  185. else if (strcmp ("-wt", argv[c]) == 0)
  186. strcpy (argv[c], "-W");
  187. else if (strcmp ("-ct", argv[c]) == 0)
  188. strcpy (argv[c], "-C");
  189. }
  190. while (1) {
  191. c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
  192. &option);
  193. if (c == -1 || c == EOF)
  194. break;
  195. switch (c) {
  196. case '?': /* print short usage statement if args not parsable */
  197. usage5 ();
  198. case 'h': /* help */
  199. print_help ();
  200. exit (STATE_OK);
  201. case 'V': /* version */
  202. print_revision (progname, NP_VERSION);
  203. exit (STATE_OK);
  204. case 'H': /* hostname */
  205. if (is_host (optarg) == FALSE)
  206. usage2 (_("Invalid hostname/address"), optarg);
  207. server_address = optarg;
  208. break;
  209. case 'w': /* warning-variance */
  210. if (is_intnonneg (optarg)) {
  211. warning_diff = strtoul (optarg, NULL, 10);
  212. check_warning_diff = TRUE;
  213. }
  214. else if (strspn (optarg, "0123456789:,") > 0) {
  215. if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
  216. check_warning_diff = TRUE;
  217. check_warning_time = TRUE;
  218. }
  219. else {
  220. usage4 (_("Warning thresholds must be a positive integer"));
  221. }
  222. }
  223. else {
  224. usage4 (_("Warning threshold must be a positive integer"));
  225. }
  226. break;
  227. case 'c': /* critical-variance */
  228. if (is_intnonneg (optarg)) {
  229. critical_diff = strtoul (optarg, NULL, 10);
  230. check_critical_diff = TRUE;
  231. }
  232. else if (strspn (optarg, "0123456789:,") > 0) {
  233. if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
  234. 2) {
  235. check_critical_diff = TRUE;
  236. check_critical_time = TRUE;
  237. }
  238. else {
  239. usage4 (_("Critical thresholds must be a positive integer"));
  240. }
  241. }
  242. else {
  243. usage4 (_("Critical threshold must be a positive integer"));
  244. }
  245. break;
  246. case 'W': /* warning-connect */
  247. if (!is_intnonneg (optarg))
  248. usage4 (_("Warning threshold must be a positive integer"));
  249. else
  250. warning_time = atoi (optarg);
  251. check_warning_time = TRUE;
  252. break;
  253. case 'C': /* critical-connect */
  254. if (!is_intnonneg (optarg))
  255. usage4 (_("Critical threshold must be a positive integer"));
  256. else
  257. critical_time = atoi (optarg);
  258. check_critical_time = TRUE;
  259. break;
  260. case 'p': /* port */
  261. if (!is_intnonneg (optarg))
  262. usage4 (_("Port must be a positive integer"));
  263. else
  264. server_port = atoi (optarg);
  265. break;
  266. case 't': /* timeout */
  267. if (!is_intnonneg (optarg))
  268. usage2 (_("Timeout interval must be a positive integer"), optarg);
  269. else
  270. socket_timeout = atoi (optarg);
  271. break;
  272. case 'u': /* udp */
  273. use_udp = TRUE;
  274. }
  275. }
  276. c = optind;
  277. if (server_address == NULL) {
  278. if (argc > c) {
  279. if (is_host (argv[c]) == FALSE)
  280. usage2 (_("Invalid hostname/address"), optarg);
  281. server_address = argv[c];
  282. }
  283. else {
  284. usage4 (_("Hostname was not supplied"));
  285. }
  286. }
  287. return OK;
  288. }
  289. void
  290. print_help (void)
  291. {
  292. char *myport;
  293. asprintf (&myport, "%d", TIME_PORT);
  294. print_revision (progname, NP_VERSION);
  295. printf ("Copyright (c) 1999 Ethan Galstad\n");
  296. printf (COPYRIGHT, copyright, email);
  297. printf ("%s\n", _("This plugin will check the time on the specified host."));
  298. printf ("\n\n");
  299. print_usage ();
  300. printf (UT_HELP_VRSN);
  301. printf (UT_EXTRA_OPTS);
  302. printf (UT_HOST_PORT, 'p', myport);
  303. printf (" %s\n", "-u, --udp");
  304. printf (" %s\n", _("Use UDP to connect, not TCP"));
  305. printf (" %s\n", "-w, --warning-variance=INTEGER");
  306. printf (" %s\n", _("Time difference (sec.) necessary to result in a warning status"));
  307. printf (" %s\n", "-c, --critical-variance=INTEGER");
  308. printf (" %s\n", _("Time difference (sec.) necessary to result in a critical status"));
  309. printf (" %s\n", "-W, --warning-connect=INTEGER");
  310. printf (" %s\n", _("Response time (sec.) necessary to result in warning status"));
  311. printf (" %s\n", "-C, --critical-connect=INTEGER");
  312. printf (" %s\n", _("Response time (sec.) necessary to result in critical status"));
  313. printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  314. #ifdef NP_EXTRA_OPTS
  315. printf ("\n");
  316. printf ("%s\n", _("Notes:"));
  317. printf (UT_EXTRA_OPTS_NOTES);
  318. #endif
  319. printf (UT_SUPPORT);
  320. }
  321. void
  322. print_usage (void)
  323. {
  324. printf (_("Usage:"));
  325. printf ("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n",progname);
  326. printf (" [-W connect_time] [-C connect_time] [-t timeout]\n");
  327. }