From 2c14a97846dd6871e1aeb66305aebecf70c4b4de Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 9 Jun 2023 15:08:28 +0200
Subject: [PATCH] add check check_onehost

---
 check_onehost | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 check_onehost

diff --git a/check_onehost b/check_onehost
new file mode 100644
index 0000000..20c89c5
--- /dev/null
+++ b/check_onehost
@@ -0,0 +1,107 @@
+#!/bin/bash
+# ======================================================================
+#
+# Check ONEHOST
+#
+# requirements:
+# - sudo onehost
+#
+# ----------------------------------------------------------------------
+# 2023-06-09  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 hosts in OpenNebula and warn if a host is down.
+
+SYNTAX:
+$(basename $0) [ -w value -c value -h ]
+
+    -w VALUE       cpu usage warning level  (default: 1)
+    -c VALUE       cpu usage critical level (default: 2)
+    -h or --help   show this help.
+
+PARAMETERS:
+
+    None.
+
+EXAMPLE:
+$(basename $0) -c 1   set to critical if the 1st host is off.
+
+EOF
+}
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+# --- check required tools
+ph.require onehost
+
+# --- check param -h
+case "$1" in
+    "--help"|"-h")
+        showHelp
+        exit 0
+        ;;
+    *)
+esac
+
+# --- set optional limits
+typeset -i iWarnLimit=$(     ph.getValueWithParam 1 w "$@")
+typeset -i iCriticalLimit=$( ph.getValueWithParam 2 c "$@")
+
+
+# --- get data
+cmdout=$( onehost  list --csv 2>&1 )
+
+if ! grep "ID,NAME" <<< "$cmdout" >/dev/null; then
+        ph.setStatus "unknown"
+        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 iOn;    iOn=$(    echo "$csvdata" | grep -c "on$" )
+typeset -i iOther; iOther=$iTotal-$iOn
+
+ph.perfadd "total"   "${iTotal}"
+ph.perfadd "on"      "${iOn}"
+ph.perfadd "other"   "${iOther}"
+
+
+if [ $iOther -ge $iCriticalLimit ]; then
+        out="ERROR: not all hosts are up - count $iOther reached critical value $iCriticalLimit"
+        ph.setStatus "critical"
+elif [ $iOther -ge $iWarnLimit ]; then
+        out="ERROR: not all hosts are up - count $iOther reached critical value $iWarnLimit"
+        ph.setStatus "warning"        
+fi
+
+ph.status "ONEHOST - Total: $iTotal .. on: $iOn .. other: $iOther"
+echo "$cmdout"
+echo "$out"
\ No newline at end of file
-- 
GitLab