Check PowerSupply with snmp (HP DL Server)

Bei HP DL380 Servern sollen die Powersupplies mit snmp (nagios) überwacht werden.

Verwendete MIB: CPQHLTH-MIB

snmp community= public

Einträge:

Anzahl der PowerSupplies:

PScnt=`snmpwalk -v 2c -c public <$ServerIP> 1.3.6.1.4.1.232.6.2.9.3.1.5 | wc -l`

Die OIDs sind Generations abhängig:

DL380G8:

SNMPv2-SMI::enterprises.232.6.2.9.3.1.5.0.1 = INTEGER: 1
SNMPv2-SMI::enterprises.232.6.2.9.3.1.5.0.2 = INTEGER: 1

DL380G7:

SNMPv2-SMI::enterprises.232.6.2.9.3.1.5.1.1 = INTEGER: 1
SNMPv2-SMI::enterprises.232.6.2.9.3.1.5.1.2 = INTEGER: 1

einlesen der OIDs:

var3=`snmpwalk -v 2c -c public  <$ServerIP> 1.3.6.1.4.1.232.6.2.9.3.1.5 | sed ’s/SNMPv2-SMI:://g‘ | cut -d‘ ‚ -f1`

aufsplitten in ein Array:


var4=()
while read -r line; do
  var4+=("$line")
done "$var3"

Jetzt kann mit einer for Schleife der Zustand abgefragt werden:


err=1
for ((a=1; a<=$PScnt; a++)); do
  OID=${var4[$a-1]}
  PSstat=`snmpget -v 2c -c $snmpcm $ServerIP $OID`
  IFS=':' read -ra ADDR <<< "$PSstat"
  curr_stat=${ADDR[3]}
  # echo "Status: Powersupply $a : $curr_stat"
 if [[ $curr_stat -ne 1 ]]; then
  echo "Error: Power supply $a status: ${err_msg[$curr_stat]}"
  err=$(( err + 1 ))
  exit 2
 else
  rtn_msg=$rtn_msg"Power supply $a: ${err_msg[$curr_stat]}; "
 fi
done

if [[ $err -eq 1 ]]; then
  echo "$rtn_msg"
  exit 0
fi

Ausgabe:

# ./check_HP_PS_snmp-002 <$ServerIP> public
Power supply 1: noError; Power supply 2: noError;

Das komplette Script:


#!/bin/bash

#
# check powersupply with snmp
#
# v 1.0 02.11.2016 J.H.
#
# v 1.1 07.07.2017 change snmp ID for HP DL380 G7 / G8
#
# check_hp_ps_snmp
#

ServerIP=$1
snmpcm=$2
err_cnt=99

 

#ServerIP=xxx.xxx.xxx.xxx
#snmpcm=public

# --------- check arguments --------------

if [ "$#" -ne 2 ]; then
     echo "Illegal number of parameters IP public"
     exit 3
fi

# ----------------------------------------

rtn_msg=""

## --- check connection errors ---

chk=`snmpwalk -v 2c -c $snmpcm $ServerIP 1.3.6.1.4.1.232.6.2.9.3.1.4 2>&1`
err_cnt=$?

if [[ $err_cnt -eq 1 ]]; then
     echo "ERROR $chk"
     exit 3
fi

# --------------------------------

# how many PS installed ?
PScnt=`snmpwalk -v 2c -c $snmpcm $ServerIP 1.3.6.1.4.1.232.6.2.9.3.1.5| wc -l`

# Base OID: .6.2.9.3.1.5.x
#

# -- get snmp ID's Base = 1.3.6.1.4.1.232.6.2.9.3.1.5

# count OIDs
IDcnt=`snmpwalk -v 2c -c $snmpcm $ServerIP 1.3.6.1.4.1.232.6.2.9.3.1.5 | wc -l`
# get OIDs
PSids=`snmpwalk -v 2c -c $snmpcm $ServerIP 1.3.6.1.4.1.232.6.2.9.3.1.5`

var3=`snmpwalk -v 2c -c $snmpcm $ServerIP 1.3.6.1.4.1.232.6.2.9.3.1.5 | sed 's/SNMPv2-SMI:://g' | cut -d' ' -f1`

var4=()
while read -r line; do
     var4+=("$line")
done <<< "$var3"

#echo "var4 0: ${var4[0]} var4 1: ${var4[1]}"

 

# check each PS 1=noError 2=generalFailure 3=bistFailure 4=fan error etc) 5=tempFailure

err_msg=(''
'noError'
'generalFailure'
'bistFailure'
'fanFailure'
'tempFailure'
)

err=1
for ((a=1; a<=$PScnt; a++)); do
     OID=${var4[$a-1]}
     PSstat=`snmpget -v 2c -c $snmpcm $ServerIP $OID`
     IFS=':' read -ra ADDR <<< "$PSstat"
     curr_stat=${ADDR[3]}
     # echo "Status: Powersupply $a : $curr_stat"
     if [[ $curr_stat -ne 1 ]]; then
         echo "Error: Power supply $a status: ${err_msg[$curr_stat]}"
         err=$(( err + 1 ))
         exit 2
     else
         rtn_msg=$rtn_msg"Power supply $a: ${err_msg[$curr_stat]}; "
     fi
done

if [[ $err -eq 1 ]]; then
  echo "$rtn_msg"
  exit 0
fi