#!/bin/bash

: << =cut

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf suggest

=cut

TYPE="node"
NC_STAT="/var/run/eucalyptus/nc-stats"
SC_STAT="/var/log/eucalyptus/sc-stats.log"
WALRUS_STAT="/var/log/eucalyptus/walrus-stats.log"
GMETRIC="`which gmetric 2> /dev/null`"
DEBUG="N"
EUCALYPTUS="/"

usage () {

	echo "$0 [options]"
	echo
	echo "   -help                       this message"
	echo "   -type [nc|walrus|sc]        which stat file to look for"
	echo "   -d <eucalyptus_dir>         where eucalyptus is installed"
    echo "   -munin                      force munin mode (normally autodetects)"
	echo
}

# let's parse the command line
basename=`basename $0`
if [[ "$basename" =~ ^euca_ ]] ; then
    # munin mode
    MUNIN=1
    MUNIN_MODE=$1
    if [ -z "$MUNIN_MODE" ] ; then
        MUNIN_MODE="default"
    fi
    PLUGIN_MODE=${0##*euca_}
else
    MUNIN=0
    MUNIN_MODE=none
    PLUGIN_MODE=none
    while [ $# -gt 0 ]; do
        if [ "$1" = "-h" -o "$1" = "-help" -o "$1" = "?" -o "$1" = "--help" ]; then
            usage
            exit 0
        fi

        if [ "$1" = "-type" ]; then
            if [ -z "$2" ]; then
                echo "Need the type!"
                exit 1
            fi
            TYPE="$2"
            shift; shift
            continue
        fi
        if [ "$1" = "-debug" ]; then
            DEBUG="Y"
            shift
            continue
        fi
        if [ "$1" = "-d" ]; then
            if [ -z "$2" ]; then
                echo "Need eucalyptus directory!"
                exit 1
            fi
            EUCALYPTUS="$2"
            shift; shift
            continue
        fi
        usage
        exit 1
    done
    if [ -z "$GMETRIC" ]; then
        echo "Cannot find gmetric: do you have ganglia installed?"
        exit 1
    fi
fi

# some checks
if [ ! -e "$EUCALYPTUS/etc/eucalyptus/eucalyptus.conf" ]; then
    if [ $MUNIN -a $MUNIN_MODE = "autoconf" ] ; then
        echo "no (/etc/eucalyptus/eucalyptus.conf not found)"
        exit 0
    fi
	echo "Is Eucalyptus installed in $EUCALYPTUS?"
	exit 1
fi
if [ $MUNIN -a $MUNIN_MODE = "autoconf" ] ; then
    echo yes
    exit 0
fi
if [ "$TYPE" = "nc" -o $MUNIN ]; then
	# let's check we have the stat file
	if [ -e ${EUCALYPTUS}${NC_STAT} ]; then
        # number of running VMs
        NUM="`grep ^id: ${EUCALYPTUS}${NC_STAT}|wc -l`"
        # memory available to VMs
        M_AVAIL="`grep ^memory ${EUCALYPTUS}${NC_STAT}|sed \"s;memory.*MB: [[:digit:]]*/\([[:digit:]]*\)/[[:digit:]]*;\1;\"`"
        # memory used by VMs
        M_USED="`grep ^memory ${EUCALYPTUS}${NC_STAT}|sed \"s;memory.*MB: [[:digit:]]*/[[:digit:]]*/\([[:digit:]]*\);\1;\"`"
        # cores available to VMs
        C_AVAIL="`grep ^cores ${EUCALYPTUS}${NC_STAT}|sed \"s;cores.*: [[:digit:]]*/\([[:digit:]]*\)/[[:digit:]]*;\1;\"`"
        # cores used by VMs
        C_USED="`grep ^cores ${EUCALYPTUS}${NC_STAT}|sed \"s;cores.*: [[:digit:]]*/[[:digit:]]*/\([[:digit:]]*\);\1;\"`"
        # disk available to VMs
        D_AVAIL="`grep ^disk ${EUCALYPTUS}${NC_STAT}|sed \"s;disk.*GB: [[:digit:]]*/\([[:digit:]]*\)/[[:digit:]]*;\1;\"`"
        # disk used by VMs
        D_USED="`grep ^disk ${EUCALYPTUS}${NC_STAT}|sed \"s;disk.*GB: [[:digit:]]*/[[:digit:]]*/\([[:digit:]]*\);\1;\"`"
        
        if [ $MUNIN ] ; then
            if [ $MUNIN_MODE = "suggest" ] ; then
                echo nc_memory
                echo nc_vms
                echo nc_disk
            elif [ "$PLUGIN_MODE" = "nc_memory" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo "graph_title Node Controller Memory"
                    echo "graph_args --base 1024"
                    echo "graph_vlabel Memory(MB)"
                    echo "graph_category eucalyptus"
                    echo "graph_info This graph shows how much of the node controllers memory is allocated."
                    echo graph_order avail used
                    echo avail.draw AREA
                    echo avail.label MB avail
                    echo used.draw AREA
                    echo used.label MB used
                else
                    echo avail.value $M_AVAIL
                    echo used.value $M_USED
                fi
            elif [ "$PLUGIN_MODE" = "nc_vms" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo graph_title Node Controller Cores and VMs
                    echo graph_vlabel Cores/VMs
                    echo graph_category eucalyptus
                    echo graph_info This graph shows how many cores and VMs are in use/available.
                    echo graph_order num cores_avail cores_used
                    echo num.draw LINE1
                    echo num.label VMs running
                    echo cores_avail.draw AREA
                    echo cores_avail.label Cores Available
                    echo cores_used.draw AREA
                    echo cores_used.label Cores Used
                else
                    echo num.value $NUM
                    echo cores_avail.value $C_AVAIL
                    echo cores_used.value $C_USED
                fi
            elif [ "$PLUGIN_MODE" = "nc_disk" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo graph_title Node Controller Disk Resources
                    echo graph_args --base 1024
                    echo graph_vlabel GB of Space
                    echo graph_category eucalyptus
                    echo graph_info This graph shows usage of Node Controller disk resources
                    echo graph_order avail used
                    echo avail.draw AREA
                    echo avail.label GB available
                    echo used.draw AREA
                    echo used.label GB used
                else
                    echo avail.value $D_AVAIL
                    echo used.value $D_USED
                fi
            fi
        else
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "Running VMs" -v $NUM -t int16 
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "VMs available memory" -v $M_AVAIL -t int32 -u MB
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "VMs used memory" -v $M_USED -t int32 -u MB
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "VMs available cores" -v $C_AVAIL -t int32 
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "VMs used cores" -v $C_USED -t int32 
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "VMs available disks" -v $D_AVAIL -t int32 -u GB
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "VMs used disks" -v $D_USED -t int32 -u GB

            $GMETRIC -n "Running VMs" -v $NUM -t int16 
            $GMETRIC -n "VMs available memory" -v $M_AVAIL -t int32 -u MB
            $GMETRIC -n "VMs used memory" -v $M_USED -t int32 -u MB
            $GMETRIC -n "VMs available cores" -v $C_AVAIL -t int32 
            $GMETRIC -n "VMs used cores" -v $C_USED -t int32 
            $GMETRIC -n "VMs available disks" -v $D_AVAIL -t int32 -u GB
            $GMETRIC -n "VMs used disks" -v $D_USED -t int32 -u GB
            exit 0
        fi
    elif [ ! $MUNIN ] ; then
		echo "Cannot find NC stat file!"
		exit 1
	fi
fi

if [ "$TYPE" = "sc" -o $MUNIN ]; then
	# let's check we have the stat file
	if [ -e ${EUCALYPTUS}${SC_STAT} ]; then

        V_USED="`tail -n 1 ${EUCALYPTUS}${SC_STAT}|sed \"s/.*Volumes: \([[:digit:]]*\).*/\1/\"`"
        S_USED="`tail -n 1 ${EUCALYPTUS}${SC_STAT}|sed \"s/.*Space Used: \([[:digit:]]*\)/\1/\"`"

        if [ $MUNIN ] ; then
            if [ $MUNIN_MODE = "suggest" ] ; then
                echo sc_volumes
                echo sc_disk
            elif [ "$PLUGIN_MODE" = "sc_volumes" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo graph_title Storage Controller Volumes
                    echo graph_vlabel Volumes
                    echo graph_category eucalyptus
                    echo graph_info This graph shows usage of Volumes on the Storage Controller
                    echo used.draw AREA
                    echo used.label Volumes in Use
                else
                    echo used.value $V_USED
                fi
            elif [ "$PLUGIN_MODE" = "sc_disk" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo graph_title Storage Controller Disk Resources
                    echo graph_args --base 1024
                    echo graph_vlabel GB of Space
                    echo graph_category eucalyptus
                    echo graph_info This graph shows usage of Storage Controller disk space
                    echo used.draw AREA
                    echo used.label GB disk space in use
                else
                    echo used.value $S_USED
                fi
            fi
        else

            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "Volumes" -v $V_USED -t int16 
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "Volumes disk usage" -v $S_USED -t int16 

            $GMETRIC -n "Volumes" -v $V_USED -t int16 
            $GMETRIC -n "Volumes disk usage" -v $S_USED -t int16 
            exit 0
        fi
    elif [ ! $MUNIN ] ; then
		echo "Cannot find SC stat file!"
		exit 1
	fi
fi

if [ "$TYPE" = "walrus" -o $MUNIN ]; then
	# let's check we have the stat file
	if [ -e ${EUCALYPTUS}${WALRUS_STAT} ]; then

        B_USED="`tail -n 1 ${EUCALYPTUS}${WALRUS_STAT}|sed \"s/.*Buckets: \([[:digit:]]*\).*/\1/\"`"
        S_USED="`tail -n 1 ${EUCALYPTUS}${WALRUS_STAT}|sed \"s/.*Space Used: \([[:digit:]]*\)/\1/\"`"

        if [ $MUNIN ] ; then
            if [ $MUNIN_MODE = "suggest" ] ; then
                echo "walrus_buckets"
                echo "walrus_disk"
            elif [ "$PLUGIN_MODE" = "walrus_buckets" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo graph_title Walrus Buckets
                    echo graph_vlabel Buckets
                    echo graph_category eucalyptus
                    echo graph_info This graph shows usage of Buckets on Walrus
                    echo used.draw AREA
                    echo used.label Buckets in use
                else
                    echo used.value $B_USED
                fi
            elif [ "$PLUGIN_MODE" = "walrus_disk" ] ; then
                if [ "$MUNIN_MODE" = "config" ] ; then
                    echo graph_title Walrus Disk Resources
                    echo graph_args --base 1024
                    echo graph_vlabel GB of Space
                    echo graph_category eucalyptus
                    echo graph_info This graph shows usage of Walrus disk space
                    echo used.draw AREA
                    echo used.label GB of space in use
                else
                    echo used.value $S_USED
                fi
            fi
        else

            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "Buckets" -v $B_USED -t int16 
            [ "$DEBUG" = "Y" ] && echo $GMETRIC -n "Buckets disk usage" -v $S_USED -t int16 

            $GMETRIC -n "Buckets" -v $B_USED -t int16 
            $GMETRIC -n "Buckets disk usage" -v $S_USED -t int16 
            exit 0
        fi
    elif [ ! $MUNIN ] ; then
		echo "Cannot find wlarus stat file!"
		exit 1
	fi
fi
if [ ! $MUNIN ] ; then
	echo "Unknown type!"
	exit 1
fi
