Repository
Munin (contrib)
Last change
2021-01-17
Graph Categories
Family
auto
Capabilities
Keywords
Language
Shell
Authors

mpdstats_

Example graph: day

Name

mpdstats_ - Munin plugin to monitor a MPD database

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.

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.

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.

Different netcat implementations use different flags. In the MPD command protocol, it is up to the client to close the connection, so you want to use a timeout flag. For instance, Fedora/Debian will use ncat, which uses ‘-w’ as a timeout option. You can configure this using the ‘netcat_args’ environment variable.

Configuration Example

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

Author

Copyright (C) 2012 ToM tom@leloop.org

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.

Magic Markers

#%# family=auto
#%# capabilities=autoconf suggest
#!/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.

Different netcat implementations use different flags. In the MPD command
protocol, it is up to the client to close the connection, so you want to use
a timeout flag. For instance, Fedora/Debian will use ncat, which uses '-w'
as a timeout option.
You can configure this using the 'netcat_args' environment variable.

=head2 CONFIGURATION EXAMPLE

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

=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)}
NCARGS=${netcat_args:-""}

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

#
# FUNCTIONS
#

do_autoconf () {
    # shellcheck disable=SC2086
    if [ -z "$NCBIN" ] ; then
        echo "no (missing netcat program ('nc'))"
    elif ! echo stats | "$NCBIN" $NCARGS "$MPDHOST" "$MPDPORT" >/dev/null 2>&1; then
        echo "no (connection failed)"
    else
        echo "yes"
    fi
    exit 0
}


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


print_mpd_stat_value() {
    local key="$1"
    local fieldname
    local stat_value
    fieldname="$(clean_fieldname "$key")"
    # shellcheck disable=SC2086
    stat_value="$(echo stats | "$NCBIN" $NCARGS "$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