Projet

Général

Profil

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

root / plugins / other / wget_page @ e908d2d2

Historique | Voir | Annoter | Télécharger (8,99 ko)

1
#!/bin/sh
2
#
3
# Plugin to graph response times of the specified websites/URLs.
4
#
5
# Parameters:
6
#
7
#       config   (required)
8
#       autoconf (optional - used by lrrd-config)
9
#
10
# Configuration example:
11
#
12
# [wget_page]
13
# timeout 30
14
# env.names url1 url2
15
# env.timeout 20
16
# env.error_value 60
17
# env.max 120
18
#
19
# env.url_url1 http://www1.example.com/path1/page1
20
# env.label_url1 Example URL#1
21
# env.timeout_url1 10
22
# env.warning_url1 5
23
# env.critical_url1 8
24
#
25
# env.url_url2 https://www2.example.com/path2/page2
26
# env.label_url2 Example URL#2
27
# env.timeout_url2 30
28
# env.warning_url2 15
29
# env.critical_url2 20
30
# env.wget_opts_url2 --no-cache --tries=1 --no-check-certificate
31
#
32
# URL options:
33
#
34
# You can define the following options for each specified URL
35
# as seen in the above example.
36
#
37
# - url: the URL to be downloaded with Wget
38
# - label: the label assigned to the line of the given in URL in the graph
39
# - timeout: the value passed to Wget through the "--timeout" option.
40
# - warning: the value for the given URL that stands for the warning level
41
# - critical: the value for the given URL that stands for the critical level
42
# - max: the maximum value for the given URL (values above this will be
43
#     discarded)
44
# - error_value: the value for the given URL that will be used to mark when
45
#     Wget returned an error. A zero error_value causes the plugin to ignore
46
#     Wget's return value.
47
# - regex_error_value: the value for the given URL that will be used to mark
48
#     when a regular expression match failed (either for the HTTP response
49
#     headers or the body).
50
# - regex_header_<n>: a regular expression that the HTTP response header must
51
#     match or the plugin will return regex_error_value for the given URL.
52
#     By default the plugin uses egrep, thus extended regexps are expected.
53
#     You can define any number of regexps, but you've to start the index at
54
#     "1" and increase it sequentially.
55
#     I.e. regex_header_1, regex_header_2, ...
56
# - regex_body_<n>: same as regex_header_<n>, but matches the HTTP response
57
#     body.
58
# - grep_opts: various options supplied to grep for regexp matches for the
59
#     given URL. By default these are: -E (use of extended regexps)
60
#     and -i (case insensitive regexp matching)
61
# - wget_opts: various options supplied to the Wget command for the given URL.
62
# - join_lines: if "true" and regexp matching is applied, the HTTP response body
63
#     is stripped of newline ("\n") characters. This helps with complex regexps
64
#     since grep can match only in a single line at a time and it would not be
65
#     possible to match on complex HTML/XML structures otherwise.
66
#     This is enabled by default.
67
#
68
# $Log$
69
#
70
# Revision 1.0  2006/07/11 08:49:43 cipixul@gmail.com
71
# Initial version
72
#
73
# Revision 2.0  2010/03/25 13:46:13 muzso@muzso.hu
74
# Rewrote most of the code. Added multips-like options.
75
#
76
# Revision 2.1  2010/04/22 11:43:53 muzso@muzso.hu
77
# Added regular expression matching against the contents of the checked URL.
78
#
79
# Revision 2.2  2010/04/23 15:21:12 muzso@muzso.hu
80
# Bugfix. Regexp matching on HTTP response bodies with a trailing newline
81
# was flawed.
82
#
83
#%# family=auto
84
#%# capabilities=autoconf
85

    
86
[ -n "${wget_bin}" ] || wget_bin=$(which wget)
87
[ -n "${time_bin}" ] || time_bin=$(which time)
88
[ -n "${mktemp_bin}" ] || mktemp_bin=$(which mktemp)
89
[ -n "${grep_bin}" ] || grep_bin=$(which grep)
90
[ -n "${tail_bin}" ] || tail_bin=$(which tail)
91

    
92
default_error_value=30
93
default_regex_error_value=40
94
default_grep_opts="-E -i"
95
default_wget_opts="--no-cache --tries=1"
96
default_timeout=20
97
default_join_lines=true
98

    
99
if [ "${1}" = "autoconf" ]; then
100
  result=0
