#!/bin/sh

# vim: set ft=sh :
# -*- sh -*-

: << =cut

=head1 NAME

mpdstats_ - Munin plugin to monitor a MPD database

=head1 DEPENDENCIES

This plugin uses netcat(1) to communicate with the MPD daemon.

Tip: To see if it is already set up correctly, just run this plugin
with the parameter "autoconf". If you get a "yes", everything should
work like a charm already.

=head1 INSTALLATION

This wildcard plugin can be symlinked using one of the statistic
categories of mpd. See the output of "echo stats | nc MPD_HOST 6600".
Without a symlink configuration (no suffix after the underscore) all
stats are shown.

=head1 CONFIGURATION

No configuration should be required if run on the same server as
MPD. If the plugin is run from remote or in a non-default configuration,
please use the environment variables 'mpd_host' and 'mpd_port' to connect.

Also, if your netcat(1) binary is anywhere else than /bin/nc please define
it using the 'netcat' environment variable.

=head2 CONFIGURATION EXAMPLE

 [mpdstats_*]
  env.mpd_host 192.168.0.43
  env.mpd_port 6606
  env.netcat /usr/local/bin/nc

=head1 AUTHOR

Copyright (C) 2012 ToM <tom@leloop.org>

=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=auto
 #%# capabilities=autoconf suggest

=cut


. "$MUNIN_LIBDIR/plugins/plugin.sh"

MPDHOST=${mpd_host:-localhost}
MPDPORT=${mpd_port:-6600}
NCBIN=${netcat:-$(which nc)}

ACTION="$(basename "$0" | sed 's/^.*_//')"

#
# FUNCTIONS
#

do_autoconf () {
    if [ -z "$NCBIN" ] ; then
        echo "no (missing netcat program ('nc'))"
        exit 1
    fi

    if ! echo version | "$NCBIN" "$MPDHOST" "$MPDPORT" >/dev/null 2>&1; then
        echo "no (connection failed)"
        exit 1
    fi

    echo "yes"
    exit 0
}


get_mpd_stats_keys() {
    echo stats | "$NCBIN" "$MPDHOST" "$MPDPORT" | awk 'BEGIN {FS=":"} /^[^ ]+:/ {print $1}'
}


print_mpd_stat_value() {
    local key="$1"
    local fieldname
    local stat_value
    fieldname="$(clean_fieldname "$key")"
    stat_value="$(echo stats | "$NCBIN" "$MPDHOST" "$MPDPORT" | awk "/^${key}:/ {print \$2}")"
    echo "${fieldname}.value $stat_value"
}


#
# MAIN
#


case "$1" in
    autoconf)
        do_autoconf
        ;;
    suggest)
        get_mpd_stats_keys
        ;;
    config)
        echo "graph_category streaming"
        echo "graph_args --base 1000 -l 0"
        echo "graph_scale no"
        if [ -z "$ACTION" ]; then
            echo "graph_title MPD Statistics"
            echo "graph_vlabel number of items"
            for stat_key in $(get_mpd_stats_keys); do
                fieldname="$(clean_fieldname "$stat_key")"
                echo "${fieldname}.type GAUGE"
                echo "${fieldname}.draw LINE"
                echo "${fieldname}.label $stat_key"
            done
        else
            # Show only a single stat
            echo "graph_title $ACTION in the MPD database"
            echo "graph_info Number of $ACTION in the database."
            echo "graph_vlabel $ACTION in the db"
            echo "$ACTION.type GAUGE"
            echo "$ACTION.draw LINE2"
            echo "$ACTION.label $ACTION"
        fi
        ;;
    *)
        if [ -z "$ACTION" ]; then
            for stat_key in $(get_mpd_stats_keys); do
                print_mpd_stat_value "$stat_key"
            done
        else
            print_mpd_stat_value "$ACTION"
        fi
        ;;
esac
exit 0
