check_http.c 43 KB

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