#!/bin/sh
#
# Plugin to graph response times of the specified websites/URLs.
#
# Parameters:
#
#       config   (required)
#       autoconf (optional - used by lrrd-config)
#
# Configuration example
#
# [wget_page]
# env.names url1 url2
# env.timeout 20
# env.error_value 60
# env.max 120
#
# env.url_url1 http://www1.example.com/path1/page1
# env.label_url1 Example URL#1
# env.timeout_url1 10
# env.warning_url1 5
# env.critical_url1 8
#
# env.url_url2 https://www2.example.com/path2/page2
# env.label_url2 Example URL#2
# env.timeout_url2 30
# env.warning_url2 15
# env.critical_url2 20
# env.wget_opts_url2 --no-cache --tries=1 --no-check-certificate
#
# $Log$
#
# Revision 1.0  2006/07/11 08:49:43 cipixul@gmail.com
# Initial version
#
# Revision 2.0  2010/03/25 13:46:13 muzso@muzso.hu
# Rewrote most of the code. Added multips-like options.
#
#%# family=auto
#%# capabilities=autoconf

time_bin=$(which time)
wget_bin=$(which wget)

default_error_value=30
default_wget_opts="--no-cache --tries=1"
default_timeout=20

if [ "${1}" = "autoconf" ]; then
  result=0
  if [ -z "${time_bin}" ]; then
    result=1
  else
    [ -z "${wget_bin}" ] && result=2
  fi
  if [ ${result} -eq 0 ]; then
    echo "yes"
  else
    echo "no"
  fi
  exit $result
fi

if [ -z "${names}" ]; then
  echo "Configuration required"
  exit 1
fi

[ -n "${error_value}" ] || error_value=${default_error_value}
[ -n "${wget_opts}" ] || wget_opts=${default_wget_opts}
[ -n "${timeout}" ] || timeout=${default_timeout}
[ -n "${warning}" ] || warning=$((timeout/2))
[ -n "${critical}" ] || critical=${timeout}
[ -n "${max}" ] || max=$((timeout*2))

if [ "${1}" = "config" ]; then
  echo "graph_title wget loadtime of webpages"
  echo "graph_args --base 1000 -l 0"
  echo "graph_scale no"
  echo "graph_vlabel Load time in seconds"
  echo "graph_category http"
  echo "graph_info This graph shows load time in seconds of one or more urls"
  I=1
  for name in ${names}; do
    eval iurl='${url_'${name}'}'
    if [ -n "${iurl}" ]; then
      eval ilabel='${label_'${name}':-url${I}}'
      eval iwarning='${warning_'${name}':-${warning}}'
      eval icritical='${critical_'${name}':-${critical}}'
      eval imax='${max_'${name}':-${max}}'
      cat << EOH
loadtime${I}.label ${ilabel}
loadtime${I}.info Load time for ${iurl}
loadtime${I}.min 0
loadtime${I}.max ${imax}
loadtime${I}.warning ${iwarning}
loadtime${I}.critical ${icritical}
EOH
      I=$((I+1))
    fi
  done
  exit 0
fi

I=1
for name in ${names}; do
  eval iurl='${url_'${name}'}'
  if [ -n "${iurl}" ]; then
    eval ierror_value='${error_value_'${name}':-${error_value}}'
    eval iwget_opts='${wget_opts_'${name}':-${wget_opts}}'
    eval itimeout='${timeout_'${name}':-${timeout}}'
    timing=`${time_bin} -p ${wget_bin} --no-directories --output-document=/dev/null --timeout=${itimeout} ${iwget_opts} "${iurl}" 2>&1`
    if [ $? -ne 0 -a ${ierror_value} -ne 0 ]; then
      loadtime=${ierror_value}
    else
      loadtime=`echo "${timing}" | grep "^real *[0-9]" | cut -d ' ' -f 2`
    fi
    echo "loadtime${I}.value ${loadtime}"
    I=$((I+1))
  fi
done


