Skip to content
Snippets Groups Projects
check_onevm 6.93 KiB
#!/bin/bash
# ======================================================================
#
# Check ONEVM
# show virtual machines
## requirements:
# - sudo onevm
#
# ----------------------------------------------------------------------
#
# short state, state, meaning
# see https://docs.opennebula.io/6.6/management_and_operations/vm_management/vm_instances.html
#
# STAT:pend:Pending:By default a VM starts in the pending state, waiting for a resource to run on. It will stay in this state until the scheduler decides to deploy it, or the user deploys it using the onevm deploy command.
# STAT:hold:Hold:The owner has held the VM and it will not be scheduled until it is released. It can be, however, deployed manually.
# STAT:clon:Cloning:The VM is waiting for one or more disk images to finish the initial copy to the repository (image state still in lock)
# STAT:prol:Prolog:The system is transferring the VM files (disk images and the recovery file) to the host in which the virtual machine will be running.
# STAT:boot:Boot:OpenNebula is waiting for the hypervisor to create the VM.
# STAT:runn:Running:The VM is running (note that this stage includes the internal virtualized machine booting and shutting down phases). In this state, the virtualization driver will periodically monitor it.
# STAT:migr:Migrate:The VM is migrating from one resource to another. This can be a life migration or cold migration (the VM is saved, powered-off or powered-off hard and VM files are transferred to the new resource).
# STAT:hotp:Hotplug:A disk attach/detach, nic attach/detach operation is in process.
# STAT:snap:Snapshot:A system snapshot is being taken.
# STAT:save:Save:The system is saving the VM files after a migration, stop or suspend operation.
# STAT:epil:Epilog:In this phase the system cleans up the Host used to virtualize the VM, and additionally disk images to be saved are copied back to the system datastore.
# STAT:shut:Shutdown:OpenNebula has sent the VM the shutdown ACPI signal, and is waiting for it to complete the shutdown process. If after a timeout period the VM does not disappear, OpenNebula will assume that the guest OS ignored the ACPI signal and the VM state will be changed to running, instead of done.
# STAT:stop:Stopped:The VM is stopped. VM state has been saved and it has been transferred back along with the disk images to the system datastore.
# STAT:susp:Suspended:Same as stopped, but the files are left in the host to later resume the VM there (i.e. there is no need to re-schedule the VM).
# STAT:poff:PowerOff:Same as suspended, but no checkpoint file is generated. Note that the files are left in the host to later boot the VM there. When the VM guest is shutdown, OpenNebula will put the VM in this state.
# STAT:unde:Undeployed:The VM is shut down. The VM disks are transferred to the system datastore. The VM can be resumed ater.
# STAT:fail:Failed:The VM failed.
# STAT:unkn:Unknown:The VM couldn’t be reached, it is in an unknown state.
# STAT:clea:Cleanup-resubmit:The VM is waiting for the drivers to clean the host after a onevm recover --recreate action
# STAT:done:Done:The VM is done. VMs in this state won’t be shown with onevm list but are kept in the database for accounting purposes. You can still get their information with the onevm show command.
# ----------------------------------------------------------------------
# 2023-06-12  v1.0  <axel.hahn@unibe.ch>  initial version
# 2023-06-15  v1.1  <axel.hahn@unibe.ch>  fix output if no vm is running
# 2023-08-23  v1.2  <axel.hahn@unibe.ch>  update help; show help without requirements
# ======================================================================


. $(dirname $0)/inc_pluginfunctions

self_APPVERSION=1.2

# new line
NL="
"
out=""
showData=0

# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------

function showHelp(){
    local _self; _self=$(basename $0)
cat <<EOF
$( ph.showImlHelpHeader )

Show count of vms in OpenNebula - listed by state and by host.
It will go to warning if a non running state was found.
It will go to critical if a vm is on failure.

SYNTAX:
$_self

    -h or --help   show this help.

PARAMETERS:

    None.

EOF
}


function _add(){
        out+="1$*${NL}"
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# --- check param -h
case "$1" in
    "--help"|"-h")
        showHelp
        exit 0
        ;;
    *)
esac

# --- check required tools
ph.require onehost

# --- get data
cmdout=$( sudo onevm list --csv 2>&1 )

if ! grep "ID,USER" <<< "$cmdout" >/dev/null; then
        ph.setStatus "unknown"
        echo "UNKNOWN: sudo onevm failed."
        echo "$cmdout"
        ph.exit
fi

# header=$( head -1 <<< "$cmdout" )
csvdata=$( echo "$cmdout" | sed -n '2,$p' )

# --- get result

out=""
typeset -i iTotal;    iTotal=$(   grep -c .        <<< "$csvdata" )
typeset -i iRunning;  iRunning=$( grep -c ",runn," <<< "$csvdata" )
typeset -i iFail;     iFail=$(    grep -c ",fail," <<< "$csvdata" )
typeset -i iUnknown;  iUnknown=$( grep -c ",unkn," <<< "$csvdata" )
typeset -i iOther;    iOther=$iTotal-$iRunning-$iFail-$iUnknown

ph.perfadd "total"    "${iTotal}"
ph.perfadd "running"  "${iRunning}"
ph.perfadd "fail"     "${iFail}"
ph.perfadd "unknown"  "${iUnknown}"
ph.perfadd "other"    "${iOther}"

if [ $iTotal -gt 0 ]; then
    if [ $iUnknown -ne 0 ]; then
        ph.setStatus "warning"
        out+="WARNING: a vm with status [Unknown] was found.${NL}"
    fi

    if [ $iOther -ne 0 ]; then
        ph.setStatus "warning"
        out+="WARNING: There is a VM that has another status than [running].${NL}"
    fi

    if [ $iFail -ne 0 ]; then
        ph.setStatus "critical"
        out+="ERROR: a vm with status [Failed] was found.${NL}"
    fi

    out+=$( 
        echo '>>>>>> By state'
        grep "^# STAT:" "$0" | while read -r line
        do
                IFS=':' read -r prefix statusS statusL descr <<<"$line"
                data=$( grep ",${statusS}," <<< "$csvdata" )
                typeset -i count; count=$( wc -l <<< "$data" )
                if [ -n "$data" ]; then
                        echo "--- $statusL ($statusS): $count"
                        echo "$descr"
                        if [ $showData -ne 0 ] || [ "${statusS}" != "runn" ]; then
                            echo "$data"
                        fi
                fi
        done
        echo 
    )

    out+=$(
        echo
        echo '>>>>>> By host:'
        for onehost in $( cut -f 8 -d ',' <<< "$csvdata" | sort -u )
        do
                echo -n "--- $onehost: "
                grep -c ",${onehost}," <<< "$csvdata"
                test $showData -ne 0 && grep ",${onehost}," <<< "$csvdata" | nl
        done
        echo
    )
else
    out+="Info: no vm was created yet."
fi

ph.status "OpenNebula VMs - Total: $iTotal .. running: $iRunning, Fail: $iFail, unknown: $iUnknown .. other: $iOther"
echo "$out"
ph.exit