check_http.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*****************************************************************************
  2. *
  3. * Nagios check_http plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2008 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_http plugin
  13. *
  14. * This plugin tests the HTTP service on the specified host. It can test
  15. * normal (http) and secure (https) servers, follow redirects, search for
  16. * strings and regular expressions, check connection times, and report on
  17. * certificate expiration times.
  18. *
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation, either version 3 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32. *
  33. * $Id$
  34. *
  35. *****************************************************************************/
  36. /* splint -I. -I../../plugins -I../../lib/ -I/usr/kerberos/include/ ../../plugins/check_http.c */
  37. const char *progname = "check_http";
  38. const char *revision = "$Revision$";
  39. const char *copyright = "1999-2008";
  40. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  41. #include "common.h"
  42. #include "netutils.h"
  43. #include "utils.h"
  44. #include "base64.h"
  45. #include <ctype.h>
  46. #define INPUT_DELIMITER ";"
  47. #define HTTP_EXPECT "HTTP/1."
  48. enum {
  49. MAX_IPV4_HOSTLENGTH = 255,
  50. HTTP_PORT = 80,
  51. HTTPS_PORT = 443,
  52. MAX_PORT = 65535
  53. };
  54. #ifdef HAVE_SSL
  55. int check_cert = FALSE;
  56. int days_till_exp;
  57. char *randbuff;
  58. X509 *server_cert;
  59. # define my_recv(buf, len) ((use_ssl) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
  60. # define my_send(buf, len) ((use_ssl) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
  61. #else /* ifndef HAVE_SSL */
  62. # define my_recv(buf, len) read(sd, buf, len)
  63. # define my_send(buf, len) send(sd, buf, len, 0)
  64. #endif /* HAVE_SSL */
  65. int no_body = FALSE;
  66. int maximum_age = -1;
  67. enum {
  68. REGS = 2,
  69. MAX_RE_SIZE = 256
  70. };
  71. #include "regex.h"
  72. regex_t preg;
  73. regmatch_t pmatch[REGS];
  74. char regexp[MAX_RE_SIZE];
  75. char errbuf[MAX_INPUT_BUFFER];
  76. int cflags = REG_NOSUB | REG_EXTENDED | REG_NEWLINE;
  77. int errcode;
  78. int invert_regex = 0;
  79. struct timeval tv;
  80. #define HTTP_URL "/"
  81. #define CRLF "\r\n"
  82. int specify_port = FALSE;
  83. int server_port = HTTP_PORT;
  84. char server_port_text[6] = "";
  85. char server_type[6] = "http";
  86. char *server_address;
  87. char *host_name;
  88. char *server_url;
  89. char *user_agent;
  90. int server_url_length;
  91. int server_expect_yn = 0;
  92. char server_expect[MAX_INPUT_BUFFER] = HTTP_EXPECT;
  93. char string_expect[MAX_INPUT_BUFFER] = "";
  94. double warning_time = 0;
  95. int check_warning_time = FALSE;
  96. double critical_time = 0;
  97. int check_critical_time = FALSE;
  98. char user_auth[MAX_INPUT_BUFFER] = "";
  99. int display_html = FALSE;
  100. char **http_opt_headers;
  101. int http_opt_headers_count = 0;
  102. int onredirect = STATE_OK;
  103. int use_ssl = FALSE;
  104. int verbose = FALSE;
  105. int sd;
  106. int min_page_len = 0;
  107. int max_page_len = 0;
  108. int redir_depth = 0;
  109. int max_depth = 15;
  110. char *http_method;
  111. char *http_post_data;
  112. char *http_content_type;
  113. char buffer[MAX_INPUT_BUFFER];
  114. int process_arguments (int, char **);
  115. int check_http (void);
  116. void redir (char *pos, char *status_line);
  117. int server_type_check(const char *type);
  118. int server_port_check(int ssl_flag);
  119. char *perfd_time (double microsec);
  120. char *perfd_size (int page_len);
  121. void print_help (void);
  122. void print_usage (void);
  123. int
  124. main (int argc, char **argv)
  125. {
  126. int result = STATE_UNKNOWN;
  127. setlocale (LC_ALL, "");
  128. bindtextdomain (PACKAGE, LOCALEDIR);
  129. textdomain (PACKAGE);
  130. /* Set default URL. Must be malloced for subsequent realloc if --onredirect=follow */
  131. server_url = strdup(HTTP_URL);
  132. server_url_length = strlen(server_url);
  133. asprintf (&user_agent, "User-Agent: check_http/%s (nagios-plugins %s)",
  134. clean_revstring (revision), VERSION);
  135. /* Parse extra opts if any */
  136. argv=np_extra_opts (&argc, argv, progname);
  137. if (process_arguments (argc, argv) == ERROR)
  138. usage4 (_("Could not parse arguments"));
  139. if (display_html == TRUE)
  140. printf ("<A HREF=\"%s://%s:%d%s\" target=\"_blank\">",
  141. use_ssl ? "https" : "http", host_name ? host_name : server_address,
  142. server_port, server_url);
  143. /* initialize alarm signal handling, set socket timeout, start timer */
  144. (void) signal (SIGALRM, socket_timeout_alarm_handler);
  145. (void) alarm (socket_timeout);
  146. gettimeofday (&tv, NULL);
  147. result = check_http ();
  148. return result;
  149. }
  150. /* process command-line arguments */
  151. int
  152. process_arguments (int argc, char **argv)
  153. {
  154. int c = 1;
  155. char *p;
  156. enum {
  157. INVERT_REGEX = CHAR_MAX + 1
  158. };
  159. int option = 0;
  160. static struct option longopts[] = {
  161. STD_LONG_OPTS,
  162. {"link", no_argument, 0, 'L'},
  163. {"nohtml", no_argument, 0, 'n'},
  164. {"ssl", no_argument, 0, 'S'},
  165. {"post", required_argument, 0, 'P'},
  166. {"method", required_argument, 0, 'j'},
  167. {"IP-address", required_argument, 0, 'I'},
  168. {"url", required_argument, 0, 'u'},
  169. {"port", required_argument, 0, 'p'},
  170. {"authorization", required_argument, 0, 'a'},
  171. {"string", required_argument, 0, 's'},
  172. {"expect", required_argument, 0, 'e'},
  173. {"regex", required_argument, 0, 'r'},
  174. {"ereg", required_argument, 0, 'r'},
  175. {"eregi", required_argument, 0, 'R'},
  176. {"linespan", no_argument, 0, 'l'},
  177. {"onredirect", required_argument, 0, 'f'},
  178. {"certificate", required_argument, 0, 'C'},
  179. {"useragent", required_argument, 0, 'A'},
  180. {"header", required_argument, 0, 'k'},
  181. {"no-body", no_argument, 0, 'N'},
  182. {"max-age", required_argument, 0, 'M'},
  183. {"content-type", required_argument, 0, 'T'},
  184. {"pagesize", required_argument, 0, 'm'},
  185. {"invert-regex", no_argument, NULL, INVERT_REGEX},
  186. {"use-ipv4", no_argument, 0, '4'},
  187. {"use-ipv6", no_argument, 0, '6'},
  188. {0, 0, 0, 0}
  189. };
  190. if (argc < 2)
  191. return ERROR;
  192. for (c = 1; c < argc; c++) {
  193. if (strcmp ("-to", argv[c]) == 0)
  194. strcpy (argv[c], "-t");
  195. if (strcmp ("-hn", argv[c]) == 0)
  196. strcpy (argv[c], "-H");
  197. if (strcmp ("-wt", argv[c]) == 0)
  198. strcpy (argv[c], "-w");
  199. if (strcmp ("-ct", argv[c]) == 0)
  200. strcpy (argv[c], "-c");
  201. if (strcmp ("-nohtml", argv[c]) == 0)
  202. strcpy (argv[c], "-n");
  203. }
  204. while (1) {
  205. c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:e:p:s:R:r:u:f:C:nlLSm:M:N", longopts, &option);
  206. if (c == -1 || c == EOF)
  207. break;
  208. switch (c) {
  209. case '?': /* usage */
  210. usage5 ();
  211. break;
  212. case 'h': /* help */
  213. print_help ();
  214. exit (STATE_OK);
  215. break;
  216. case 'V': /* version */
  217. print_revision (progname, revision);
  218. exit (STATE_OK);
  219. break;
  220. case 't': /* timeout period */
  221. if (!is_intnonneg (optarg))
  222. usage2 (_("Timeout interval must be a positive integer"), optarg);
  223. else
  224. socket_timeout = atoi (optarg);
  225. break;
  226. case 'c': /* critical time threshold */
  227. if (!is_nonnegative (optarg))
  228. usage2 (_("Critical threshold must be integer"), optarg);
  229. else {
  230. critical_time = strtod (optarg, NULL);
  231. check_critical_time = TRUE;
  232. }
  233. break;
  234. case 'w': /* warning time threshold */
  235. if (!is_nonnegative (optarg))
  236. usage2 (_("Warning threshold must be integer"), optarg);
  237. else {
  238. warning_time = strtod (optarg, NULL);
  239. check_warning_time = TRUE;
  240. }
  241. break;
  242. case 'A': /* User Agent String */
  243. asprintf (&user_agent, "User-Agent: %s", optarg);
  244. break;
  245. case 'k': /* Additional headers */
  246. if (http_opt_headers_count == 0)
  247. http_opt_headers = malloc (sizeof (char *) * (++http_opt_headers_count));
  248. else
  249. http_opt_headers = realloc (http_opt_headers, sizeof (char *) * (++http_opt_headers_count));
  250. http_opt_headers[http_opt_headers_count - 1] = optarg;
  251. /* asprintf (&http_opt_headers, "%s", optarg); */
  252. break;
  253. case 'L': /* show html link */
  254. display_html = TRUE;
  255. break;
  256. case 'n': /* do not show html link */
  257. display_html = FALSE;
  258. break;
  259. case 'C': /* Check SSL cert validity */
  260. #ifdef HAVE_SSL
  261. if (!is_intnonneg (optarg))
  262. usage2 (_("Invalid certificate expiration period"), optarg);
  263. else {
  264. days_till_exp = atoi (optarg);
  265. check_cert = TRUE;
  266. }
  267. /* Fall through to -S option */
  268. #endif
  269. case 'S': /* use SSL */
  270. #ifndef HAVE_SSL
  271. usage4 (_("Invalid option - SSL is not available"));
  272. #endif
  273. use_ssl = TRUE;
  274. if (specify_port == FALSE)
  275. server_port = HTTPS_PORT;
  276. break;
  277. case 'f': /* onredirect */
  278. if (!strcmp (optarg, "follow"))
  279. onredirect = STATE_DEPENDENT;
  280. if (!strcmp (optarg, "unknown"))
  281. onredirect = STATE_UNKNOWN;
  282. if (!strcmp (optarg, "ok"))
  283. onredirect = STATE_OK;
  284. if (!strcmp (optarg, "warning"))
  285. onredirect = STATE_WARNING;
  286. if (!strcmp (optarg, "critical"))
  287. onredirect = STATE_CRITICAL;
  288. if (verbose)
  289. printf(_("option f:%d \n"), onredirect);
  290. break;
  291. /* Note: H, I, and u must be malloc'd or will fail on redirects */
  292. case 'H': /* Host Name (virtual host) */
  293. host_name = strdup (optarg);
  294. if (host_name[0] == '[') {
  295. if ((p = strstr (host_name, "]:")) != NULL) /* [IPv6]:port */
  296. server_port = atoi (p + 2);
  297. } else if ((p = strchr (host_name, ':')) != NULL
  298. && strchr (++p, ':') == NULL) /* IPv4:port or host:port */
  299. server_port = atoi (p);
  300. break;
  301. case 'I': /* Server IP-address */
  302. server_address = strdup (optarg);
  303. break;
  304. case 'u': /* URL path */
  305. server_url = strdup (optarg);
  306. server_url_length = strlen (server_url);
  307. break;
  308. case 'p': /* Server port */
  309. if (!is_intnonneg (optarg))
  310. usage2 (_("Invalid port number"), optarg);
  311. else {
  312. server_port = atoi (optarg);
  313. specify_port = TRUE;
  314. }
  315. break;
  316. case 'a': /* authorization info */
  317. strncpy (user_auth, optarg, MAX_INPUT_BUFFER - 1);
  318. user_auth[MAX_INPUT_BUFFER - 1] = 0;
  319. break;
  320. case 'P': /* HTTP POST data in URL encoded format; ignored if settings already */
  321. if (! http_post_data)
  322. http_post_data = strdup (optarg);
  323. if (! http_method)
  324. http_method = strdup("POST");
  325. break;
  326. case 'j': /* Set HTTP method */
  327. if (http_method)
  328. free(http_method);
  329. http_method = strdup (optarg);
  330. break;
  331. case 's': /* string or substring */
  332. strncpy (string_expect, optarg, MAX_INPUT_BUFFER - 1);
  333. string_expect[MAX_INPUT_BUFFER - 1] = 0;
  334. break;
  335. case 'e': /* string or substring */
  336. strncpy (server_expect, optarg, MAX_INPUT_BUFFER - 1);
  337. server_expect[MAX_INPUT_BUFFER - 1] = 0;
  338. server_expect_yn = 1;
  339. break;
  340. case 'T': /* Content-type */
  341. asprintf (&http_content_type, "%s", optarg);
  342. break;
  343. case 'l': /* linespan */
  344. cflags &= ~REG_NEWLINE;
  345. break;
  346. case 'R': /* regex */
  347. cflags |= REG_ICASE;
  348. case 'r': /* regex */
  349. strncpy (regexp, optarg, MAX_RE_SIZE - 1);
  350. regexp[MAX_RE_SIZE - 1] = 0;
  351. errcode = regcomp (&preg, regexp, cflags);
  352. if (errcode != 0) {
  353. (void) regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  354. printf (_("Could Not Compile Regular Expression: %s"), errbuf);
  355. return ERROR;
  356. }
  357. break;
  358. case INVERT_REGEX:
  359. invert_regex = 1;
  360. break;
  361. case '4':
  362. address_family = AF_INET;
  363. break;
  364. case '6':
  365. #ifdef USE_IPV6
  366. address_family = AF_INET6;
  367. #else
  368. usage4 (_("IPv6 support not available"));
  369. #endif
  370. break;
  371. case 'v': /* verbose */
  372. verbose = TRUE;
  373. break;
  374. case 'm': /* min_page_length */
  375. {
  376. char *tmp;
  377. if (strchr(optarg, ':') != (char *)NULL) {
  378. /* range, so get two values, min:max */
  379. tmp = strtok(optarg, ":");
  380. if (tmp == NULL) {
  381. printf("Bad format: try \"-m min:max\"\n");
  382. exit (STATE_WARNING);
  383. } else
  384. min_page_len = atoi(tmp);
  385. tmp = strtok(NULL, ":");
  386. if (tmp == NULL) {
  387. printf("Bad format: try \"-m min:max\"\n");
  388. exit (STATE_WARNING);
  389. } else
  390. max_page_len = atoi(tmp);
  391. } else
  392. min_page_len = atoi (optarg);
  393. break;
  394. }
  395. case 'N': /* no-body */
  396. no_body = TRUE;
  397. break;
  398. case 'M': /* max-age */
  399. {
  400. int L = strlen(optarg);
  401. if (L && optarg[L-1] == 'm')
  402. maximum_age = atoi (optarg) * 60;
  403. else if (L && optarg[L-1] == 'h')
  404. maximum_age = atoi (optarg) * 60 * 60;
  405. else if (L && optarg[L-1] == 'd')
  406. maximum_age = atoi (optarg) * 60 * 60 * 24;
  407. else if (L && (optarg[L-1] == 's' ||
  408. isdigit (optarg[L-1])))
  409. maximum_age = atoi (optarg);
  410. else {
  411. fprintf (stderr, "unparsable max-age: %s\n", optarg);
  412. exit (STATE_WARNING);
  413. }
  414. }
  415. break;
  416. }
  417. }
  418. c = optind;
  419. if (server_address == NULL && c < argc)
  420. server_address = strdup (argv[c++]);
  421. if (host_name == NULL && c < argc)
  422. host_name = strdup (argv[c++]);
  423. if (server_address == NULL) {
  424. if (host_name == NULL)
  425. usage4 (_("You must specify a server address or host name"));
  426. else
  427. server_address = strdup (host_name);
  428. }
  429. if (check_critical_time && critical_time>(double)socket_timeout)
  430. socket_timeout = (int)critical_time + 1;
  431. if (http_method == NULL)
  432. http_method = strdup ("GET");
  433. return TRUE;
  434. }
  435. /* Returns 1 if we're done processing the document body; 0 to keep going */
  436. static int
  437. document_headers_done (char *full_page)
  438. {
  439. const char *body;
  440. for (body = full_page; *body; body++) {
  441. if (!strncmp (body, "\n\n", 2) || !strncmp (body, "\n\r\n", 3))
  442. break;
  443. }
  444. if (!*body)
  445. return 0; /* haven't read end of headers yet */
  446. full_page[body - full_page] = 0;
  447. return 1;
  448. }
  449. static time_t
  450. parse_time_string (const char *string)
  451. {
  452. struct tm tm;
  453. time_t t;
  454. memset (&tm, 0, sizeof(tm));
  455. /* Like this: Tue, 25 Dec 2001 02:59:03 GMT */
  456. if (isupper (string[0]) && /* Tue */
  457. islower (string[1]) &&
  458. islower (string[2]) &&
  459. ',' == string[3] &&
  460. ' ' == string[4] &&
  461. (isdigit(string[5]) || string[5] == ' ') && /* 25 */
  462. isdigit (string[6]) &&
  463. ' ' == string[7] &&
  464. isupper (string[8]) && /* Dec */
  465. islower (string[9]) &&
  466. islower (string[10]) &&
  467. ' ' == string[11] &&
  468. isdigit (string[12]) && /* 2001 */
  469. isdigit (string[13]) &&
  470. isdigit (string[14]) &&
  471. isdigit (string[15]) &&
  472. ' ' == string[16] &&
  473. isdigit (string[17]) && /* 02: */
  474. isdigit (string[18]) &&
  475. ':' == string[19] &&
  476. isdigit (string[20]) && /* 59: */
  477. isdigit (string[21]) &&
  478. ':' == string[22] &&
  479. isdigit (string[23]) && /* 03 */
  480. isdigit (string[24]) &&
  481. ' ' == string[25] &&
  482. 'G' == string[26] && /* GMT */
  483. 'M' == string[27] && /* GMT */
  484. 'T' == string[28]) {
  485. tm.tm_sec = 10 * (string[23]-'0') + (string[24]-'0');
  486. tm.tm_min = 10 * (string[20]-'0') + (string[21]-'0');
  487. tm.tm_hour = 10 * (string[17]-'0') + (string[18]-'0');
  488. tm.tm_mday = 10 * (string[5] == ' ' ? 0 : string[5]-'0') + (string[6]-'0');
  489. tm.tm_mon = (!strncmp (string+8, "Jan", 3) ? 0 :
  490. !strncmp (string+8, "Feb", 3) ? 1 :
  491. !strncmp (string+8, "Mar", 3) ? 2 :
  492. !strncmp (string+8, "Apr", 3) ? 3 :
  493. !strncmp (string+8, "May", 3) ? 4 :
  494. !strncmp (string+8, "Jun", 3) ? 5 :
  495. !strncmp (string+8, "Jul", 3) ? 6 :
  496. !strncmp (string+8, "Aug", 3) ? 7 :
  497. !strncmp (string+8, "Sep", 3) ? 8 :
  498. !strncmp (string+8, "Oct", 3) ? 9 :
  499. !strncmp (string+8, "Nov", 3) ? 10 :
  500. !strncmp (string+8, "Dec", 3) ? 11 :
  501. -1);
  502. tm.tm_year = ((1000 * (string[12]-'0') +
  503. 100 * (string[13]-'0') +
  504. 10 * (string[14]-'0') +
  505. (string[15]-'0'))
  506. - 1900);
  507. tm.tm_isdst = 0; /* GMT is never in DST, right? */
  508. if (tm.tm_mon < 0 || tm.tm_mday < 1 || tm.tm_mday > 31)
  509. return 0;
  510. /*
  511. This is actually wrong: we need to subtract the local timezone
  512. offset from GMT from this value. But, that's ok in this usage,
  513. because we only comparing these two GMT dates against each other,
  514. so it doesn't matter what time zone we parse them in.
  515. */
  516. t = mktime (&tm);
  517. if (t == (time_t) -1) t = 0;
  518. if (verbose) {
  519. const char *s = string;
  520. while (*s && *s != '\r' && *s != '\n')
  521. fputc (*s++, stdout);
  522. printf (" ==> %lu\n", (unsigned long) t);
  523. }
  524. return t;
  525. } else {
  526. return 0;
  527. }
  528. }
  529. /* Checks if the server 'reply' is one of the expected 'statuscodes' */
  530. static int
  531. expected_statuscode (const char *reply, const char *statuscodes)
  532. {
  533. char *expected, *code;
  534. int result = 0;
  535. if ((expected = strdup (statuscodes)) == NULL)
  536. die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n"));
  537. for (code = strtok (expected, ","); code != NULL; code = strtok (NULL, ","))
  538. if (strstr (reply, code) != NULL) {
  539. result = 1;
  540. break;
  541. }
  542. free (expected);
  543. return result;
  544. }
  545. static void
  546. check_document_dates (const char *headers)
  547. {
  548. const char *s;
  549. char *server_date = 0;
  550. char *document_date = 0;
  551. s = headers;
  552. while (*s) {
  553. const char *field = s;
  554. const char *value = 0;
  555. /* Find the end of the header field */
  556. while (*s && !isspace(*s) && *s != ':')
  557. s++;
  558. /* Remember the header value, if any. */
  559. if (*s == ':')
  560. value = ++s;
  561. /* Skip to the end of the header, including continuation lines. */
  562. while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t')))
  563. s++;
  564. /* Avoid stepping over end-of-string marker */
  565. if (*s)
  566. s++;
  567. /* Process this header. */
  568. if (value && value > field+2) {
  569. char *ff = (char *) malloc (value-field);
  570. char *ss = ff;
  571. while (field < value-1)
  572. *ss++ = tolower(*field++);
  573. *ss++ = 0;
  574. if (!strcmp (ff, "date") || !strcmp (ff, "last-modified")) {
  575. const char *e;
  576. while (*value && isspace (*value))
  577. value++;
  578. for (e = value; *e && *e != '\r' && *e != '\n'; e++)
  579. ;
  580. ss = (char *) malloc (e - value + 1);
  581. strncpy (ss, value, e - value);
  582. ss[e - value] = 0;
  583. if (!strcmp (ff, "date")) {
  584. if (server_date) free (server_date);
  585. server_date = ss;
  586. } else {
  587. if (document_date) free (document_date);
  588. document_date = ss;
  589. }
  590. }
  591. free (ff);
  592. }
  593. }
  594. /* Done parsing the body. Now check the dates we (hopefully) parsed. */
  595. if (!server_date || !*server_date) {
  596. die (STATE_UNKNOWN, _("HTTP UNKNOWN - Server date unknown\n"));
  597. } else if (!document_date || !*document_date) {
  598. die (STATE_CRITICAL, _("HTTP CRITICAL - Document modification date unknown\n"));
  599. } else {
  600. time_t srv_data = parse_time_string (server_date);
  601. time_t doc_data = parse_time_string (document_date);
  602. if (srv_data <= 0) {
  603. die (STATE_CRITICAL, _("HTTP CRITICAL - Server date \"%100s\" unparsable"), server_date);
  604. } else if (doc_data <= 0) {
  605. die (STATE_CRITICAL, _("HTTP CRITICAL - Document date \"%100s\" unparsable"), document_date);
  606. } else if (doc_data > srv_data + 30) {
  607. die (STATE_CRITICAL, _("HTTP CRITICAL - Document is %d seconds in the future\n"), (int)doc_data - (int)srv_data);
  608. } else if (doc_data < srv_data - maximum_age) {
  609. int n = (srv_data - doc_data);
  610. if (n > (60 * 60 * 24 * 2))
  611. die (STATE_CRITICAL,
  612. _("HTTP CRITICAL - Last modified %.1f days ago\n"),
  613. ((float) n) / (60 * 60 * 24));
  614. else
  615. die (STATE_CRITICAL,
  616. _("HTTP CRITICAL - Last modified %d:%02d:%02d ago\n"),
  617. n / (60 * 60), (n / 60) % 60, n % 60);
  618. }
  619. free (server_date);
  620. free (document_date);
  621. }
  622. }
  623. int
  624. get_content_length (const char *headers)
  625. {
  626. const char *s;
  627. int content_length = 0;
  628. s = headers;
  629. while (*s) {
  630. const char *field = s;
  631. const char *value = 0;
  632. /* Find the end of the header field */
  633. while (*s && !isspace(*s) && *s != ':')
  634. s++;
  635. /* Remember the header value, if any. */
  636. if (*s == ':')
  637. value = ++s;
  638. /* Skip to the end of the header, including continuation lines. */
  639. while (*s && !(*s == '\n' && (s[1] != ' ' && s[1] != '\t')))
  640. s++;
  641. s++;
  642. /* Process this header. */
  643. if (value && value > field+2) {
  644. char *ff = (char *) malloc (value-field);
  645. char *ss = ff;
  646. while (field < value-1)
  647. *ss++ = tolower(*field++);
  648. *ss++ = 0;
  649. if (!strcmp (ff, "content-length")) {
  650. const char *e;
  651. while (*value && isspace (*value))
  652. value++;
  653. for (e = value; *e && *e != '\r' && *e != '\n'; e++)
  654. ;
  655. ss = (char *) malloc (e - value + 1);
  656. strncpy (ss, value, e - value);
  657. ss[e - value] = 0;
  658. content_length = atoi(ss);
  659. free (ss);
  660. }
  661. free (ff);
  662. }
  663. }
  664. return (content_length);
  665. }
  666. char *
  667. prepend_slash (char *path)
  668. {
  669. char *newpath;
  670. if (path[0] == '/')
  671. return path;
  672. if ((newpath = malloc (strlen(path) + 2)) == NULL)
  673. die (STATE_UNKNOWN, _("HTTP UNKNOWN - Memory allocation error\n"));
  674. newpath[0] = '/';
  675. strcpy (newpath + 1, path);
  676. free (path);
  677. return newpath;
  678. }
  679. int
  680. check_http (void)
  681. {
  682. char *msg;
  683. char *status_line;
  684. char *status_code;
  685. char *header;
  686. char *page;
  687. char *auth;
  688. int http_status;
  689. int i = 0;
  690. size_t pagesize = 0;
  691. char *full_page;
  692. char *buf;
  693. char *pos;
  694. long microsec;
  695. double elapsed_time;
  696. int page_len = 0;
  697. int result = STATE_UNKNOWN;
  698. /* try to connect to the host at the given port number */
  699. if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
  700. die (STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n"));
  701. #ifdef HAVE_SSL
  702. if (use_ssl == TRUE) {
  703. np_net_ssl_init(sd);
  704. if (check_cert == TRUE) {
  705. result = np_net_ssl_check_cert(days_till_exp);
  706. np_net_ssl_cleanup();
  707. if (sd) close(sd);
  708. return result;
  709. }
  710. }
  711. #endif /* HAVE_SSL */
  712. asprintf (&buf, "%s %s HTTP/1.0\r\n%s\r\n", http_method, server_url, user_agent);
  713. /* tell HTTP/1.1 servers not to keep the connection alive */
  714. asprintf (&buf, "%sConnection: close\r\n", buf);
  715. /* optionally send the host header info */
  716. if (host_name) {
  717. /*
  718. * Specify the port only if we're using a non-default port (see RFC 2616,
  719. * 14.23). Some server applications/configurations cause trouble if the
  720. * (default) port is explicitly specified in the "Host:" header line.
  721. */
  722. if ((use_ssl == FALSE && server_port == HTTP_PORT) ||
  723. (use_ssl == TRUE && server_port == HTTPS_PORT))
  724. asprintf (&buf, "%sHost: %s\r\n", buf, host_name);
  725. else
  726. asprintf (&buf, "%sHost: %s:%d\r\n", buf, host_name, server_port);
  727. }
  728. /* optionally send any other header tag */
  729. if (http_opt_headers_count) {
  730. for (i = 0; i < http_opt_headers_count ; i++) {
  731. for ((pos = strtok(http_opt_headers[i], INPUT_DELIMITER)); pos; (pos = strtok(NULL, INPUT_DELIMITER)))
  732. asprintf (&buf, "%s%s\r\n", buf, pos);
  733. }
  734. /* This cannot be free'd here because a redirection will then try to access this and segfault */
  735. /* Covered in a testcase in tests/check_http.t */
  736. /* free(http_opt_headers); */
  737. }
  738. /* optionally send the authentication info */
  739. if (strlen(user_auth)) {
  740. base64_encode_alloc (user_auth, strlen (user_auth), &auth);
  741. asprintf (&buf, "%sAuthorization: Basic %s\r\n", buf, auth);
  742. }
  743. /* either send http POST data (any data, not only POST)*/
  744. if (http_post_data) {
  745. if (http_content_type) {
  746. asprintf (&buf, "%sContent-Type: %s\r\n", buf, http_content_type);
  747. } else {
  748. asprintf (&buf, "%sContent-Type: application/x-www-form-urlencoded\r\n", buf);
  749. }
  750. asprintf (&buf, "%sContent-Length: %i\r\n\r\n", buf, (int)strlen (http_post_data));
  751. asprintf (&buf, "%s%s%s", buf, http_post_data, CRLF);
  752. }
  753. else {
  754. /* or just a newline so the server knows we're done with the request */
  755. asprintf (&buf, "%s%s", buf, CRLF);
  756. }
  757. if (verbose) printf ("%s\n", buf);
  758. my_send (buf, strlen (buf));
  759. /* fetch the page */
  760. full_page = strdup("");
  761. while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) {
  762. buffer[i] = '\0';
  763. asprintf (&full_page, "%s%s", full_page, buffer);
  764. pagesize += i;
  765. if (no_body && document_headers_done (full_page)) {
  766. i = 0;
  767. break;
  768. }
  769. }
  770. if (i < 0 && errno != ECONNRESET) {
  771. #ifdef HAVE_SSL
  772. /*
  773. if (use_ssl) {
  774. sslerr=SSL_get_error(ssl, i);
  775. if ( sslerr == SSL_ERROR_SSL ) {
  776. die (STATE_WARNING, _("HTTP WARNING - Client Certificate Required\n"));
  777. } else {
  778. die (STATE_CRITICAL, _("HTTP CRITICAL - Error on receive\n"));
  779. }
  780. }
  781. else {
  782. */
  783. #endif
  784. die (STATE_CRITICAL, _("HTTP CRITICAL - Error on receive\n"));
  785. #ifdef HAVE_SSL
  786. /* XXX
  787. }
  788. */
  789. #endif
  790. }
  791. /* return a CRITICAL status if we couldn't read any data */
  792. if (pagesize == (size_t) 0)
  793. die (STATE_CRITICAL, _("HTTP CRITICAL - No data received from host\n"));
  794. /* close the connection */
  795. #ifdef HAVE_SSL
  796. np_net_ssl_cleanup();
  797. #endif
  798. if (sd) close(sd);
  799. /* reset the alarm */
  800. alarm (0);
  801. /* leave full_page untouched so we can free it later */
  802. page = full_page;
  803. if (verbose)
  804. printf ("%s://%s:%d%s is %d characters\n",
  805. use_ssl ? "https" : "http", server_address,
  806. server_port, server_url, (int)pagesize);
  807. /* find status line and null-terminate it */
  808. status_line = page;
  809. page += (size_t) strcspn (page, "\r\n");
  810. pos = page;
  811. page += (size_t) strspn (page, "\r\n");
  812. status_line[strcspn(status_line, "\r\n")] = 0;
  813. strip (status_line);
  814. if (verbose)
  815. printf ("STATUS: %s\n", status_line);
  816. /* find header info and null-terminate it */
  817. header = page;
  818. while (strcspn (page, "\r\n") > 0) {
  819. page += (size_t) strcspn (page, "\r\n");
  820. pos = page;
  821. if ((strspn (page, "\r") == 1 && strspn (page, "\r\n") >= 2) ||
  822. (strspn (page, "\n") == 1 && strspn (page, "\r\n") >= 2))
  823. page += (size_t) 2;
  824. else
  825. page += (size_t) 1;
  826. }
  827. page += (size_t) strspn (page, "\r\n");
  828. header[pos - header] = 0;
  829. if (verbose)
  830. printf ("**** HEADER ****\n%s\n**** CONTENT ****\n%s\n", header,
  831. (no_body ? " [[ skipped ]]" : page));
  832. /* make sure the status line matches the response we are looking for */
  833. if (!expected_statuscode (status_line, server_expect)) {
  834. if (server_port == HTTP_PORT)
  835. asprintf (&msg,
  836. _("Invalid HTTP response received from host: %s\n"),
  837. status_line);
  838. else
  839. asprintf (&msg,
  840. _("Invalid HTTP response received from host on port %d: %s\n"),
  841. server_port, status_line);
  842. die (STATE_CRITICAL, "HTTP CRITICAL - %s", msg);
  843. }
  844. /* Exit here if server_expect was set by user and not default */
  845. if ( server_expect_yn ) {
  846. asprintf (&msg,
  847. _("HTTP OK: Status line output matched \"%s\"\n"),
  848. server_expect);
  849. if (verbose)
  850. printf ("%s\n",msg);
  851. }
  852. else {
  853. /* Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF */
  854. /* HTTP-Version = "HTTP" "/" 1*DIGIT "." 1*DIGIT */
  855. /* Status-Code = 3 DIGITS */
  856. status_code = strchr (status_line, ' ') + sizeof (char);
  857. if (strspn (status_code, "1234567890") != 3)
  858. die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status Line (%s)\n"), status_line);
  859. http_status = atoi (status_code);
  860. /* check the return code */
  861. if (http_status >= 600 || http_status < 100)
  862. die (STATE_CRITICAL, _("HTTP CRITICAL: Invalid Status (%s)\n"), status_line);
  863. /* server errors result in a critical state */
  864. else if (http_status >= 500)
  865. die (STATE_CRITICAL, _("HTTP CRITICAL: %s\n"), status_line);
  866. /* client errors result in a warning state */
  867. else if (http_status >= 400)
  868. die (STATE_WARNING, _("HTTP WARNING: %s\n"), status_line);
  869. /* check redirected page if specified */
  870. else if (http_status >= 300) {
  871. if (onredirect == STATE_DEPENDENT)
  872. redir (header, status_line);
  873. else if (onredirect == STATE_UNKNOWN)
  874. printf (_("HTTP UNKNOWN"));
  875. else if (onredirect == STATE_OK)
  876. printf (_("HTTP OK"));
  877. else if (onredirect == STATE_WARNING)
  878. printf (_("HTTP WARNING"));
  879. else if (onredirect == STATE_CRITICAL)
  880. printf (_("HTTP CRITICAL"));
  881. microsec = deltime (tv);
  882. elapsed_time = (double)microsec / 1.0e6;
  883. die (onredirect,
  884. _(" - %s - %.3f second response time %s|%s %s\n"),
  885. status_line, elapsed_time,
  886. (display_html ? "</A>" : ""),
  887. perfd_time (elapsed_time), perfd_size (pagesize));
  888. } /* end if (http_status >= 300) */
  889. } /* end else (server_expect_yn) */
  890. if (maximum_age >= 0) {
  891. check_document_dates (header);
  892. }
  893. /* check elapsed time */
  894. microsec = deltime (tv);
  895. elapsed_time = (double)microsec / 1.0e6;
  896. asprintf (&msg,
  897. _(" - %s - %.3f second response time %s|%s %s\n"),
  898. status_line, elapsed_time,
  899. (display_html ? "</A>" : ""),
  900. perfd_time (elapsed_time), perfd_size (pagesize));
  901. if (check_critical_time == TRUE && elapsed_time > critical_time)
  902. die (STATE_CRITICAL, "HTTP %s: %s", _("CRITICAL"), msg);
  903. if (check_warning_time == TRUE && elapsed_time > warning_time)
  904. die (STATE_WARNING, "HTTP %s: %s", _("WARNING"), msg);
  905. /* Page and Header content checks go here */
  906. /* these checks should be last */
  907. if (strlen (string_expect)) {
  908. if (strstr (page, string_expect)) {
  909. printf (_("HTTP OK %s - %.3f second response time %s|%s %s\n"),
  910. status_line, elapsed_time,
  911. (display_html ? "</A>" : ""),
  912. perfd_time (elapsed_time), perfd_size (pagesize));
  913. exit (STATE_OK);
  914. }
  915. else {
  916. printf (_("HTTP CRITICAL - string not found%s|%s %s\n"),
  917. (display_html ? "</A>" : ""),
  918. perfd_time (elapsed_time), perfd_size (pagesize));
  919. exit (STATE_CRITICAL);
  920. }
  921. }
  922. if (strlen (regexp)) {
  923. errcode = regexec (&preg, page, REGS, pmatch, 0);
  924. if ((errcode == 0 && invert_regex == 0) || (errcode == REG_NOMATCH && invert_regex == 1)) {
  925. printf (_("HTTP OK %s - %.3f second response time %s|%s %s\n"),
  926. status_line, elapsed_time,
  927. (display_html ? "</A>" : ""),
  928. perfd_time (elapsed_time), perfd_size (pagesize));
  929. exit (STATE_OK);
  930. }
  931. else if ((errcode == REG_NOMATCH && invert_regex == 0) || (errcode == 0 && invert_regex == 1)) {
  932. if (invert_regex == 0)
  933. msg = strdup(_("pattern not found"));
  934. else
  935. msg = strdup(_("pattern found"));
  936. printf (("%s - %s%s|%s %s\n"),
  937. _("HTTP CRITICAL"),
  938. msg,
  939. (display_html ? "</A>" : ""),
  940. perfd_time (elapsed_time), perfd_size (pagesize));
  941. exit (STATE_CRITICAL);
  942. }
  943. else {
  944. regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
  945. printf (_("HTTP CRITICAL - Execute Error: %s\n"), errbuf);
  946. exit (STATE_CRITICAL);
  947. }
  948. }
  949. /* make sure the page is of an appropriate size */
  950. /* page_len = get_content_length(header); */
  951. page_len = pagesize;
  952. if ((max_page_len > 0) && (page_len > max_page_len)) {
  953. printf (_("HTTP WARNING: page size %d too large%s|%s\n"),
  954. page_len, (display_html ? "</A>" : ""), perfd_size (page_len) );
  955. exit (STATE_WARNING);
  956. } else if ((min_page_len > 0) && (page_len < min_page_len)) {
  957. printf (_("HTTP WARNING: page size %d too small%s|%s\n"),
  958. page_len, (display_html ? "</A>" : ""), perfd_size (page_len) );
  959. exit (STATE_WARNING);
  960. }
  961. /* We only get here if all tests have been passed */
  962. asprintf (&msg, _("HTTP OK %s - %d bytes in %.3f seconds %s|%s %s\n"),
  963. status_line, page_len, elapsed_time,
  964. (display_html ? "</A>" : ""),
  965. perfd_time (elapsed_time), perfd_size (page_len));
  966. die (STATE_OK, "%s", msg);
  967. return STATE_UNKNOWN;
  968. }
  969. /* per RFC 2396 */
  970. #define URI_HTTP "%5[HTPShtps]"
  971. #define URI_HOST "%255[-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
  972. #define URI_PORT "%6d" /* MAX_PORT's width is 5 chars, 6 to detect overflow */
  973. #define URI_PATH "%[-_.!~*'();/?:@&=+$,%#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]"
  974. #define HD1 URI_HTTP "://" URI_HOST ":" URI_PORT "/" URI_PATH
  975. #define HD2 URI_HTTP "://" URI_HOST "/" URI_PATH
  976. #define HD3 URI_HTTP "://" URI_HOST ":" URI_PORT
  977. #define HD4 URI_HTTP "://" URI_HOST
  978. #define HD5 URI_PATH
  979. void
  980. redir (char *pos, char *status_line)
  981. {
  982. int i = 0;
  983. char *x;
  984. char xx[2];
  985. char type[6];
  986. char *addr;
  987. char *url;
  988. addr = malloc (MAX_IPV4_HOSTLENGTH + 1);
  989. if (addr == NULL)
  990. die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate addr\n"));
  991. url = malloc (strcspn (pos, "\r\n"));
  992. if (url == NULL)
  993. die (STATE_UNKNOWN, _("HTTP UNKNOWN - Could not allocate url\n"));
  994. while (pos) {
  995. sscanf (pos, "%1[Ll]%*1[Oo]%*1[Cc]%*1[Aa]%*1[Tt]%*1[Ii]%*1[Oo]%*1[Nn]:%n", xx, &i);
  996. if (i == 0) {
  997. pos += (size_t) strcspn (pos, "\r\n");
  998. pos += (size_t) strspn (pos, "\r\n");
  999. if (strlen(pos) == 0)
  1000. die (STATE_UNKNOWN,
  1001. _("HTTP UNKNOWN - Could not find redirect location - %s%s\n"),
  1002. status_line, (display_html ? "</A>" : ""));
  1003. continue;
  1004. }
  1005. pos += i;
  1006. pos += strspn (pos, " \t");
  1007. /*
  1008. * RFC 2616 (4.2): ``Header fields can be extended over multiple lines by
  1009. * preceding each extra line with at least one SP or HT.''
  1010. */
  1011. for (; (i = strspn (pos, "\r\n")); pos += i) {
  1012. pos += i;
  1013. if (!(i = strspn (pos, " \t"))) {
  1014. die (STATE_UNKNOWN, _("HTTP UNKNOWN - Empty redirect location%s\n"),
  1015. display_html ? "</A>" : "");
  1016. }
  1017. }
  1018. url = realloc (url, strcspn (pos, "\r\n") + 1);
  1019. if (url == NULL)
  1020. die (STATE_UNKNOWN, _("HTTP UNKNOWN - could not allocate url\n"));
  1021. /* URI_HTTP, URI_HOST, URI_PORT, URI_PATH */
  1022. if (sscanf (pos, HD1, type, addr, &i, url) == 4) {
  1023. url = prepend_slash (url);
  1024. use_ssl = server_type_check (type);
  1025. }
  1026. /* URI_HTTP URI_HOST URI_PATH */
  1027. else if (sscanf (pos, HD2, type, addr, url) == 3 ) {
  1028. url = prepend_slash (url);
  1029. use_ssl = server_type_check (type);
  1030. i = server_port_check (use_ssl);
  1031. }
  1032. /* URI_HTTP URI_HOST URI_PORT */
  1033. else if (sscanf (pos, HD3, type, addr, &i) == 3) {
  1034. strcpy (url, HTTP_URL);
  1035. use_ssl = server_type_check (type);
  1036. }
  1037. /* URI_HTTP URI_HOST */
  1038. else if (sscanf (pos, HD4, type, addr) == 2) {
  1039. strcpy (url, HTTP_URL);
  1040. use_ssl = server_type_check (type);
  1041. i = server_port_check (use_ssl);
  1042. }
  1043. /* URI_PATH */
  1044. else if (sscanf (pos, HD5, url) == 1) {
  1045. /* relative url */
  1046. if ((url[0] != '/')) {
  1047. if ((x = strrchr(server_url, '/')))
  1048. *x = '\0';
  1049. asprintf (&url, "%s/%s", server_url, url);
  1050. }
  1051. i = server_port;
  1052. strcpy (type, server_type);
  1053. strcpy (addr, host_name ? host_name : server_address);
  1054. }
  1055. else {
  1056. die (STATE_UNKNOWN,
  1057. _("HTTP UNKNOWN - Could not parse redirect location - %s%s\n"),
  1058. pos, (display_html ? "</A>" : ""));
  1059. }
  1060. break;
  1061. } /* end while (pos) */
  1062. if (++redir_depth > max_depth)
  1063. die (STATE_WARNING,
  1064. _("HTTP WARNING - maximum redirection depth %d exceeded - %s://%s:%d%s%s\n"),
  1065. max_depth, type, addr, i, url, (display_html ? "</A>" : ""));
  1066. if (server_port==i &&
  1067. !strcmp(server_address, addr) &&
  1068. (host_name && !strcmp(host_name, addr)) &&
  1069. !strcmp(server_url, url))
  1070. die (STATE_WARNING,
  1071. _("HTTP WARNING - redirection creates an infinite loop - %s://%s:%d%s%s\n"),
  1072. type, addr, i, url, (display_html ? "</A>" : ""));
  1073. strcpy (server_type, type);
  1074. free (host_name);
  1075. host_name = strdup (addr);
  1076. free (server_address);
  1077. server_address = strdup (addr);
  1078. free (server_url);
  1079. server_url = url;
  1080. if ((server_port = i) > MAX_PORT)
  1081. die (STATE_UNKNOWN,
  1082. _("HTTP UNKNOWN - Redirection to port above %d - %s://%s:%d%s%s\n"),
  1083. MAX_PORT, server_type, server_address, server_port, server_url,
  1084. display_html ? "</A>" : "");
  1085. if (verbose)
  1086. printf (_("Redirection to %s://%s:%d%s\n"), server_type,
  1087. host_name ? host_name : server_address, server_port, server_url);
  1088. check_http ();
  1089. }
  1090. int
  1091. server_type_check (const char *type)
  1092. {
  1093. if (strcmp (type, "https"))
  1094. return FALSE;
  1095. else
  1096. return TRUE;
  1097. }
  1098. int
  1099. server_port_check (int ssl_flag)
  1100. {
  1101. if (ssl_flag)
  1102. return HTTPS_PORT;
  1103. else
  1104. return HTTP_PORT;
  1105. }
  1106. char *perfd_time (double elapsed_time)
  1107. {
  1108. return fperfdata ("time", elapsed_time, "s",
  1109. check_warning_time, warning_time,
  1110. check_critical_time, critical_time,
  1111. TRUE, 0, FALSE, 0);
  1112. }
  1113. char *perfd_size (int page_len)
  1114. {
  1115. return perfdata ("size", page_len, "B",
  1116. (min_page_len>0?TRUE:FALSE), min_page_len,
  1117. (min_page_len>0?TRUE:FALSE), 0,
  1118. TRUE, 0, FALSE, 0);
  1119. }
  1120. void
  1121. print_help (void)
  1122. {
  1123. print_revision (progname, revision);
  1124. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  1125. printf (COPYRIGHT, copyright, email);
  1126. printf ("%s\n", _("This plugin tests the HTTP service on the specified host. It can test"));
  1127. printf ("%s\n", _("normal (http) and secure (https) servers, follow redirects, search for"));
  1128. printf ("%s\n", _("strings and regular expressions, check connection times, and report on"));
  1129. printf ("%s\n", _("certificate expiration times."));
  1130. printf ("\n\n");
  1131. print_usage ();
  1132. printf (_("NOTE: One or both of -H and -I must be specified"));
  1133. printf ("\n");
  1134. printf (_(UT_HELP_VRSN));
  1135. printf (_(UT_EXTRA_OPTS));
  1136. printf (" %s\n", "-H, --hostname=ADDRESS");
  1137. printf (" %s\n", _("Host name argument for servers using host headers (virtual host)"));
  1138. printf (" %s\n", _("Append a port to include it in the header (eg: example.com:5000)"));
  1139. printf (" %s\n", "-I, --IP-address=ADDRESS");
  1140. printf (" %s\n", _("IP address or name (use numeric address if possible to bypass DNS lookup)."));
  1141. printf (" %s\n", "-p, --port=INTEGER");
  1142. printf (" %s", _("Port number (default: "));
  1143. printf ("%d)\n", HTTP_PORT);
  1144. printf (_(UT_IPv46));
  1145. #ifdef HAVE_SSL
  1146. printf (" %s\n", "-S, --ssl");
  1147. printf (" %s\n", _("Connect via SSL. Port defaults to 443"));
  1148. printf (" %s\n", "-C, --certificate=INTEGER");
  1149. printf (" %s\n", _("Minimum number of days a certificate has to be valid. Port defaults to 443"));
  1150. printf (" %s\n", _("(when this option is used the url is not checked.)\n"));
  1151. #endif
  1152. printf (" %s\n", "-e, --expect=STRING");
  1153. printf (" %s\n", _("Comma-delimited list of strings, at least one of them is expected in"));
  1154. printf (" %s", _("the first (status) line of the server response (default: "));
  1155. printf ("%s)\n", HTTP_EXPECT);
  1156. printf (" %s\n", _("If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)"));
  1157. printf (" %s\n", "-s, --string=STRING");
  1158. printf (" %s\n", _("String to expect in the content"));
  1159. printf (" %s\n", "-u, --url=PATH");
  1160. printf (" %s\n", _("URL to GET or POST (default: /)"));
  1161. printf (" %s\n", "-P, --post=STRING");
  1162. printf (" %s\n", _("URL encoded http POST data"));
  1163. printf (" %s\n", "-j, --method=STRING (for example: HEAD, OPTIONS, TRACE, PUT, DELETE)");
  1164. printf (" %s\n", _("Set HTTP method."));
  1165. printf (" %s\n", "-N, --no-body");
  1166. printf (" %s\n", _("Don't wait for document body: stop reading after headers."));
  1167. printf (" %s\n", _("(Note that this still does an HTTP GET or POST, not a HEAD.)"));
  1168. printf (" %s\n", "-M, --max-age=SECONDS");
  1169. printf (" %s\n", _("Warn if document is more than SECONDS old. the number can also be of"));
  1170. printf (" %s\n", _("the form \"10m\" for minutes, \"10h\" for hours, or \"10d\" for days."));
  1171. printf (" %s\n", "-T, --content-type=STRING");
  1172. printf (" %s\n", _("specify Content-Type header media type when POSTing\n"));
  1173. printf (" %s\n", "-l, --linespan");
  1174. printf (" %s\n", _("Allow regex to span newlines (must precede -r or -R)"));
  1175. printf (" %s\n", "-r, --regex, --ereg=STRING");
  1176. printf (" %s\n", _("Search page for regex STRING"));
  1177. printf (" %s\n", "-R, --eregi=STRING");
  1178. printf (" %s\n", _("Search page for case-insensitive regex STRING"));
  1179. printf (" %s\n", "--invert-regex");
  1180. printf (" %s\n", _("Return CRITICAL if found, OK if not\n"));
  1181. printf (" %s\n", "-a, --authorization=AUTH_PAIR");
  1182. printf (" %s\n", _("Username:password on sites with basic authentication"));
  1183. printf (" %s\n", "-A, --useragent=STRING");
  1184. printf (" %s\n", _("String to be sent in http header as \"User Agent\""));
  1185. printf (" %s\n", "-k, --header=STRING");
  1186. printf (" %s\n", _(" Any other tags to be sent in http header. Use multiple times for additional headers"));
  1187. printf (" %s\n", "-L, --link");
  1188. printf (" %s\n", _("Wrap output in HTML link (obsoleted by urlize)"));
  1189. printf (" %s\n", "-f, --onredirect=<ok|warning|critical|follow>");
  1190. printf (" %s\n", _("How to handle redirected pages"));
  1191. printf (" %s\n", "-m, --pagesize=INTEGER<:INTEGER>");
  1192. printf (" %s\n", _("Minimum page size required (bytes) : Maximum page size required (bytes)"));
  1193. printf (_(UT_WARN_CRIT));
  1194. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  1195. printf (_(UT_VERBOSE));
  1196. printf ("\n");
  1197. printf ("%s\n", _("Notes:"));
  1198. printf (" %s\n", _("This plugin will attempt to open an HTTP connection with the host."));
  1199. printf (" %s\n", _("Successful connects return STATE_OK, refusals and timeouts return STATE_CRITICAL"));
  1200. printf (" %s\n", _("other errors return STATE_UNKNOWN. Successful connects, but incorrect reponse"));
  1201. printf (" %s\n", _("messages from the host result in STATE_WARNING return values. If you are"));
  1202. printf (" %s\n", _("checking a virtual server that uses 'host headers' you must supply the FQDN"));
  1203. printf (" %s\n", _("(fully qualified domain name) as the [host_name] argument."));
  1204. printf ("\n");
  1205. printf (_(UT_EXTRA_OPTS_NOTES));
  1206. #ifdef HAVE_SSL
  1207. printf ("\n");
  1208. printf (" %s\n", _("This plugin can also check whether an SSL enabled web server is able to"));
  1209. printf (" %s\n", _("serve content (optionally within a specified time) or whether the X509 "));
  1210. printf (" %s\n", _("certificate is still valid for the specified number of days."));
  1211. printf ("\n");
  1212. printf ("%s\n", _("Examples:"));
  1213. printf (" %s\n\n", "CHECK CONTENT: check_http -w 5 -c 10 --ssl -H www.verisign.com");
  1214. printf (" %s\n", _("When the 'www.verisign.com' server returns its content within 5 seconds,"));
  1215. printf (" %s\n", _("a STATE_OK will be returned. When the server returns its content but exceeds"));
  1216. printf (" %s\n", _("the 5-second threshold, a STATE_WARNING will be returned. When an error occurs,"));
  1217. printf (" %s\n\n", _("a STATE_CRITICAL will be returned."));
  1218. printf (" %s\n\n", "CHECK CERTIFICATE: check_http -H www.verisign.com -C 14");
  1219. printf (" %s\n", _("When the certificate of 'www.verisign.com' is valid for more than 14 days,"));
  1220. printf (" %s\n", _("a STATE_OK is returned. When the certificate is still valid, but for less than"));
  1221. printf (" %s\n", _("14 days, a STATE_WARNING is returned. A STATE_CRITICAL will be returned when"));
  1222. printf (" %s\n", _("the certificate is expired."));
  1223. #endif
  1224. printf (_(UT_SUPPORT));
  1225. }
  1226. void
  1227. print_usage (void)
  1228. {
  1229. printf (_("Usage:"));
  1230. printf (" %s -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]\n",progname);
  1231. printf (" [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L]\n");
  1232. printf (" [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]\n");
  1233. printf (" [-s string] [-l] [-r <regex> | -R <case-insensitive regex>] [-P string]\n");
  1234. printf (" [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>] [-A string]\n");
  1235. printf (" [-k string] [-S] [-C <age>] [-T <content-type>] [-j method]\n");
  1236. }