#!/bin/bash # #Usage: ./check_esx_datastore_snmp # #Description: # This plugin will catch used and free Datastore space via SNMP the from an VMWare ESX Server, tested with VMware ESXi, 6.7.0 # Model: ProLiant BL460c Gen8 32 Cores # refers to the datastore position in the list returned by SNMP. For example if you want to check the first datastore of # that list then use 1, to check the second use 2 and so on. # Make sure that the script is fetiching the right DATA. You can test it with the following command: # # snmpwalk -m ALL -c public -v 2c myhost.mydomain .1.3.6.1.2.1.25.2.3.1.5.5 # # snmpwalk -m ALL -c public -v 2c myhost.mydomain .1.3.6.1.2.1.25.2.3.1.6.5 # # HOST-RESOURCES-MIB::hrStorageType.5 .1.3.6.1.2.1.25.2.3.1.2.5 OID: HOST-RESOURCES-TYPES::hrStorageFixedDisk # HOST-RESOURCES-MIB::hrStorageSize.5 .1.3.6.1.2.1.25.2.3.1.5.5 # HOST-RESOURCES-MIB::hrStorageUsed.5 .1.3.6.1.2.1.25.2.3.1.6.5 # #MIB FILE # # /usr/share/snmp/mibs # # https://kb.vmware.com/s/article/1013445 # # # #SYNTAX: ./check_esx_datastore_snmp_V2 " # #EXAMPLE ./check_esx_datastore_snmp_V2 myhost.mydomain public 90 95 ;datastore must be in format "/vmfs/volumes/60...." # # # Created: 2021-01-13 Author : Cesar San # based on Mattia Pezzetta work "check_esx_mem_snmp", # based on Thibaut Ploquin work "check_hpux_mem", # based on Alain van der Heiden & Remco Hage works "check_snmp_hpux_mem" # # Changes: 2021-01-13 First Release # # Changes: 2022-04-08 Powered by Anze Tomazin # # # ################################################################################## PATH="/usr/bin:/usr/sbin:/bin:/sbin" STATE_OK=0 STATE_WARNING=1 STATE_CRITICAL=2 STATE_UNKNOWN=3 STATE_DEPENDENT=4 ################################################################################## PROGNAME=$0 HOSTNAME=$1 COMMUNITY=$2 WARNING=$3 CRITICAL=$4 DATASTORE=$5 print_help() { echo $PROGNAME echo "" echo "Usage: $PROGNAME " echo "" echo "This plugin checks the total and used datastore space on ESXI servers." echo "Tests if used space is greater than and " echo "Can create graphs" echo "" exit 0 } case "$1" in --help) print_help exit 0 ;; -h) print_help exit 0 ;; --version) echo $PROGNAME v1.0 exit 0 ;; -V) echo $PROGNAME v1.0 exit 0 ;; *) OIDUSEDDISK=$(snmpwalk -v 2c -c $COMMUNITY "$HOSTNAME" -Oq .1.3.6.1.2.1.25.2.3.1 | grep "${DATASTORE}" | sed "s/hrStorageDescr/hrStorageUsed/" | cut -f 1 -d " ") OIDSIZEDISK=$(snmpwalk -v 2c -c $COMMUNITY "$HOSTNAME" -Oq .1.3.6.1.2.1.25.2.3.1 | grep "${DATASTORE}" | sed "s/hrStorageDescr/hrStorageSize/" | cut -f 1 -d " ") USEDMB=$(snmpwalk -v 2c -c $COMMUNITY "$HOSTNAME" -Oq -Ov $OIDUSEDDISK 2> /dev/null) #Free Memory in MB HOST-RESOURCES-MIB::hrStorageUsed.5 if [ $? -ne 0 ]; then # Something's wrong echo "UNKNOWN" exit $STATE_UNKNOWN fi USEDKB=$(echo "scale=0 ; $USEDMB*1024"| bc) #Free Memory in KB HOST-RESOURCES-MIB::hrStorageUsed.5 TOTALMB=$(snmpwalk -v 2c -c $COMMUNITY "$HOSTNAME" -Oq -Ov $OIDSIZEDISK 2> /dev/null) #Total Memory in MB HOST-RESOURCES-MIB::hrStorageSize.5 if [ $? -ne 0 ]; then # Something's wrong echo "UNKNOWN" exit $STATE_UNKNOWN fi TOTALKB=$(echo "scale=0 ; $TOTALMB*1024"| bc) #Total Memory in KB HOST-RESOURCES-MIB::hrStorageSize.5 FREEKB=$(echo "scale=0 ; $TOTALKB-$USEDKB"| bc) #Free Memory in KB HOST-RESOURCES-MIB::hrStorageUsed.5 TOTALG=$(echo "scale=0 ; $TOTALKB*1024"| bc) #Total for graph in Byte USEDG=$(echo "scale=0 ; $USEDKB*1024"| bc) #Used for graph in Byte #### Conversion TOTALDISK KB -> MB -> GB #### TESTTOT=$(echo "scale=0 ; $TOTALKB/1024"| bc) if [ $TESTTOT -ge 1 ]; then UTOT=$(echo " MB") TOTALDISKINT=$(echo "scale=0 ; $TOTALKB/1024"| bc) TOTALDISK=$(echo "scale=4 ; $TOTALKB/1024"| bc) TESTTOT=$(echo "scale=0 ; $TOTALDISKINT/1024"| bc) if [ $TESTTOT -ge 1 ]; then UTOT=$(echo " GB") TOTALDISK=$(echo "scale=4 ; $TOTALDISK/1024"| bc) fi else TOTALDISK=$(echo "scale=0 ; $TOTALKB") UTOT=$(echo " MB") fi #### Conversion USEDDISK KB -> MB -> GB #### TESTUSE=$(echo "scale=0 ; $USEDKB/1024"| bc) if [ $TESTUSE -ge 1 ]; then UUSE=$(echo " MB") USEDDISKINT=$(echo "scale=0 ; $USEDKB/1024"| bc) USEDDISK=$(echo "scale=4 ; $USEDKB/1024"| bc) TESTUSE=$(echo "scale=0 ; $USEDDISKINT/1024"| bc) if [ $TESTUSE -ge 1 ]; then UUSE=$(echo " GB") USEDDISK=$(echo "scale=4 ; $USEDDISK/1024"| bc) fi else USEDDISK=$(echo "scale=0 ; $USEDKB") UUSE=$(echo " MB") fi #### Percentage calculation #### CALDEPTH=$(echo "scale=0 ; $USEDKB*100"| bc) PERCENT=$(echo "scale=2 ; $CALDEPTH/$TOTALKB"| bc) INTPERCENT=$(echo "scale=0 ; $CALDEPTH/$TOTALKB"| bc) #### Affichage #### outmessage="Disk: $USEDDISK $UUSE / $TOTALDISK $UTOT ($PERCENT%)|used=${USEDG}B size=${TOTALG}B" #### TEST if Critical or Warning #### #echo $INTPERCENT if [ $CRITICAL -le $INTPERCENT ]; then echo "CRITICAL $outmessage" exit $STATE_CRITICAL elif [ $WARNING -le $INTPERCENT ]; then echo "WARNING $outmessage" exit $STATE_WARNING else echo "OK $outmessage" exit $STATE_OK fi ;; esac