summaryrefslogtreecommitdiffstats
path: root/plugins/check_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_http.c')
-rw-r--r--plugins/check_http.c116
1 files changed, 94 insertions, 22 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c
index 9231a559..6db38e8c 100644
--- a/plugins/check_http.c
+++ b/plugins/check_http.c
@@ -1,40 +1,40 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Nagios check_http plugin 3* Nagios check_http plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 1999-2008 Nagios Plugins Development Team 6* Copyright (c) 1999-2013 Nagios Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*
10* This file contains the check_http plugin 10* This file contains the check_http plugin
11* 11*
12* This plugin tests the HTTP service on the specified host. It can test 12* This plugin tests the HTTP service on the specified host. It can test
13* normal (http) and secure (https) servers, follow redirects, search for 13* normal (http) and secure (https) servers, follow redirects, search for
14* strings and regular expressions, check connection times, and report on 14* strings and regular expressions, check connection times, and report on
15* certificate expiration times. 15* certificate expiration times.
16* 16*
17* 17*
18* This program is free software: you can redistribute it and/or modify 18* This program is free software: you can redistribute it and/or modify
19* it under the terms of the GNU General Public License as published by 19* it under the terms of the GNU General Public License as published by
20* the Free Software Foundation, either version 3 of the License, or 20* the Free Software Foundation, either version 3 of the License, or
21* (at your option) any later version. 21* (at your option) any later version.
22* 22*
23* This program is distributed in the hope that it will be useful, 23* This program is distributed in the hope that it will be useful,
24* but WITHOUT ANY WARRANTY; without even the implied warranty of 24* but WITHOUT ANY WARRANTY; without even the implied warranty of
25* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26* GNU General Public License for more details. 26* GNU General Public License for more details.
27* 27*
28* You should have received a copy of the GNU General Public License 28* You should have received a copy of the GNU General Public License
29* along with this program. If not, see <http://www.gnu.org/licenses/>. 29* along with this program. If not, see <http://www.gnu.org/licenses/>.
30* 30*
31* 31*
32*****************************************************************************/ 32*****************************************************************************/
33 33
34/* splint -I. -I../../plugins -I../../lib/ -I/usr/kerberos/include/ ../../plugins/check_http.c */ 34/* splint -I. -I../../plugins -I../../lib/ -I/usr/kerberos/include/ ../../plugins/check_http.c */
35 35
36const char *progname = "check_http"; 36const char *progname = "check_http";
37const char *copyright = "1999-2011"; 37const char *copyright = "1999-2013";
38const char *email = "nagiosplug-devel@lists.sourceforge.net"; 38const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 39
40#include "common.h" 40#include "common.h"
@@ -43,7 +43,6 @@ const char *email = "nagiosplug-devel@lists.sourceforge.net";
43#include "base64.h" 43#include "base64.h"
44#include <ctype.h> 44#include <ctype.h>
45 45
46#define INPUT_DELIMITER ";"
47#define STICKY_NONE 0 46#define STICKY_NONE 0
48#define STICKY_HOST 1 47#define STICKY_HOST 1
49#define STICKY_PORT 2 48#define STICKY_PORT 2
@@ -85,6 +84,7 @@ int errcode;
85int invert_regex = 0; 84int invert_regex = 0;
86 85
87struct timeval tv; 86struct timeval tv;
87struct timeval tv_temp;
88 88
89#define HTTP_URL "/" 89#define HTTP_URL "/"
90#define CRLF "\r\n" 90#define CRLF "\r\n"
@@ -115,6 +115,7 @@ int followsticky = STICKY_NONE;
115int use_ssl = FALSE; 115int use_ssl = FALSE;
116int use_sni = FALSE; 116int use_sni = FALSE;
117int verbose = FALSE; 117int verbose = FALSE;
118int show_extended_perfdata = FALSE;
118int sd; 119int sd;
119int min_page_len = 0; 120int min_page_len = 0;
120int max_page_len = 0; 121int max_page_len = 0;
@@ -131,6 +132,11 @@ void redir (char *pos, char *status_line);
131int server_type_check(const char *type); 132int server_type_check(const char *type);
132int server_port_check(int ssl_flag); 133int server_port_check(int ssl_flag);
133char *perfd_time (double microsec); 134char *perfd_time (double microsec);
135char *perfd_time_connect (double microsec);
136char *perfd_time_ssl (double microsec);
137char *perfd_time_firstbyte (double microsec);
138char *perfd_time_headers (double microsec);
139char *perfd_time_transfer (double microsec);
134char *perfd_size (int page_len); 140char *perfd_size (int page_len);
135void print_help (void); 141void print_help (void);
136void print_usage (void); 142void print_usage (void);
@@ -216,6 +222,7 @@ process_arguments (int argc, char **argv)
216 {"invert-regex", no_argument, NULL, INVERT_REGEX}, 222 {"invert-regex", no_argument, NULL, INVERT_REGEX},
217 {"use-ipv4", no_argument, 0, '4'}, 223 {"use-ipv4", no_argument, 0, '4'},
218 {"use-ipv6", no_argument, 0, '6'}, 224 {"use-ipv6", no_argument, 0, '6'},
225 {"extended-perfdata", no_argument, 0, 'E'},
219 {0, 0, 0, 0} 226 {0, 0, 0, 0}
220 }; 227 };
221 228
@@ -236,7 +243,7 @@ process_arguments (int argc, char **argv)
236 } 243 }
237 244
238 while (1) { 245 while (1) {
239 c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:e:p:s:R:r:u:f:C:nlLS::m:M:N", longopts, &option); 246 c = getopt_long (argc, argv, "Vvh46t:c:w:A:k:H:P:j:T:I:a:b:e:p:s:R:r:u:f:C:nlLS::m:M:N:E", longopts, &option);
240 if (c == -1 || c == EOF) 247 if (c == -1 || c == EOF)
241 break; 248 break;
242 249
@@ -471,6 +478,9 @@ process_arguments (int argc, char **argv)
471 } 478 }
472 } 479 }
473 break; 480 break;
481 case 'E': /* show extended perfdata */
482 show_extended_perfdata = TRUE;
483 break;
474 } 484 }
475 } 485 }
476 486
@@ -812,17 +822,33 @@ check_http (void)
812 char *pos; 822 char *pos;
813 long microsec; 823 long microsec;
814 double elapsed_time; 824 double elapsed_time;
825 long microsec_connect;
826 double elapsed_time_connect;
827 long microsec_ssl;
828 double elapsed_time_ssl;
829 long microsec_firstbyte;
830 double elapsed_time_firstbyte;
831 long microsec_headers;
832 double elapsed_time_headers;
833 long microsec_transfer;
834 double elapsed_time_transfer;
815 int page_len = 0; 835 int page_len = 0;
816 int result = STATE_OK; 836 int result = STATE_OK;
817 837
818 /* try to connect to the host at the given port number */ 838 /* try to connect to the host at the given port number */
839 gettimeofday (&tv_temp, NULL);
819 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK) 840 if (my_tcp_connect (server_address, server_port, &sd) != STATE_OK)
820 die (STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n")); 841 die (STATE_CRITICAL, _("HTTP CRITICAL - Unable to open TCP socket\n"));
842 microsec_connect = deltime (tv_temp);
821#ifdef HAVE_SSL 843#ifdef HAVE_SSL
844 elapsed_time_connect = (double)microsec_connect / 1.0e6;
822 if (use_ssl == TRUE) { 845 if (use_ssl == TRUE) {
846 gettimeofday (&tv_temp, NULL);
823 result = np_net_ssl_init_with_hostname_and_version(sd, (use_sni ? host_name : NULL), ssl_version); 847 result = np_net_ssl_init_with_hostname_and_version(sd, (use_sni ? host_name : NULL), ssl_version);
824 if (result != STATE_OK) 848 if (result != STATE_OK)
825 return result; 849 return result;
850 microsec_ssl = deltime (tv_temp);
851 elapsed_time_ssl = (double)microsec_ssl / 1.0e6;
826 if (check_cert == TRUE) { 852 if (check_cert == TRUE) {
827 result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); 853 result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit);
828 np_net_ssl_cleanup(); 854 np_net_ssl_cleanup();
@@ -854,8 +880,7 @@ check_http (void)
854 /* optionally send any other header tag */ 880 /* optionally send any other header tag */
855 if (http_opt_headers_count) { 881 if (http_opt_headers_count) {
856 for (i = 0; i < http_opt_headers_count ; i++) { 882 for (i = 0; i < http_opt_headers_count ; i++) {
857 for ((pos = strtok(http_opt_headers[i], INPUT_DELIMITER)); pos; (pos = strtok(NULL, INPUT_DELIMITER))) 883 xasprintf (&buf, "%s%s\r\n", buf, http_opt_headers[i]);
858 xasprintf (&buf, "%s%s\r\n", buf, pos);
859 } 884 }
860 /* This cannot be free'd here because a redirection will then try to access this and segfault */ 885 /* This cannot be free'd here because a redirection will then try to access this and segfault */
861 /* Covered in a testcase in tests/check_http.t */ 886 /* Covered in a testcase in tests/check_http.t */
@@ -891,11 +916,19 @@ check_http (void)
891 } 916 }
892 917
893 if (verbose) printf ("%s\n", buf); 918 if (verbose) printf ("%s\n", buf);
919 gettimeofday (&tv_temp, NULL);
894 my_send (buf, strlen (buf)); 920 my_send (buf, strlen (buf));
921 microsec_headers = deltime (tv_temp);
922 elapsed_time_headers = (double)microsec_headers / 1.0e6;
895 923
896 /* fetch the page */ 924 /* fetch the page */
897 full_page = strdup(""); 925 full_page = strdup("");
926 gettimeofday (&tv_temp, NULL);
898 while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) { 927 while ((i = my_recv (buffer, MAX_INPUT_BUFFER-1)) > 0) {
928 if ((i >= 1) && (elapsed_time_firstbyte <= 0.000001)) {
929 microsec_firstbyte = deltime (tv_temp);
930 elapsed_time_firstbyte = (double)microsec_firstbyte / 1.0e6;
931 }
899 buffer[i] = '\0'; 932 buffer[i] = '\0';
900 xasprintf (&full_page_new, "%s%s", full_page, buffer); 933 xasprintf (&full_page_new, "%s%s", full_page, buffer);
901 free (full_page); 934 free (full_page);
@@ -907,6 +940,8 @@ check_http (void)
907 break; 940 break;
908 } 941 }
909 } 942 }
943 microsec_transfer = deltime (tv_temp);
944 elapsed_time_transfer = (double)microsec_transfer / 1.0e6;
910 945
911 if (i < 0 && errno != ECONNRESET) { 946 if (i < 0 && errno != ECONNRESET) {
912#ifdef HAVE_SSL 947#ifdef HAVE_SSL
@@ -1108,11 +1143,25 @@ check_http (void)
1108 msg[strlen(msg)-3] = '\0'; 1143 msg[strlen(msg)-3] = '\0';
1109 1144
1110 /* check elapsed time */ 1145 /* check elapsed time */
1111 xasprintf (&msg, 1146 if (show_extended_perfdata)
1112 _("%s - %d bytes in %.3f second response time %s|%s %s"), 1147 xasprintf (&msg,
1113 msg, page_len, elapsed_time, 1148 _("%s - %d bytes in %.3f second response time %s|%s %s %s %s %s %s %s"),
1114 (display_html ? "</A>" : ""), 1149 msg, page_len, elapsed_time,
1115 perfd_time (elapsed_time), perfd_size (page_len)); 1150 (display_html ? "</A>" : ""),
1151 perfd_time (elapsed_time),
1152 perfd_size (page_len),
1153 perfd_time_connect (elapsed_time_connect),
1154 use_ssl == TRUE ? perfd_time_ssl (elapsed_time_ssl) : "",
1155 perfd_time_headers (elapsed_time_headers),
1156 perfd_time_firstbyte (elapsed_time_firstbyte),
1157 perfd_time_transfer (elapsed_time_transfer));
1158 else
1159 xasprintf (&msg,
1160 _("%s - %d bytes in %.3f second response time %s|%s %s"),
1161 msg, page_len, elapsed_time,
1162 (display_html ? "</A>" : ""),
1163 perfd_time (elapsed_time),
1164 perfd_size (page_len));
1116 1165
1117 result = max_state_alt(get_status(elapsed_time, thlds), result); 1166 result = max_state_alt(get_status(elapsed_time, thlds), result);
1118 1167
@@ -1301,7 +1350,30 @@ char *perfd_time (double elapsed_time)
1301 TRUE, 0, FALSE, 0); 1350 TRUE, 0, FALSE, 0);
1302} 1351}
1303 1352
1353char *perfd_time_connect (double elapsed_time_connect)
1354{
1355 return fperfdata ("time_connect", elapsed_time_connect, "s", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0);
1356}
1357
1358char *perfd_time_ssl (double elapsed_time_ssl)
1359{
1360 return fperfdata ("time_ssl", elapsed_time_ssl, "s", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0);
1361}
1362
1363char *perfd_time_headers (double elapsed_time_headers)
1364{
1365 return fperfdata ("time_headers", elapsed_time_headers, "s", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0);
1366}
1304 1367
1368char *perfd_time_firstbyte (double elapsed_time_firstbyte)
1369{
1370 return fperfdata ("time_firstbyte", elapsed_time_firstbyte, "s", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0);
1371}
1372
1373char *perfd_time_transfer (double elapsed_time_transfer)
1374{
1375 return fperfdata ("time_transfer", elapsed_time_transfer, "s", FALSE, 0, FALSE, 0, FALSE, 0, FALSE, 0);
1376}
1305 1377
1306char *perfd_size (int page_len) 1378char *perfd_size (int page_len)
1307{ 1379{