#!/bin/sh
#
# Plugin to monitor running and registered virtual machines in the system.
#
# Parameters:
#
#	config   (required)
#	autoconf (optional - used by munin-config)
#
#%# family=auto
#%# capabilities=autoconf

if [ "$1" = "autoconf" ]; then
    if [ -x /usr/bin/vmware-vim-cmd ]; then
	echo yes
	exit 0
    else
	echo no
	exit 1
    fi
fi


if [ "$1" = "config" ]; then
    echo 'graph_title VMware virtual machines'
    echo 'graph_vlabel number of virtual machines'
    echo 'graph_category vmware'
    echo 'graph_info This graph monitors registered and running virtual machines.'

    echo 'running.label running'
    echo 'running.info Running virtual machines.'

    echo 'registered.label registered'
    echo 'registered.info Registered virtual machines.'
    exit 0
fi

vmware-vim-cmd vmsvc/getallvms | awk '
    BEGIN {
      registered = 0;
      running = 0;
    }
    {
      if (/^[0-9]+/) {
        registered++;
	("vmware-vim-cmd vmsvc/power.getstate " $1 " | grep Powered") | getline state;
	if (state == "Powered on") {
	  running++;
	}
      }
    }
    END {
      print "registered.value " registered
      print "running.value " running
    }
'
