check_tcp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*****************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. *****************************************************************************/
  15. /* progname "check_tcp" changes depending on symlink called */
  16. char *progname;
  17. const char *revision = "$Revision$";
  18. const char *copyright = "1999-2004";
  19. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  20. #include "common.h"
  21. #include "netutils.h"
  22. #include "utils.h"
  23. #ifdef HAVE_SSL
  24. static int check_cert = FALSE;
  25. static int days_till_exp;
  26. static char *randbuff = "";
  27. static X509 *server_cert;
  28. # ifdef USE_OPENSSL
  29. static int check_certificate (X509 **);
  30. # endif /* USE_OPENSSL */
  31. # define my_recv(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
  32. # define my_send(buf, len) ((flags & FLAG_SSL) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
  33. #else
  34. # define my_recv(buf, len) read(sd, buf, len)
  35. # define my_send(buf, len) send(sd, buf, len, 0)
  36. #endif
  37. /* int my_recv(char *, size_t); */
  38. static int process_arguments (int, char **);
  39. void print_help (void);
  40. void print_usage (void);
  41. #define EXPECT server_expect[0]
  42. static char *SERVICE = "TCP";
  43. static char *SEND = NULL;
  44. static char *QUIT = NULL;
  45. static int PROTOCOL = IPPROTO_TCP; /* most common is default */
  46. static int PORT = 0;
  47. static char timestamp[17] = "";
  48. static int server_port = 0;
  49. static char *server_address = NULL;
  50. static char *server_send = NULL;
  51. static char *server_quit = NULL;
  52. static char **server_expect;
  53. static size_t server_expect_count = 0;
  54. static size_t maxbytes = 0;
  55. static char **warn_codes = NULL;
  56. static size_t warn_codes_count = 0;
  57. static char **crit_codes = NULL;
  58. static size_t crit_codes_count = 0;
  59. static unsigned int delay = 0;
  60. static double warning_time = 0;
  61. static double critical_time = 0;
  62. static double elapsed_time = 0;
  63. static long microsec;
  64. static int sd = 0;
  65. #define MAXBUF 1024
  66. static char buffer[MAXBUF];
  67. static int expect_mismatch_state = STATE_WARNING;
  68. #define FLAG_SSL 0x01
  69. #define FLAG_VERBOSE 0x02
  70. #define FLAG_EXACT_MATCH 0x04
  71. #define FLAG_TIME_WARN 0x08
  72. #define FLAG_TIME_CRIT 0x10
  73. #define FLAG_HIDE_OUTPUT 0x20
  74. static size_t flags = FLAG_EXACT_MATCH;
  75. int
  76. main (int argc, char **argv)
  77. {
  78. int result = STATE_UNKNOWN;
  79. int i;
  80. char *status = NULL;
  81. struct timeval tv;
  82. size_t len, match = -1;
  83. setlocale (LC_ALL, "");
  84. bindtextdomain (PACKAGE, LOCALEDIR);
  85. textdomain (PACKAGE);
  86. /* determine program- and service-name quickly */
  87. progname = strrchr(argv[0], '/');
  88. if(progname != NULL) progname++;
  89. else progname = argv[0];
  90. len = strlen(progname);
  91. if(len > 6 && !memcmp(progname, "check_", 6)) {
  92. SERVICE = progname + 6;
  93. for(i = 0; i < len - 6; i++)
  94. SERVICE[i] = toupper(SERVICE[i]);
  95. }
  96. /* set up a resonable buffer at first (will be realloc()'ed if
  97. * user specifies other options) */
  98. server_expect = calloc(sizeof(char *), 2);
  99. /* determine defaults for this service's protocol */
  100. if (!strncmp(SERVICE, "UDP", 3)) {
  101. PROTOCOL = IPPROTO_UDP;
  102. }
  103. else if (!strncmp(SERVICE, "FTP", 3)) {
  104. EXPECT = "220";
  105. QUIT = "QUIT\r\n";
  106. PORT = 21;
  107. }
  108. else if (!strncmp(SERVICE, "POP", 3) || !strncmp(SERVICE, "POP3", 4)) {
  109. EXPECT = "+OK";
  110. QUIT = "QUIT\r\n";
  111. PORT = 110;
  112. }
  113. else if (!strncmp(SERVICE, "SMTP", 4)) {
  114. EXPECT = "220";
  115. QUIT = "QUIT\r\n";
  116. PORT = 25;
  117. }
  118. else if (!strncmp(SERVICE, "IMAP", 4)) {
  119. EXPECT = "* OK";
  120. QUIT = "a1 LOGOUT\r\n";
  121. PORT = 143;
  122. }
  123. #ifdef HAVE_SSL
  124. else if (!strncmp(SERVICE, "SIMAP", 5)) {
  125. EXPECT = "* OK";
  126. QUIT = "a1 LOGOUT\r\n";
  127. flags |= FLAG_SSL;
  128. PORT = 993;
  129. }
  130. else if (!strncmp(SERVICE, "SPOP", 4)) {
  131. EXPECT = "+OK";
  132. QUIT = "QUIT\r\n";
  133. flags |= FLAG_SSL;
  134. PORT = 995;
  135. }
  136. else if (!strncmp(SERVICE, "SSMTP", 5)) {
  137. EXPECT = "220";
  138. QUIT = "QUIT\r\n";
  139. flags |= FLAG_SSL;
  140. PORT = 465;
  141. }
  142. else if (!strncmp(SERVICE, "JABBER", 6)) {
  143. SEND = "<stream:stream to=\'host\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>\n";
  144. EXPECT = "<?xml version=\'1.0\'?><stream:stream xmlns:stream=\'http://etherx.jabber.org/streams\'";
  145. QUIT = "</stream:stream>\n";
  146. flags |= FLAG_SSL | FLAG_HIDE_OUTPUT;
  147. PORT = 5222;
  148. }
  149. else if (!strncmp (SERVICE, "NNTPS", 5)) {
  150. server_expect_count = 2;
  151. server_expect[0] = "200";
  152. server_expect[1] = "201";
  153. QUIT = "QUIT\r\n";
  154. flags |= FLAG_SSL;
  155. PORT = 563;
  156. }
  157. #endif
  158. else if (!strncmp (SERVICE, "NNTP", 4)) {
  159. server_expect_count = 2;
  160. server_expect = malloc(sizeof(char *) * server_expect_count);
  161. server_expect[0] = strdup("200");
  162. server_expect[1] = strdup("201");
  163. QUIT = "QUIT\r\n";
  164. PORT = 119;
  165. }
  166. /* fallthrough check, so it's supposed to use reverse matching */
  167. else if (strcmp (SERVICE, "TCP"))
  168. usage (_("CRITICAL - Generic check_tcp called with unknown service\n"));
  169. server_address = "127.0.0.1";
  170. server_port = PORT;
  171. server_send = SEND;
  172. server_quit = QUIT;
  173. status = NULL;
  174. if (process_arguments (argc, argv) == ERROR)
  175. usage4 (_("Could not parse arguments"));
  176. if(flags & FLAG_VERBOSE) {
  177. printf("Using service %s\n", SERVICE);
  178. printf("Port: %d\n", PORT);
  179. printf("flags: 0x%x\n", flags);
  180. }
  181. if(EXPECT && !server_expect_count)
  182. server_expect_count++;
  183. /* set up the timer */
  184. signal (SIGALRM, socket_timeout_alarm_handler);
  185. alarm (socket_timeout);
  186. /* try to connect to the host at the given port number */
  187. gettimeofday (&tv, NULL);
  188. result = np_net_connect (server_address, server_port, &sd, PROTOCOL);
  189. if (result == STATE_CRITICAL) return STATE_CRITICAL;
  190. #ifdef HAVE_SSL
  191. if (flags & FLAG_SSL){
  192. result = np_net_ssl_init(sd);
  193. if(result != STATE_OK) return result;
  194. /* XXX does np_net_ssl take care of printing an error?
  195. die (STATE_CRITICAL,_("CRITICAL - Could not make SSL connection\n"));
  196. */
  197. }
  198. # ifdef USE_OPENSSL /* XXX gnutls does cert checking differently */
  199. /*
  200. if (flags & FLAG_SSL && check_cert == TRUE) {
  201. if ((server_cert = SSL_get_peer_certificate (ssl)) != NULL) {
  202. result = check_certificate (&server_cert);
  203. X509_free(server_cert);
  204. }
  205. else {
  206. printf(_("CRITICAL - Cannot retrieve server certificate.\n"));
  207. result = STATE_CRITICAL;
  208. }
  209. }
  210. */
  211. # endif /* USE_OPENSSL */
  212. #endif
  213. if(result != STATE_OK){
  214. #ifdef HAVE_SSL
  215. np_net_ssl_cleanup();
  216. #endif
  217. if(sd) close(sd);
  218. return result;
  219. }
  220. if (server_send != NULL) { /* Something to send? */
  221. my_send(server_send, strlen(server_send));
  222. }
  223. if (delay > 0) {
  224. tv.tv_sec += delay;
  225. sleep (delay);
  226. }
  227. if(flags & FLAG_VERBOSE) {
  228. printf("server_expect_count: %d\n", server_expect_count);
  229. for(i = 0; i < server_expect_count; i++)
  230. printf("\t%d: %s\n", i, server_expect[i]);
  231. }
  232. /* if(len) later on, we know we have a non-NULL response */
  233. len = 0;
  234. if (server_expect_count) {
  235. /* watch for the expect string */
  236. while ((i = my_recv(buffer, sizeof(buffer))) > 0) {
  237. status = realloc(status, len + i + 1);
  238. memcpy(&status[len], buffer, i);
  239. len += i;
  240. /* stop reading if user-forced or data-starved */
  241. if(i < sizeof(buffer) || (maxbytes && len >= maxbytes))
  242. break;
  243. if (maxbytes && len >= maxbytes)
  244. break;
  245. }
  246. /* no data when expected, so return critical */
  247. if (len == 0)
  248. die (STATE_CRITICAL, _("No data received from host\n"));
  249. /* force null-termination and strip whitespace from end of output */
  250. status[len--] = '\0';
  251. /* print raw output if we're debugging */
  252. if(flags & FLAG_VERBOSE)
  253. printf("received %d bytes from host\n#-raw-recv-------#\n%s\n#-raw-recv-------#\n",
  254. len + 1, status);
  255. while(isspace(status[len])) status[len--] = '\0';
  256. for (i = 0; i < server_expect_count; i++) {
  257. match = -2; /* tag it so we know if we tried and failed */
  258. if (flags & FLAG_VERBOSE)
  259. printf ("looking for [%s] %s [%s]\n", server_expect[i],
  260. (flags & FLAG_EXACT_MATCH) ? "in beginning of" : "anywhere in",
  261. status);
  262. /* match it. math first in short-circuit */
  263. if ((flags & FLAG_EXACT_MATCH && !strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
  264. (!(flags & FLAG_EXACT_MATCH) && strstr(status, server_expect[i])))
  265. {
  266. if(flags & FLAG_VERBOSE) puts("found it");
  267. match = i;
  268. break;
  269. }
  270. }
  271. }
  272. if (server_quit != NULL) {
  273. my_send(server_quit, strlen(server_quit));
  274. }
  275. #ifdef HAVE_SSL
  276. np_net_ssl_cleanup();
  277. #endif
  278. if (sd) close (sd);
  279. microsec = deltime (tv);
  280. elapsed_time = (double)microsec / 1.0e6;
  281. if (flags & FLAG_TIME_CRIT && elapsed_time > critical_time)
  282. result = STATE_CRITICAL;
  283. else if (flags & FLAG_TIME_WARN && elapsed_time > warning_time)
  284. result = STATE_WARNING;
  285. /* did we get the response we hoped? */
  286. if(match == -2 && result != STATE_CRITICAL)
  287. result = STATE_WARNING;
  288. /* reset the alarm */
  289. alarm (0);
  290. /* this is a bit stupid, because we don't want to print the
  291. * response time (which can look ok to the user) if we didn't get
  292. * the response we were looking for. if-else */
  293. printf(_("%s %s - "), SERVICE, state_text(result));
  294. if(match == -2 && len && !(flags & FLAG_HIDE_OUTPUT))
  295. printf("Unexpected response from host: %s", status);
  296. else
  297. printf("%.3f second response time on port %d",
  298. elapsed_time, server_port);
  299. if (match != -2 && !(flags & FLAG_HIDE_OUTPUT) && len)
  300. printf (" [%s]", status);
  301. /* perf-data doesn't apply when server doesn't talk properly,
  302. * so print all zeroes on warn and crit */
  303. if(match == -2)
  304. printf ("|time=%fs;0.0;0.0;0.0;0.0", elapsed_time);
  305. else
  306. printf("|%s",
  307. fperfdata ("time", elapsed_time, "s",
  308. TRUE, warning_time,
  309. TRUE, critical_time,
  310. TRUE, 0,
  311. TRUE, socket_timeout)
  312. );
  313. putchar('\n');
  314. return result;
  315. }
  316. /* process command-line arguments */
  317. static int
  318. process_arguments (int argc, char **argv)
  319. {
  320. int c;
  321. int option = 0;
  322. static struct option longopts[] = {
  323. {"hostname", required_argument, 0, 'H'},
  324. {"critical-time", required_argument, 0, 'c'},
  325. {"warning-time", required_argument, 0, 'w'},
  326. {"critical-codes", required_argument, 0, 'C'},
  327. {"warning-codes", required_argument, 0, 'W'},
  328. {"timeout", required_argument, 0, 't'},
  329. {"protocol", required_argument, 0, 'P'},
  330. {"port", required_argument, 0, 'p'},
  331. {"send", required_argument, 0, 's'},
  332. {"expect", required_argument, 0, 'e'},
  333. {"maxbytes", required_argument, 0, 'm'},
  334. {"quit", required_argument, 0, 'q'},
  335. {"jail", required_argument, 0, 'j'},
  336. {"delay", required_argument, 0, 'd'},
  337. {"refuse", required_argument, 0, 'r'},
  338. {"mismatch", required_argument, 0, 'M'},
  339. {"use-ipv4", no_argument, 0, '4'},
  340. {"use-ipv6", no_argument, 0, '6'},
  341. {"verbose", no_argument, 0, 'v'},
  342. {"version", no_argument, 0, 'V'},
  343. {"help", no_argument, 0, 'h'},
  344. #ifdef HAVE_SSL
  345. {"ssl", no_argument, 0, 'S'},
  346. {"certificate", required_argument, 0, 'D'},
  347. #endif
  348. {0, 0, 0, 0}
  349. };
  350. if (argc < 2)
  351. usage4 (_("No arguments found"));
  352. /* backwards compatibility */
  353. for (c = 1; c < argc; c++) {
  354. if (strcmp ("-to", argv[c]) == 0)
  355. strcpy (argv[c], "-t");
  356. else if (strcmp ("-wt", argv[c]) == 0)
  357. strcpy (argv[c], "-w");
  358. else if (strcmp ("-ct", argv[c]) == 0)
  359. strcpy (argv[c], "-c");
  360. }
  361. if (!is_option (argv[1])) {
  362. server_address = argv[1];
  363. argv[1] = argv[0];
  364. argv = &argv[1];
  365. argc--;
  366. }
  367. while (1) {
  368. c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
  369. longopts, &option);
  370. if (c == -1 || c == EOF || c == 1)
  371. break;
  372. switch (c) {
  373. case '?': /* print short usage statement if args not parsable */
  374. usage2 (_("Unknown argument"), optarg);
  375. case 'h': /* help */
  376. print_help ();
  377. exit (STATE_OK);
  378. case 'V': /* version */
  379. print_revision (progname, revision);
  380. exit (STATE_OK);
  381. case 'v': /* verbose mode */
  382. flags |= FLAG_VERBOSE;
  383. break;
  384. case '4':
  385. address_family = AF_INET;
  386. break;
  387. case '6':
  388. #ifdef USE_IPV6
  389. address_family = AF_INET6;
  390. #else
  391. usage4 (_("IPv6 support not available"));
  392. #endif
  393. break;
  394. case 'H': /* hostname */
  395. if (is_host (optarg) == FALSE)
  396. usage2 (_("Invalid hostname/address"), optarg);
  397. server_address = optarg;
  398. break;
  399. case 'c': /* critical */
  400. if (!is_intnonneg (optarg))
  401. usage4 (_("Critical threshold must be a positive integer"));
  402. else
  403. critical_time = strtod (optarg, NULL);
  404. flags |= FLAG_TIME_CRIT;
  405. break;
  406. case 'j': /* hide output */
  407. flags |= FLAG_HIDE_OUTPUT;
  408. break;
  409. case 'w': /* warning */
  410. if (!is_intnonneg (optarg))
  411. usage4 (_("Warning threshold must be a positive integer"));
  412. else
  413. warning_time = strtod (optarg, NULL);
  414. flags |= FLAG_TIME_WARN;
  415. break;
  416. case 'C':
  417. crit_codes = realloc (crit_codes, ++crit_codes_count);
  418. crit_codes[crit_codes_count - 1] = optarg;
  419. break;
  420. case 'W':
  421. warn_codes = realloc (warn_codes, ++warn_codes_count);
  422. warn_codes[warn_codes_count - 1] = optarg;
  423. break;
  424. case 't': /* timeout */
  425. if (!is_intpos (optarg))
  426. usage4 (_("Timeout interval must be a positive integer"));
  427. else
  428. socket_timeout = atoi (optarg);
  429. break;
  430. case 'p': /* port */
  431. if (!is_intpos (optarg))
  432. usage4 (_("Port must be a positive integer"));
  433. else
  434. server_port = atoi (optarg);
  435. break;
  436. case 's':
  437. server_send = optarg;
  438. break;
  439. case 'e': /* expect string (may be repeated) */
  440. EXPECT = NULL;
  441. flags &= ~FLAG_EXACT_MATCH;
  442. if (server_expect_count == 0)
  443. server_expect = malloc (sizeof (char *) * (++server_expect_count));
  444. else
  445. server_expect = realloc (server_expect, sizeof (char *) * (++server_expect_count));
  446. server_expect[server_expect_count - 1] = optarg;
  447. break;
  448. case 'm':
  449. if (!is_intpos (optarg))
  450. usage4 (_("Maxbytes must be a positive integer"));
  451. else
  452. maxbytes = strtol (optarg, NULL, 0);
  453. case 'q':
  454. asprintf(&server_quit, "%s\r\n", optarg);
  455. break;
  456. case 'r':
  457. if (!strncmp(optarg,"ok",2))
  458. econn_refuse_state = STATE_OK;
  459. else if (!strncmp(optarg,"warn",4))
  460. econn_refuse_state = STATE_WARNING;
  461. else if (!strncmp(optarg,"crit",4))
  462. econn_refuse_state = STATE_CRITICAL;
  463. else
  464. usage4 (_("Refuse must be one of ok, warn, crit"));
  465. break;
  466. case 'M':
  467. if (!strncmp(optarg,"ok",2))
  468. expect_mismatch_state = STATE_OK;
  469. else if (!strncmp(optarg,"warn",4))
  470. expect_mismatch_state = STATE_WARNING;
  471. else if (!strncmp(optarg,"crit",4))
  472. expect_mismatch_state = STATE_CRITICAL;
  473. else
  474. usage4 (_("Mismatch must be one of ok, warn, crit"));
  475. break;
  476. case 'd':
  477. if (is_intpos (optarg))
  478. delay = atoi (optarg);
  479. else
  480. usage4 (_("Delay must be a positive integer"));
  481. break;
  482. case 'D': /* Check SSL cert validity - days 'til certificate expiration */
  483. #ifdef HAVE_SSL
  484. # ifdef USE_OPENSSL /* XXX */
  485. if (!is_intnonneg (optarg))
  486. usage2 (_("Invalid certificate expiration period"), optarg);
  487. days_till_exp = atoi (optarg);
  488. check_cert = TRUE;
  489. flags |= FLAG_SSL;
  490. break;
  491. # endif /* USE_OPENSSL */
  492. #endif
  493. /* fallthrough if we don't have ssl */
  494. case 'S':
  495. #ifdef HAVE_SSL
  496. flags |= FLAG_SSL;
  497. #else
  498. die (STATE_UNKNOWN, _("Invalid option - SSL is not available"));
  499. #endif
  500. break;
  501. }
  502. }
  503. if (server_address == NULL)
  504. usage4 (_("You must provide a server address"));
  505. return TRUE;
  506. }
  507. /* SSL-specific functions */
  508. #ifdef HAVE_SSL
  509. # ifdef USE_OPENSSL /* XXX */
  510. static int
  511. check_certificate (X509 ** certificate)
  512. {
  513. ASN1_STRING *tm;
  514. int offset;
  515. struct tm stamp;
  516. int days_left;
  517. /* Retrieve timestamp of certificate */
  518. tm = X509_get_notAfter (*certificate);
  519. /* Generate tm structure to process timestamp */
  520. if (tm->type == V_ASN1_UTCTIME) {
  521. if (tm->length < 10) {
  522. printf (_("CRITICAL - Wrong time format in certificate.\n"));
  523. return STATE_CRITICAL;
  524. }
  525. else {
  526. stamp.tm_year = (tm->data[0] - '0') * 10 + (tm->data[1] - '0');
  527. if (stamp.tm_year < 50)
  528. stamp.tm_year += 100;
  529. offset = 0;
  530. }
  531. }
  532. else {
  533. if (tm->length < 12) {
  534. printf (_("CRITICAL - Wrong time format in certificate.\n"));
  535. return STATE_CRITICAL;
  536. }
  537. else {
  538. stamp.tm_year =
  539. (tm->data[0] - '0') * 1000 + (tm->data[1] - '0') * 100 +
  540. (tm->data[2] - '0') * 10 + (tm->data[3] - '0');
  541. stamp.tm_year -= 1900;
  542. offset = 2;
  543. }
  544. }
  545. stamp.tm_mon =
  546. (tm->data[2 + offset] - '0') * 10 + (tm->data[3 + offset] - '0') - 1;
  547. stamp.tm_mday =
  548. (tm->data[4 + offset] - '0') * 10 + (tm->data[5 + offset] - '0');
  549. stamp.tm_hour =
  550. (tm->data[6 + offset] - '0') * 10 + (tm->data[7 + offset] - '0');
  551. stamp.tm_min =
  552. (tm->data[8 + offset] - '0') * 10 + (tm->data[9 + offset] - '0');
  553. stamp.tm_sec = 0;
  554. stamp.tm_isdst = -1;
  555. days_left = (mktime (&stamp) - time (NULL)) / 86400;
  556. snprintf
  557. (timestamp, 16, "%02d/%02d/%04d %02d:%02d",
  558. stamp.tm_mon + 1,
  559. stamp.tm_mday, stamp.tm_year + 1900, stamp.tm_hour, stamp.tm_min);
  560. if (days_left > 0 && days_left <= days_till_exp) {
  561. printf (_("Certificate expires in %d day(s) (%s).\n"), days_left, timestamp);
  562. return STATE_WARNING;
  563. }
  564. if (days_left < 0) {
  565. printf (_("Certificate expired on %s.\n"), timestamp);
  566. return STATE_CRITICAL;
  567. }
  568. if (days_left == 0) {
  569. printf (_("Certificate expires today (%s).\n"), timestamp);
  570. return STATE_WARNING;
  571. }
  572. printf (_("Certificate will expire on %s.\n"), timestamp);
  573. return STATE_OK;
  574. }
  575. # endif /* USE_OPENSSL */
  576. #endif /* HAVE_SSL */
  577. void
  578. print_help (void)
  579. {
  580. print_revision (progname, revision);
  581. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  582. printf (COPYRIGHT, copyright, email);
  583. printf (_("This plugin tests %s connections with the specified host.\n\n"),
  584. SERVICE);
  585. print_usage ();
  586. printf (_(UT_HELP_VRSN));
  587. printf (_(UT_HOST_PORT), 'p', "none");
  588. printf (_(UT_IPv46));
  589. printf (_("\
  590. -s, --send=STRING\n\
  591. String to send to the server\n\
  592. -e, --expect=STRING\n\
  593. String to expect in server response\n\
  594. -q, --quit=STRING\n\
  595. String to send server to initiate a clean close of the connection\n"));
  596. printf (_("\
  597. -r, --refuse=ok|warn|crit\n\
  598. Accept tcp refusals with states ok, warn, crit (default: crit)\n\
  599. -M, --mismatch=ok|warn|crit\n\
  600. Accept expected string mismatches with states ok, warn, crit (default: warn)\n\
  601. -j, --jail\n\
  602. Hide output from TCP socket\n\
  603. -m, --maxbytes=INTEGER\n\
  604. Close connection once more than this number of bytes are received\n\
  605. -d, --delay=INTEGER\n\
  606. Seconds to wait between sending string and polling for response\n"));
  607. #ifdef HAVE_SSL
  608. printf (_("\
  609. -D, --certificate=INTEGER\n\
  610. Minimum number of days a certificate has to be valid.\n\
  611. -S, --ssl\n\
  612. Use SSL for the connection.\n"));
  613. #endif
  614. printf (_(UT_WARN_CRIT));
  615. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  616. printf (_(UT_VERBOSE));
  617. printf (_(UT_SUPPORT));
  618. }
  619. void
  620. print_usage (void)
  621. {
  622. printf ("\
  623. Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
  624. [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
  625. [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
  626. [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
  627. [-D <days to cert expiry>] [-S <use SSL>]\n", progname);
  628. }