101
  if [ -z "${wget_bin}" -o ! -f "${wget_bin}" -o ! -x "${wget_bin}" ]; then
102
    result=1
103
  else
104
    if [ -z "${time_bin}" -o ! -f "${time_bin}" -o ! -x "${time_bin}" ]; then
105
      result=2
106
    else
107
      if [ -z "${mktemp_bin}" -o ! -f "${mktemp_bin}" -o ! -x "${mktemp_bin}" ]; then
108
        result=3
109
      else
110
        if [ -z "${grep_bin}" -o ! -f "${grep_bin}" -o ! -x "${grep_bin}" ]; then
111
          result=4
112
        else
113
          [ -z "${tail_bin}" -o ! -f "${tail_bin}" -o ! -x "${tail_bin}" ] && result=5
114
        fi
115
      fi
116
    fi
117
  fi
118
  if [ ${result} -eq 0 ]; then
119
    echo "yes"
120
  else
121
    echo "no"
122
  fi
123
  exit $result
124
fi
125

    
126
if [ -z "${names}" ]; then
127
  echo "Configuration required"
128
  exit 1
129
fi
130

    
131
[ -n "${error_value}" ] || error_value=${default_error_value}
132
[ -n "${regex_error_value}" ] || regex_error_value=${default_regex_error_value}
133
[ -n "${grep_opts}" ] || grep_opts=${default_grep_opts}
134
[ -n "${wget_opts}" ] || wget_opts=${default_wget_opts}
135
[ -n "${timeout}" ] || timeout=${default_timeout}
136
[ -n "${join_lines}" ] || join_lines=${default_join_lines}
137
[ -n "${warning}" ] || warning=$((timeout/2))
138
[ -n "${critical}" ] || critical=${timeout}
139
[ -n "${max}" ] || max=$((timeout*2))
140

    
141
if [ "${1}" = "config" ]; then
142
  echo "graph_title wget loadtime of webpages"
143
  echo "graph_args --base 1000 -l 0"
144
  echo "graph_scale no"
145
  echo "graph_vlabel Load time in seconds"
146
  echo "graph_category http"
147
  echo "graph_info This graph shows load time in seconds of one or more urls"
148
  I=1
149
  for name in ${names}; do
150
    eval iurl='${url_'${name}'}'
151
    if [ -n "${iurl}" ]; then
152
      eval ilabel='${label_'${name}':-url${I}}'
153
      eval iwarning='${warning_'${name}':-${warning}}'
154
      eval icritical='${critical_'${name}':-${critical}}'
155
      eval imax='${max_'${name}':-${max}}'
156
      cat << EOH
157
loadtime${I}.label ${ilabel}
158
loadtime${I}.info Load time for ${iurl}
159
loadtime${I}.min 0
160
loadtime${I}.max ${imax}
161
EOH
162
      [ ${iwarning} -gt 0 ] && echo "loadtime${I}.warning ${iwarning}"
163
      [ ${icritical} -gt 0 ] && echo "loadtime${I}.critical ${icritical}"
164
      I=$((I+1))
165
    fi
166
  done
167
  exit 0
168
fi
169

    
170
I=1
171
for name in ${names}; do
172
  eval iurl='${url_'${name}'}'
173
  if [ -n "${iurl}" ]; then
174
    eval ierror_value='${error_value_'${name}':-${error_value}}'
175
    eval iregex_error_value='${regex_error_value_'${name}':-${regex_error_value}}'
