#!/bin/sh
# -*- sh -*-

: << =cut

=head1 NAME

http_responsecode - Is the site up?

=head1 CONFIGURATION

The following environment variables are used

 sites       - Sites to check
 max_time    - Timeout for each site check - defaults to 5 seconds
 host_name   - To put the graph onto a different host

Configuration example

 [http_responsecode]
  env.sites example.com example2.de
  env.max_time 5

You can display the graph on another host (e.g., the actual server) than the
one running http_responsecode.
To do so, first configure the plugin to use a different hostname.

    env.host_name server

Then configure munin (in /etc/munin/munin-conf or /etc/munin/munin-conf.d), to
support a new host.

  [example.net;server]
    address 127.0.0.1
    use_node_name no

=head1 AUTHOR

Copyright (C) 2013 Thomas Heidrich

=head1 LICENSE

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

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

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

=head1 MAGIC MARKERS

 #%# family=manual

=cut

if [ "$1" = "config" ]; then
    echo 'graph_args --base 1000 -l 0 -u 511'
    echo 'graph_title HTTP Response Codes'
    echo 'graph_vlabel Code'
    echo 'graph_category network'
    echo 'graph_info This graph shows HTTP response code statistics.'
    echo 'graph_printf %3.0lf'
    if [ -n "$host_name" ]; then
        echo "host_name $host_name"
    fi
    for site in $sites; do
	siteid=`echo $site | sed 's/[^a-z]*//g'`
	echo "$siteid.label $site"
	echo "$siteid.info HTTP response code statistics for $site."
	echo "$siteid.critical 99:399";
    done
    exit 0
fi

for site in $sites; do
    export siteid=`echo $site | sed 's/[^a-z]*//g'`
    echo -n "$siteid.value "
    curl --write-out %{http_code} --max-time "${max_time:-5}" --silent --output /dev/null "${site}"
    echo
done
