1#
2# General http monitor code
3# (sourced by apache and httpmon)
4#
5# Author:	Alan Robertson
6#		Sun Jiang Dong
7#
8# Support:	users@clusterlabs.org
9#
10# License:	GNU General Public License (GPL)
11#
12# Copyright:	(C) 2002-2005 International Business Machines
13#
14
15# default options for http clients
16# NB: We _always_ test a local resource, so it should be
17# safe to connect from the local interface.
18bind_address="127.0.0.1"
19curl_ipv6_opts=""
20if ocf_is_true "$OCF_RESKEY_use_ipv6" || echo "$STATUSURL" | grep -qs "::"; then
21	bind_address="::1"
22	curl_ipv6_opts="-g"
23fi
24WGETOPTS="-O- -q -L --no-proxy --bind-address=$bind_address"
25CURLOPTS="-o - -Ss -L --interface lo $curl_ipv6_opts"
26
27request_url_header() {
28	which curl >/dev/null 2>&1
29	if [ $? -eq 0 ]; then
30		curl -IL --connect-timeout 5 --interface lo $curl_ipv6_opts "$1" > /dev/null 2>&1
31		return $?
32	fi
33
34	which wget >/dev/null 2>&1
35	if [ $? -eq 0 ]; then
36		local header=$(wget --server-response --spider --timeout=5 --tries=2 "$1" 2>&1)
37		if [ $? -eq 0 ]; then
38			return $OCF_SUCCESS
39		fi
40
41		# a 4xx error is still a server response.
42		echo "$header" | grep "HTTP/1.1 4.. " > /dev/null 2>&1
43		return $?
44	fi
45	return $OCF_ERR_GENERIC
46}
47
48#
49# run the http client
50#
51curl_func() {
52	cl_opts="$CURLOPTS $test_httpclient_opts"
53	if [ x != "x$test_user" ]; then
54		echo "-u $test_user:$test_password" |
55			curl -K - $cl_opts "$1"
56	else
57		curl $cl_opts "$1"
58	fi
59}
60wget_func() {
61	auth=""
62	cl_opts="$WGETOPTS $test_httpclient_opts"
63	[ x != "x$test_user" ] &&
64		auth="--http-user=$test_user --http-passwd=$test_password"
65	wget $auth $cl_opts "$1"
66}
67#
68# rely on whatever the user provided
69userdefined() {
70	$test_httpclient $test_httpclient_opts "$1"
71}
72
73#
74# find a good http client
75#
76findhttpclient() {
77	# prefer wget (for historical reasons)
78	if [ "x$CLIENT" != x ] && which "$CLIENT" >/dev/null 2>&1; then
79		echo "$CLIENT"
80	elif which wget >/dev/null 2>&1; then
81		echo "wget"
82	elif which curl >/dev/null 2>&1; then
83		echo "curl"
84	else
85		return 1
86	fi
87}
88gethttpclient() {
89	[ -z "$test_httpclient" ] &&
90		test_httpclient=$ourhttpclient
91	case "$test_httpclient" in
92		curl|wget) echo ${test_httpclient}_func;;  #these are supported
93		*) echo userdefined;;
94	esac
95}
96
97# test configuration good?
98is_testconf_sane() {
99	if [ "x$test_regex" = x -o "x$test_url" = x ]; then
100		ocf_log err "test regular expression or test url empty"
101		return 1
102	fi
103	if [ "x$test_user$test_password" != x -a \( "x$test_user" = x -o "x$test_password" = x \) ]; then
104		ocf_log err "bad user authentication for extended test"
105		return 1
106	fi
107	return 0
108}
109#
110# read the test definition from the config
111#
112readtestconf() {
113	test_name="$1" # we look for this one or the first one if empty
114	lcnt=0
115	readdef=""
116	test_url="" test_regex=""
117	test_user="" test_password=""
118	test_httpclient="" test_httpclient_opts=""
119
120	while read key value; do
121		lcnt=$((lcnt+1))
122		if [ "$readdef" ]; then
123			case "$key" in
124			"url") test_url="$value" ;;
125			"user") test_user="$value" ;;
126			"password") test_password="$value" ;;
127			"client") test_httpclient="$value" ;;
128			"client_opts") test_httpclient_opts="$value" ;;
129			"match") test_regex="$value" ;;
130			"end") break ;;
131			"#"*|"") ;;
132			*) ocf_log err "$lcnt: $key: unknown keyword"; return 1 ;;
133			esac
134		else
135			[ "$key" = "test" ] &&
136				[ -z "$test_name" -o "$test_name" = "$value" ] &&
137				readdef=1
138		fi
139	done
140}
141