176
    eval igrep_opts='${grep_opts_'${name}':-${grep_opts}}'
177
    eval iwget_opts='${wget_opts_'${name}':-${wget_opts}}'
178
    eval iwget_post_data='${wget_post_data_'${name}':-${wget_post_data}}'
179
    eval ijoin_lines='${join_lines_'${name}':-${join_lines}}'
180
    eval itimeout='${timeout_'${name}':-${timeout}}'
181
    loadtime=""
182
    tempfile=$(mktemp)
183
    if [ -z "${iwget_post_data}" ]; then
184
      timing=$(${time_bin} -p ${wget_bin} --save-headers --no-directories --output-document "${tempfile}" --timeout ${itimeout} ${iwget_opts} "${iurl}" 2>&1)
185
    else
186
      timing=$(${time_bin} -p ${wget_bin} --post-data "${iwget_post_data}" --save-headers --no-directories --output-document "${tempfile}" --timeout ${itimeout} ${iwget_opts} "${iurl}" 2>&1)
187
    fi
188
    wget_result=$?
189
    if [ -f "${tempfile}" ]; then
190
      if [ ${wget_result} -ne 0 -a ${ierror_value} -gt 0 ]; then
191
        loadtime=${ierror_value}
192
      else
193
        tempheader=""
194
        tempbody=""
195
        K=0
196
        while [ -z "${loadtime}" ]; do
197
          K=$((K+1))
198
          eval iregex_header='${regex_header_'${K}'_'${name}':-${regex_header_'${K}'}}'
199
          eval iregex_body='${regex_body_'${K}'_'${name}':-${regex_body_'${K}'}}'
200
          [ -z "${iregex_header}" -a -z "${iregex_body}" ] && break
201
          if [ ${K} -eq 1 ]; then
202
            OIFS="${IFS}"
203
            # we skip carrige return characters from the end of header lines
204
            IFS=$(echo -en "\r")
205
            inheader=0
206
            # The "read" command reads only lines terminated by a specific
207
            # character (which by default the newline char).
208
            # To read the end of the file (the bytes after the last newline) too
209
            # we append a newline.
210
            echo "" >> "${tempfile}"
211
            while read -r line; do
212
              if [ -z "${line}" ]; then
213
                inheader=1
214
                # We reached the border of the header and the body.
215
                # Setting IFS to an empty string puts the entire read lines from
216
                # the body into our "line" variable.
217
                IFS=""
218
              else
219
                if [ ${inheader} -eq 0 ]; then
220
                  tempheader="${tempheader}${line}
221
"
222
                else
223
                  if [ "${ijoin_lines}" = "true" ]; then
224
                    tempbody="${tempbody}${line}"
225
                  else
226
                    tempbody="${tempbody}${line}
227
"
228
                  fi
229
                fi
230
              fi
231
            done < "${tempfile}"
232
            IFS="${OIFS}"
233
          fi
234
          if [ -n "${iregex_header}" ] && ! echo "${tempheader}" | ${grep_bin} -qs ${igrep_opts} "${iregex_header}" 2> /dev/null; then
235
            loadtime=${iregex_error_value}
236
          else
237
            if [ -n "${iregex_body}" ] && ! echo "${tempbody}" | ${grep_bin} -qs ${igrep_opts} "${iregex_body}" 2> /dev/null; then
238
              loadtime=${iregex_error_value}
239
            fi
240
          fi
241
        done
242
        if [ -z "${loadtime}" ]; then
243
          loadtime=$(echo "${timing}" | grep "^real *[0-9]" | cut -d ' ' -f 2)
244
        fi
245
      fi
246
      rm -f "${tempfile}" > /dev/null 2>&1
247
    else
248
      loadtime=$((ierror_value*2))
249
    fi
250
    echo "loadtime${I}.value ${loadtime}"
251
    I=$((I+1))
252
  fi
253
done
254