#!@@GOODSH@@
# -*- sh -*-

: <<=cut

=head1 NAME

munin_update - Munin plugin to graph the time to query about each host from the nodes.

=head1 APPLICABLE SYSTEMS

Munin master servers.

=head1 CONFIGURATION

Normaly needs no configuration.  You may configure it with the
following parameter:

  [munin*]
      env.UPDATE_STATSFILE .../munin-update.stats
      env.MUNIN_UPDATE_LOCACTION .../munin-update

The first is the statistics file for munin update.

The exact location of this file is package/site specific, but
munin_update will know where it is unless you have made changes.

=head1 INTERPRETATION

The script reads the munin-update "stats" file to determine how long
it takes to query the nodes about each host configured in Munin.

Munin is run from cron every 5 minutes and before the next run of
munin-update the previous run needs to be done.  Each run of
munin-update forks one process pr. host that needs to get data
collected, so all collection runs in paralell.

Any host that is slow, for example slower than 4 miniutes, causes a
risk that the next run of munin-update must be cancled due to the
lateness of the previous run.  In such cases there will be single line
gaps in the "by day" graph.

Keep your hosts updating quickly and all will be well.

=head1 MAGIC MARKERS

  #%# family=manual
  #%# capabilities=autoconf

=head1 BUGS

Munin-update is always run at the same time as this plugin runs -
therefore the stats file may be incompletely written and the plugin
will likely show a incomplete list of hosts.  It should be using
munin-update.old-stats, which is not currently even made.

Munin-update removes the "domain" information on all hosts.  If there
are two hosts with the same host name in different domains then one of
them will be disappeared by the munin-update collection process.


=head1 AUTHOR

The munin_update plugin has been included in munin for many years (at
least 2004).  The most likely author is one of the original munin team.

Documentation and updating to 2009 for Munin 1.4 by Nicolai Langfeldt.

(C) 2004-2009 The Munin Team, Redpill Linpro AS

=head1 LICENSE

GPLv2

=cut

. "$MUNIN_LIBDIR/plugins/plugin.sh"


UPDATE_STATSFILE=${UPDATE_STATSFILE:-$MUNIN_DBDIR/munin-update.stats}
MUNIN_UPDATE_LOCATION=${MUNIN_UPDATE_LOCATION:-$MUNIN_LIBDIR/munin-update}


if [ "$1" = "autoconf" ]; then
	if [ -e "$MUNIN_UPDATE_LOCATION" ] ; then
	    echo "yes"
	else
	    echo "no ($MUNIN_UPDATE_LOCATION is not present so this is not a munin-master)"
	fi
	exit 0
fi

if [ "$1" = "config" ]; then
	[ -f "$UPDATE_STATSFILE" ] || {
	    echo 'graph_title Plugin error'
	    echo "graph_info  Plugin cannot read stats file $UPDATE_STATSFILE"
	    echo 'error.label Error'
	    echo 'error.critical 1'
	    exit 0
	}

	echo 'graph_title Munin-update'
	echo 'graph_vlabel seconds'
	echo 'graph_category munin'
	echo 'graph_info This graph shows the time it takes to collect data from each hosts that munin collects data on. Munin-master is run from cron every 5 minutes and we want each of the munin-update runs to complete before the next one starts.  If munin-update uses too long time to run on one host run it with --debug to determine which plugin(s) are slow and solve the problem with them if possible.'
	sed '/^UD|/!d; s/.*;//; s/|/ /;' < "$UPDATE_STATSFILE" | sort |
	while read -r i j; do
            name="$(clean_fieldname "$i")"
	    echo "$name.label $i"
	    echo "$name.warning 240"
	    echo "$name.critical 285"
	done
	exit 0
fi

[ -f "$UPDATE_STATSFILE" ] || {
    echo 'error.value 1'
    echo "error.extinfo Plugin cannot read stats file $UPDATE_STATSFILE"
    exit 0
}

sed '/^UD|/!d; s/.*;//; s/|/ /;' < "$UPDATE_STATSFILE" | sort |
while read -r i j; do
        name="$(clean_fieldname "$i")"
	echo "$name.value $j"
done
