Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / http / http_response @ ad3351ca

Historique | Voir | Annoter | Télécharger (5,77 ko)

1
#!/usr/bin/env bash
2
# vim: expandtab:ts=4:sw=4
3

    
4
: << =cut
5

    
6
=head1 NAME
7

    
8
http_response - Monitor HTTP response statistics
9

    
10
=head1 CONFIGURATION
11

    
12
The following environment variables are used
13

    
14
 sites       - Sites to check
15
                 - separated by spaces
16
                 - can contain basic auth credentials
17
                 - defaults to "http://localhost/"
18
 max_time    - Timeout for each site check in seconds
19
                 - defaults to 5 seconds
20
 short_label - Switch for shortening the label below the graph
21
                 - defaults to false
22

    
23
=head2 CONFIGURATION EXAMPLE
24

    
25
 [http_response]
26
  env.sites http://example.com/ https://user:secret@example2.de
27
  env.max_time 20
28
  env.short_label true
29

    
30
=head1 PREREQUISITES
31

    
32
This plugin needs at least bash version 4 to run
33

    
34
=head1 NOTES
35

    
36
This plugin unifies the functionalities of the following plugins into one
37
multigraph plugin
38

    
39
 http_loadtime     - https://gallery.munin-monitoring.org/plugins/munin/http_loadtime/
40
 http_responsecode - https://gallery.munin-monitoring.org/plugins/munin-contrib/http_responsecode/
41

    
42
In contrast to using these two plugins with the same configuration, this plugin
43
performs only one request per site and munin run to gather its statistics.
44

    
45
=head1 AUTHOR
46

    
47
Copyright (C) 2020 Klaus Sperner
48

    
49
=head1 LICENSE
50

    
51
This program is free software; you can redistribute it and/or
52
modify it under the terms of the GNU General Public License
53
as published by the Free Software Foundation; version 2 dated June,
54
1991.
55

    
56
This program is distributed in the hope that it will be useful,
57
but WITHOUT ANY WARRANTY; without even the implied warranty of
58
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
59
GNU General Public License for more details.
60

    
61
You should have received a copy of the GNU General Public License
62
along with this program; if not, write to the Free Software
63
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
64

    
65
=head1 MAGIC MARKERS
66

    
67
 #%# family=manual
68

    
69
=cut
70

    
71
. "$MUNIN_LIBDIR/plugins/plugin.sh"
72

    
73
readonly uri_regex='^(https?://)([^:]*):(.*)@(.*)$'
74

    
75
strip_credentials_from_url() {
76
    if [[ "$1" =~ $uri_regex ]]; then
77
        echo "${BASH_REMATCH[1]}${BASH_REMATCH[4]}"
78
    else
79
        echo "$1"
80
    fi
81
}
82

    
83
extract_username_from_url() {
84
    if [[ "$1" =~ $uri_regex ]]; then
85
        echo "${BASH_REMATCH[2]}"
86
    else
87
        echo ""
88
    fi
89
}
90

    
91
extract_password_from_url() {
92
    if [[ "$1" =~ $uri_regex ]]; then
93
        echo "${BASH_REMATCH[3]}"
94
    else
95
        echo ""
96
    fi
97
}
98

    
99
compute_label() {
100
    if [[ "${short_label,,}" == "true" || "${short_label,,}" == "yes" ]]; then
101
        if [[ ${#1} -gt 33 ]]; then
102
            echo "${1:0:30}..."
103
        else
104
            echo "$1"
105
        fi
106
    else
107
        echo "$1"
108
    fi
109
}
110

    
111
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
112
    >&2 echo "The plugin http_response needs at least bash version 4"
113
    exit 1
114
fi
115

    
116
sites=${sites:-"http://localhost/"}
117
max_time=${max_time:-5}
118
short_label=${short_label:-"false"}
119

    
120
if [[ "$1" == "config" ]]; then
121
    echo 'multigraph http_response_code'
122
    echo 'graph_args --base 1000 -l 0 -u 511'
123
    echo 'graph_title HTTP Response Codes'
124
    echo 'graph_vlabel Repsonse Code'
125
    echo 'graph_category network'
126
    echo 'graph_info This graph shows HTTP response code statistics'
127
    echo 'graph_printf %3.0lf'
128
    for site in $sites; do
129
        site_without_credentials=$( strip_credentials_from_url "$site" )
130
        siteid="$( clean_fieldname "$site_without_credentials" )"
131
        echo "$siteid.label $( compute_label "$site_without_credentials" )"
132
        echo "$siteid.info HTTP response code statistics for $site_without_credentials"
133
        echo "$siteid.critical 99:399";
134
    done
135
    echo 'multigraph http_response_time'
136
    echo 'graph_args --base 1000 -l 0'
137
    echo 'graph_title HTTP Response Times'
138
    echo 'graph_vlabel Response Time in seconds'
139
    echo 'graph_category network'
140
    echo 'graph_info This graph shows HTTP response time statistics'
141
    for site in $sites; do
142
        site_without_credentials=$( strip_credentials_from_url "$site" )
143
        siteid="$( clean_fieldname "$site_without_credentials" )"
144
        echo "$siteid.label $( compute_label "$site_without_credentials" )"
145
        echo "$siteid.info HTTP response time statistics for $site_without_credentials"
146
    done
147
    exit 0
148
fi
149

    
150
declare -A response_codes
151
declare -A response_times
152

    
153
for site in $sites; do
154
    site_without_credentials=$( strip_credentials_from_url "$site" )
155
    username=$( extract_username_from_url "$site" )
156
    password=$( extract_password_from_url "$site" )
157

    
158
    curl_config_file=""
159
    curl_auth_opt=()
160
    if [ -n "$username" ]; then
161
        if [ -z "$password" ]; then
162
            >&2 echo "Invalid configuration: username specified without password"
163
            exit 1
164
        fi
165
        curl_config_file=$(mktemp) || exit 1
166
        trap 'rm -f "$curl_config_file"' EXIT
167
        echo "user=${username}:${password}" >> "$curl_config_file"
168
        curl_auth_opt=(--config "$curl_config_file")
169
    fi
170

    
171
    siteid="$( clean_fieldname "$site_without_credentials" )"
172
    statuscode=
173
    loadtime=
174
    start=$(date +%s.%N)
175
    statuscode=$( curl "${curl_auth_opt[@]}" --write-out '%{http_code}' --max-time "$max_time" --silent --output /dev/null "$site_without_credentials" )
176
    returncode=$?
177
    loadtime=$( echo "$start" "$(date +%s.%N)" | awk '{ print($2 - $1); }' )
178
    if [[ $returncode -ne 0 ]]; then
179
        loadtime=0
180
    fi
181
    response_codes+=(["$siteid"]="$statuscode")
182
    response_times+=(["$siteid"]="$loadtime")
183

    
184
    if [ -n "$curl_config_file" ]; then
185
        rm -f "$curl_config_file"
186
    fi
187
done
188

    
189
echo 'multigraph http_response_code'
190
for siteid in "${!response_codes[@]}"; do
191
    echo "${siteid}.value ${response_codes[${siteid}]}"
192
done
193

    
194
echo 'multigraph http_response_time'
195
for siteid in "${!response_times[@]}"; do
196
    echo "${siteid}.value ${response_times[${siteid}]}"
197
done
198