From cb615a663d78d9ef6d261eb0c8079adb83ae08ad Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 12 Jun 2023 14:58:25 +0200
Subject: [PATCH] add onevm

---
 check_onevm | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 158 insertions(+)
 create mode 100644 check_onevm

diff --git a/check_onevm b/check_onevm
new file mode 100644
index 0000000..66547c1
--- /dev/null
+++ b/check_onevm
@@ -0,0 +1,158 @@
+#!/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
+# ======================================================================
+
+
+. $(dirname $0)/inc_pluginfunctions
+
+self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
+self_APPVERSION=1.0
+
+# ----------------------------------------------------------------------
+# functions
+# ----------------------------------------------------------------------
+
+function showHelp(){
+cat <<EOF
+______________________________________________________________________
+
+$self_APPNAME 
+v$self_APPVERSION
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+______________________________________________________________________
+
+show count of vms in OpenNebula
+
+SYNTAX:
+$(basename $0)
+
+    -h or --help   show this help.
+
+PARAMETERS:
+
+    None.
+
+EOF
+}
+
+
+function _add(){
+        out+="1$*${NL}"
+}
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+# --- check required tools
+ph.require onehost
+
+# --- check param -h
+case "$1" in
+    "--help"|"-h")
+        showHelp
+        exit 0
+        ;;
+    *)
+esac
+
+showData=0
+
+# --- get data
+cmdout=$( sudo onevm list --csv 2>&1 )
+
+if ! grep "ID,USER" <<< "$cmdout" >/dev/null; then
+        ph.setStatus "unknown"
+        echo "UNKNOWN: sudo onehost failed."
+        echo "$cmdout"
+        ph.exit
+fi
+
+# header=$( head -1 <<< "$cmdout" )
+csvdata=$( echo "$cmdout" | sed -n '2,$p' )
+
+# --- get result
+
+out=""
+typeset -i iTotal;    iTotal=$(   echo "$csvdata" | wc -l )
+typeset -i iRunning;  iRunning=$( echo "$csvdata" | grep -c ",runn," )
+typeset -i iOther;    iOther=$iTotal-$iRunning
+
+ph.perfadd "total"    "${iTotal}"
+ph.perfadd "running"  "${iRunning}"
+ph.perfadd "other"     "${iOther}"
+
+if [ $iOther -ne 0 ]; then
+    ps.setStatus "warning"
+    out+="WARNING: There is a VM that has another status than [running]."
+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
+)
+
+
+ph.status "OpenNebula VMs - Total: $iTotal .. running: $iRunning .. other: $iOther"
+echo "$out"
+ph.exit
\ No newline at end of file
-- 
GitLab