Code backup
This commit is contained in:
2026-05-10 16:59:01 +02:00
commit 368d6fafea
796 changed files with 315310 additions and 0 deletions
@@ -0,0 +1,418 @@
#!/bin/bash
# - VAR
# - Bash info
APPNAME=$(basename $0)
NAME="Check Switch Huawei"
AUTHOR="Kalarumeth"
VERSION="v0.1"
URL="https://github.com/Kalarumeth"
# - Default settings for connection
COMMUNITY="public"
HOST_NAME="localhost"
AP="authPriv"
AUTH="SHA-256"
PRIV="AES"
SNMPVERSION="3"
# - State Variables
STATE_OK=0
STATE_WARN=1
STATE_CRIT=2
STATE_UNK=3
STATE=$STATE_OK
# - OID
Huawei.OIDS() {
#Info
OID_sysDescr="1.3.6.1.2.1.1.1.0"
OID_sysUptimeIstance="1.3.6.1.2.1.1.3"
#Fan
OID_fanSlot="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.1"
OID_fanSn="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.2"
OID_fanReg="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.3"
OID_fanMode="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.4"
OID_fanSpeed="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.5"
OID_fanPresent="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.6"
OID_fanState="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.7"
OID_fanDesc="1.3.6.1.4.1.2011.5.25.31.1.1.10.1.8"
#Power
OID_pwrSlot="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.1"
OID_pwrSn="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.2"
OID_pwrReg="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.3"
OID_pwrMode="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.4"
OID_pwrPresent="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.5"
OID_pwrState="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.6"
OID_pwrCurrent="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.7"
OID_pwrVoltage="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.8"
OID_pwrDesc="1.3.6.1.4.1.2011.5.25.31.1.1.18.1.9"
#Stack
OID_stkRun="1.3.6.1.4.1.2011.5.25.183.1.1"
OID_stkType="1.3.6.1.4.1.2011.5.25.183.1.2"
OID_stkPriority="1.3.6.1.4.1.2011.5.25.183.1.20.1.2"
OID_stkRole="1.3.6.1.4.1.2011.5.25.183.1.20.1.3"
OID_stkMac="1.3.6.1.4.1.2011.5.25.183.1.20.1.4"
OID_stkDevice="1.3.6.1.4.1.2011.5.25.183.1.20.1.5"
OID_stkId="1.3.6.1.4.1.2011.5.25.183.1.20.1.6"
}
# - MAIN CODE
Script.HostAlive() {
for server in $HOST_NAME; do
ping -c1 -W1 -q $server &>/dev/null
if [[ $? != 0 ]] ; then
printf "%s\n" "$server is unreachable"
exit $STATE_UNK
fi
done
}
Script.SNMP() {
snmpwalk -v $SNMPVERSION -r 1 -t 10 -Oen -l $AP -u $USER -e $ENGINEID -a $AUTH -A $PHAUTH -x $PRIV -X $PHPRIV $HOST_NAME $1
}
Huawei.Main() {
Script.HostAlive
case $1 in
info)
Huawei.Info
exit $STATE ;;
uptime)
Huawei.UpTime
exit $STATE ;;
fan)
Huawei.Fan
exit $STATE ;;
power)
Huawei.Power
exit $STATE ;;
stack)
Huawei.Stack
exit $STATE ;;
*)
echo "Unknown check!"
Help.Huawei
exit $STATE_UNK ;;
esac
}
Huawei.GetData() {
Huawei.OIDS
case $1 in
description)
sysDescr=$(Script.SNMP $OID_sysDescr | cut -d ':' -f 2);;
uptime)
sysUptimeIstance=$(Script.SNMP $OID_sysUptimeIstance | cut -d ')' -f 2 );;
fan)
fanSlot=($(Script.SNMP $OID_fanSlot | cut -d ' ' -f 4 ))
fanSn=($(Script.SNMP $OID_fanSn | cut -d ' ' -f 4 ))
fanReg=($(Script.SNMP $OID_fanReg | cut -d ' ' -f 4 ))
fanMode=($(Script.SNMP $OID_fanMode | cut -d ' ' -f 4 ))
fanSpeed=($(Script.SNMP $OID_fanSpeed | cut -d ' ' -f 4 ))
fanPresent=($(Script.SNMP $OID_fanPresent | cut -d ' ' -f 4 ))
fanState=($(Script.SNMP $OID_fanState | cut -d ' ' -f 4 ))
fanDesc=($(Script.SNMP $OID_fanDesc | cut -d '"' -f 2))
;;
power)
pwrSlot=($(Script.SNMP $OID_pwrSlot | cut -d ' ' -f 4 ))
pwrSn=($(Script.SNMP $OID_pwrSn | cut -d ' ' -f 4 ))
pwrReg=($(Script.SNMP $OID_pwrReg | cut -d ' ' -f 4 ))
pwrMode=($(Script.SNMP $OID_pwrMode | cut -d ' ' -f 4 ))
pwrPresent=($(Script.SNMP $OID_pwrPresent | cut -d ' ' -f 4 ))
pwrState=($(Script.SNMP $OID_pwrState | cut -d ' ' -f 4 ))
pwrCurrent=($(Script.SNMP $OID_pwrCurrent | cut -d ' ' -f 4 ))
pwrVoltage=($(Script.SNMP $OID_pwrVoltage | cut -d ' ' -f 4))
pwrDesc=($(Script.SNMP $OID_pwrDesc | cut -d '"' -f 2))
;;
stack)
stkRun=$(Script.SNMP $OID_stkRun | cut -d ' ' -f 4 )
stkType=$(Script.SNMP $OID_stkType | cut -d ' ' -f 4 )
stkPriority=($(Script.SNMP $OID_stkPriority | cut -d ' ' -f 4 ))
stkRole=($(Script.SNMP $OID_stkRole | cut -d ' ' -f 4 ))
stkMac=($(Script.SNMP $OID_stkMac | cut -d '"' -f 2 ))
stkDevice=($(Script.SNMP $OID_stkDevice | cut -d '"' -f 2 ))
stkId=($(Script.SNMP $OID_stkId | cut -d ' ' -f 4 ))
;;
esac
}
Huawei.ErrorCounter() {
for Error in "$(( WarningCounter + CriticalCounter ))"; do
if [[ $WarningCounter == 0 ]] && [[ $CriticalCounter == 0 ]] ; then
printf "%s\t%s\n\n" "OK!" "All $1 is Up"
elif [[ $WarningCounter != 0 ]] && [[ $CriticalCounter == 0 ]] ; then
printf "%s\t%s\n\n" "WARNING!" "$Error $1 with problem"
STATE=$STATE_WARN
elif [[ $CriticalCounter != 0 ]] ; then
printf "%s\t%s\n\n" "CRITICAL!" "$Error $1 with problem"
STATE=$STATE_CRIT
fi
done
}
# Device Information
Huawei.Info() {
Huawei.GetData description
printf "%s\n" "$sysDescr"
}
# UpTime
Huawei.UpTime() {
Huawei.GetData uptime
printf "%s\n" "$sysUptimeIstance"
}
# Fan
Huawei.Fan() {
Huawei.GetData fan
Huawei.Fan.Header
for((i=0; i<${#fanSlot[@]}; i++))
do
Huawei.Fan.Desc
printf "%s\t" "${fanSlot[$i]}"
printf "%s\t" "${fanSn[$i]}"
Huawei.Fan.Reg
Huawei.Fan.Mode
printf "%s\t" "${fanSpeed[$i]}"
Huawei.Fan.Present
Huawei.Fan.State
printf "\n"
done
}
Huawei.Fan.Header() {
WarningCounter=0
CriticalCounter=0
for((i=0; i<${#fanSlot[@]}; i++)); do
for value in "${fanState[$i]}"; do
if [[ $value == 2 ]]; then
CriticalCounter=$(( CriticalCounter + 1 ))
fi
done
done
Huawei.ErrorCounter "Fan"
if [[ $HOST_NAME == *"core"* ]] ; then
printf "%s\t\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "Desc" "Unit" "Slot" "Reg" "Mode" "Speed" "Present" "State" "========================================================================"
elif [[ $HOST_NAME == *"acc"* ]] ; then
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "Unit" "Slot" "Reg" "Mode" "Speed" "Present" "State" "========================================================"
fi
}
Huawei.Fan.Desc() {
if [[ $HOST_NAME == *"core"* ]] ; then
printf "%s\t" "${fanDesc[$i]}"
fi
}
Huawei.Fan.Reg() {
case ${fanReg[$i]} in
1) printf "%s\t" "Yes" ;;
2) printf "%s\t" "No" ;;
esac
}
Huawei.Fan.Mode() {
case ${fanMode[$i]} in
1) printf "%s\t" "Auto" ;;
2) printf "%s\t" "Manual" ;;
3) printf "%s\t" "Unknown" ;;
4) printf "%s\t" "Silent" ;;
esac
}
Huawei.Fan.Present() {
case ${fanPresent[$i]} in
1) printf "%s\t" "Present" ;;
2) printf "%s\t" "absent" ;;
esac
}
Huawei.Fan.State() {
case ${fanState[$i]} in
1) printf "%s\t" "Normal" ;;
2) printf "%s\t" "Abnormal" ;;
esac
}
# Power
Huawei.Power() {
Huawei.GetData power
Huawei.Power.Header
for((i=0; i<${#pwrSlot[@]}; i++))
do
printf "%s\t" "${pwrDesc[$i]}"
printf "%s\t" "${pwrSlot[$i]}"
printf "%s\t" "${pwrSn[$i]}"
Huawei.Power.Reg
Huawei.Power.Mode
Huawei.Power.Present
Huawei.Power.State
printf "%s\t" "${pwrCurrent[$i]} mA"
printf "%s\t" "${pwrVoltage[$i]} mV"
printf "\n"
done
}
Huawei.Power.Header() {
WarningCounter=0
CriticalCounter=0
for((i=0; i<${#pwrSlot[@]}; i++)); do
for value in "${pwrState[$i]}"; do
if [[ $value == 2 ]]; then
CriticalCounter=$(( CriticalCounter + 1 ))
fi
done
done
Huawei.ErrorCounter "Power Supply"
printf "%s\t\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" "Desc" "Unit" "Slot" "Reg" "Mode" "Present" "State" "Current" "Voltage" "=================================================================================="
}
Huawei.Power.Reg() {
case ${pwrReg[$i]} in
1) printf "%s\t" "Yes" ;;
2) printf "%s\t" "No" ;;
esac
}
Huawei.Power.Mode() {
case ${pwrMode[$i]} in
1) printf "%s\t" "Unknown" ;;
2) printf "%s\t" "DC" ;;
3) printf "%s\t" "AC" ;;
4) printf "%s\t" "hvdc" ;;
esac
}
Huawei.Power.Present() {
case ${pwrPresent[$i]} in
1) printf "%s\t" "Present" ;;
2) printf "%s\t" "Absent" ;;
esac
}
Huawei.Power.State() {
case ${pwrState[$i]} in
1) printf "%s\t" "Supply" ;;
2) printf "%s\t" "NotSupply" ;;
3) printf "%s\t" "Sleep" ;;
4) printf "%s\t" "Unknown" ;;
esac
}
# Stack
Huawei.Stack() {
Huawei.GetData stack
Huawei.Stack.Header
for((i=0; i<${#stkDevice[@]}; i++))
do
printf "%s\t" "${stkId[$i]}"
printf "%s\t" "${stkDevice[$i]}"
printf "%s\t" "${stkMac[$i]}"
Huawei.Stack.Role
printf "%s\t" "${stkPriority[$i]}"
printf "\n"
done
}
Huawei.Stack.Header() {
if [[ $stkRun == 1 ]] ; then
Huawei.Stack.Status
printf "%s\t%s\t\t%s\t\t%s\t\t%s\n" "Id" "Device" "Mac Address" "Role" "Priority" "=========================================================================="
elif [[ $stkRun == 2 ]] ; then
printf "%s\t%s\n\n" "CRITICAL!" "Stack is disable"
exit $STATE_CRIT
fi
}
Huawei.Stack.Status() {
printf "%s" "OK! Stack is "
case $stkRun in
1) printf "%s" "Enable" ;;
2) printf "%s" "Disable" ;;
esac
if [[ $HOST_NAME == *"acc"* ]] ; then
printf "%s" " and topology type is "
case $stkType in
1) printf "%s" "Ring" ;;
2) printf "%s" "Link" ;;
esac
fi
printf "\n\n"
}
Huawei.Stack.Role() {
if [[ $HOST_NAME == *"acc"* ]] ; then
printf "\t"
fi
case ${stkRole[$i]} in
1) printf "%s\t" "Master Switch" ;;
2) printf "%s\t" "Backup Switch" ;;
3) printf "%s\t" "Slave Switch" ;;
esac
}
# - COMMAND LINE ENCODER
while test -n "$1"; do
case "$1" in
--host | -h)
HOST_NAME=$2
shift ;;
--user | -u)
USER=$2
shift ;;
--engineId | -e)
ENGINEID=$2
shift ;;
--phAuth | -pa)
PHAUTH=$2
shift ;;
--phPriv | -pp)
PHPRIV=$2
shift ;;
--type | -t)
Huawei.Main $2
shift ;;
--help | -H)
Help.Main ;;
--version | -V)
Help.Info
exit $STATE ;;
*)
echo "Unknown argument: $1"
print_help
exit $STATE_UNK ;;
esac
shift
done
Huawei.Main