|
@@ -0,0 +1,125 @@
+#!/bin/bash
+# Author: Marek Rzepala <Marek.Rzepala@internetx.com>
+# Version: 1.01
+# Last changed: 12.11.2020
+# Upstream URL: https://gitlab.muc.internetx.com/p-s/nagios/check_disks_xcp
+
+### CHANGE THIS IF NEEDED
+CRIT=90
+WARN=80
+TMP_FILE="/tmp/disks_host_"
+### CHANGE THIS IF NEEDED
+
+#exit without sr type argument
+if [ $# -ne 1 ]; then
+ echo ""
+ echo $0
+ echo "### usage: "
+ echo " $0 [ valid sr type; showtypes for help ]"
+ exit 1
+fi
+
+# save sr uuids in some tmp files, which file names are based on hostnames reported from xe
+# also dont use host attribute on lvmoiscsi; but really this should be a check whether or not sr type also has the host=<shared> property
+if [ $1 == lvmoiscsi ]; then
+ xe host-list params=name-label | sort | awk '{print $NF}' | sed -r '/^\s*$/d' | while read line; do xe sr-list type=$1 params=uuid | sed -r '/^\s*$/d' > "$TMP_FILE""$line"; done
+else
+ if [ $1 == showtypes ]; then
+ xe host-list params=name-label | sort | sed -r '/^\s*$/d' | while read line; do xe sr-list params=type; done | sort | uniq | sed -r '/^\s*$/d' && exit 0
+ else
+ xe host-list params=name-label | sort | awk '{print $NF}' | sed -r '/^\s*$/d' | while read line; do xe sr-list host=$line type=$1 params=uuid | sed -r '/^\s*$/d' > "$TMP_FILE"$line ; done
+ fi
+fi
+no_hosts=$( ls -1 "$TMP_FILE"* | wc -l )
+
+for f in "$TMP_FILE"*
+do
+# create some variables for further use
+# append different characters based on conditions
+ hostname=$(echo "$f" | sed "s#$TMP_FILE##g" | sed "s#-#_#g")
+ cat $f | awk '{print $NF}' | while read line2; do
+ physical_size=$(xe sr-list uuid=$line2 params=physical-size | awk '{print $NF}')
+ physical_usage=$(xe sr-list uuid=$line2 params=physical-utilisation | awk '{print $NF}')
+ sr_name=$(xe sr-list uuid=$line2 params=name-label | awk -F ":" '{print $NF}')
+ is_sr_shared=$([[ -z $( xe sr-list uuid=$line2 params=host | grep shared) ]] && echo 0 || echo 1)
+ if [[ $physical_size -eq 0 ]]; then
+ disk_usage=$(echo 0)
+ else
+ disk_usage=$( echo "scale=3; $( echo $physical_usage) / $( echo $physical_size) * 100" | bc | awk '{print ($0-int($0)<0.499)?int($0):int($0)+1}')
+ fi
+ if [[ $disk_usage -ge CRIT ]]; then
+ if [[ $is_sr_shared -eq 1 ]]; then
+# echo "1CR SHARED on $line2 - $disk_usage"%" storage usage on"$sr_name" ":" $hostname" >> $f
+ echo "1CR SHARED on $line2 - $disk_usage"%" storage usage on"$sr_name"" >> $f
+ else
+# echo "1CR on $line2 - $disk_usage"%" storage usage on"$sr_name" ":" $hostname" >> $f
+ echo "1CR on $line2 - $disk_usage"%" storage usage on"$sr_name"" >> $f
+ fi
+ else
+ if [[ $disk_usage -ge WARN ]]; then
+ if [[ $is_sr_shared -eq 1 ]]; then
+# echo "1WR SHARED on $line2 - $disk_usage"%" storage usage on"$sr_name" ":" $hostname" >> $f
+ echo "1WR SHARED on $line2 - $disk_usage"%" storage usage on"$sr_name"" >> $f
+ else
+# echo "1WR on $line2 - $disk_usage"%" storage usage on"$sr_name" ":" $hostname" >> $f
+ echo "1WR on $line2 - $disk_usage"%" storage usage on"$sr_name"" >> $f
+ fi
+ fi
+ fi
+ done
+
+done
+
+if [ $1 == lvmoiscsi ]; then
+ cr_shared=$(grep "SHARED" "$TMP_FILE"* | awk -F ":" '{print $2}' | sort | uniq | grep 1CR)
+ wr_shared=$(grep "SHARED" "$TMP_FILE"* | awk -F ":" '{print $2}' | sort | uniq | grep 1WR)
+ if [[ $(echo $cr_shared | awk '{print NF}') -ne 0 ]]; then
+ echo -n "CRITICAL - "
+ echo -n $(grep "SHARED" "$TMP_FILE"* | awk -F ":" '{print $2}' | sort | uniq | grep 1CR | awk -F "-" '{print $6 " " "##"}')
+ [[ $(echo $wr_shared | awk '{print NF}') -eq 0 ]] && echo
+ if [[ $(echo $wr_shared | awk '{print NF}') -ne 0 ]]; then
+ echo -n "WARNING" $(grep "SHARED" "$TMP_FILE"* | awk -F ":" '{print $2}' | sort | uniq | grep 1WR | awk -F "-" '{print $6 " " "##"}')
+ echo
+ exit 2
+ fi
+ else
+ if [[ $(echo $wr_shared | awk '{print NF}') -ne 0 ]]; then
+ echo -n "WARNING - "
+ echo -n $(grep "SHARED" "$TMP_FILE"* | awk -F ":" '{print $2}' | sort | uniq | grep 1WR | awk -F "-" '{print $6 " " "##"}')
+ echo
+ exit 1
+ else
+ if [[ $(echo $cr_shared | awk '{print NF}') -eq 0 ]] && [[ $(echo $wr_shared | awk '{print NF}') -eq 0 ]]; then
+ echo "OK"
+ exit 0
+ fi
+ fi
+ fi
+fi
+
+cr_local=$(grep "1CR" "$TMP_FILE"* | awk -F ":" '{print $2}')
+wr_local=$(grep "1WR" "$TMP_FILE"* | awk -F ":" '{print $2}')
+
+if [[ $(echo $cr_local | awk '{print NF}') -ne 0 ]]; then
+ echo -n "CRITICAL - "
+ echo -n $(grep "1CR" "$TMP_FILE"* | awk '{print $5, $6, $7, $8, $9, $10, $8, $1}' | awk -F ":" '{print $1 " ##"}' | sed "s#$TMP_FILE##g")
+ [[ $(echo $wr_local | awk '{print NF}') -eq 0 ]] && echo
+ if [[ $(echo $wr_local | awk '{print NF}') -ne 0 ]]; then
+ echo -n "WARNING" $(grep "1WR" "$TMP_FILE"* | awk '{print $5, $6, $7, $8, $9, $10, $8, $1}' | awk -F ":" '{print $1 " ##"}' | sed "s#$TMP_FILE##g")
+ echo
+ exit 2
+fi
+else
+ if [[ $(echo $wr_local | awk '{print NF}') -ne 0 ]]; then
+ echo -n "WARNING - "
+ echo -n $(grep "1WR" "$TMP_FILE"* | awk '{print $5, $6, $7, $8, $9, $10, $8, $1}' | awk -F ":" '{print $1 " ##"}' | sed "s#$TMP_FILE##g")
+
+ echo
+ exit 1
+ else
+ if [[ $(echo $cr_local | awk '{print NF}') -eq 0 ]] && [[ $(echo $wr_local | awk '{print NF}') -eq 0 ]]; then
+ echo "OK"
+ exit 0
+ fi
+ fi
+fi
|