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

: << =cut

=head1 NAME

vg_ - Wildcard-plugin to monitor lvm2 volume groups and the
logival volumes in that group.

=head1 CONFIGURATION

Configuration:
[vg_*]
user root

The plugin needs to binaries from the lvm tools, lvs and vgs
It will use the lvs and vgs found in default PATH, if you need
to change this you need to configure this:

  [vg_*]
     env.LVS /sbin/lvs
     env.VGS /sbin/vgs

This is a wildcard plugin. To monitor a volume group , link
vg_<volume group name> to this file. E.g.

  ln -s /usr/share/munin/plugins/vg_ \
        /etc/munin/plugins/vg_local

...will monitor the volume group named local.

=head1 AUTHOR

Jonas Forsberg - jonas@forsberg.co

=head1 LICENSE
# This file may be licensed under the terms of of the
# GNU General Public License Version 2 (the ``GPL'').
#
# Software distributed under the License is distributed
# on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
# express or implied. See the GPL for the specific language
# governing rights and limitations.
#
# You should have received a copy of the GPL along with this
# program. If not, go to http://www.gnu.org/licenses/gpl.html
# or 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

=head1 VERSION
 
 0.1

=cut

# -----
[ -z "$VGS" ] && VGS="$(which vgs)"
[ -z "$LVS" ] && LVS="$(which lvs)"

VOLUMEGROUP=${0##*vg_}

do_ () {
    echo "free.value $($VGS --nosuffix --noheadings --unit b -o vg_free  $VOLUMEGROUP | sed 's/ //g')"
    echo "size.value $($VGS --nosuffix --noheadings --unit b -o vg_size  $VOLUMEGROUP | sed 's/ //g')"
    # because of bug http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=601124 we can not have a field named root
    # adding lv_ to every logical volume name
    $LVS --noheading --unit b --nosuffix --options lv_name,lv_size $VOLUMEGROUP | sed 's/\./_/g' | awk '{print "lv_"$1".value "$2}'
}

do_autoconf () {
    [ $VGS ] || { echo "no (vgs not found)" ; exit 0 ;}
    [ $LVS ] || { echo "no (lvs not found)" ; exit 0 ;}
    echo "yes"
    exit 0
}

do_suggest () {
    $VGS --noheadings -o vg_name | sed 's/ //g'
}

do_config () {
    cat <<EOF
graph_title Volume group $VOLUMEGROUP
graph_info Information about the volume group
graph_vlabel bytes
graph_category disk
graph_args --base 1024 -l 0
graph_scale yes
size.label Size
size.info Total size of volume group
size.draw LINE1   
EOF
    # because of bug http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=601124 we can not have a field named root
    # adding lv_ to every logical volume name
   $LVS --noheading --unit b --nosuffix --options lv_name $VOLUMEGROUP | \
   sed 's/\./_/g' | \
   awk '{ {print "lv_"$1".label "$1}
          {print "lv_"$1".info "$1" Logical Volume Size"}
          {print (NR==1 ? "lv_"$1".draw AREA" : "lv_"$1".draw STACK")}
        }'
   cat <<EOF
free.label Free
free.info Volume Group Free space
free.draw STACK
EOF
}


do_$1

