aboutsummaryrefslogtreecommitdiff
path: root/check_snmp_if
blob: 279f3975b72cbbeb73caf30873c9de86d3da4c2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash

# https://ixnfo.com/en/snmp-oid-and-mib-for-interfaces.html
# https://oidref.com/1.3.6.1.2.1.2.2.1

RESULTS=(OK WARNING CRITICAL UNKNOWN)

perror() {
	echo "$@"
} >&2

fail() {
	local RET=$1
	shift
	perror "$@"
	exit $RET
}

result() {
	local RET=$1
	shift
	echo -n "SNMP IF ${RESULTS[$RET]}: $@"
	perfdata
	exit $RET
}

walk() {
	snmpwalk -c "$COMMUNITY" -v 2c "$HOSTNAME" "1.3.6.1.2.1.2.2.1.$1.$INTERFACE"
	return $?
}

extract_val()  {
	sed -n 's/.*=\(.*\)/\1/p' | sed -n 's/.*:\s*\(.*\)/\1/p'
}

perfdata() {
	[ $GENPERFDATA -eq 0 ] && return

	echo -n "| "
	echo -n "in=$INOCTETS "
	echo -n "out=$OUTOCTETS "
	echo -n "in_err=$INERRORS "
	echo -n "out_err=$OUTERRORS "
	echo
}

GENPERFDATA=0

while getopts ":H:c:i:" opt; do
	case $opt in
		H)
			HOSTNAME="$OPTARG"
			;;
		c)
			COMMUNITY="$OPTARG"
			;;
		i)
			INTERFACE="$OPTARG"
			;;
		:)
			fail 3 "$OPTARG requires Argument."
			;;
		*)
			fail 3 "Invalid option $OPTARG"
	esac
done

SNMPWALK_RESULT=$(snmpwalk -c "$COMMUNITY" -v 2c "$HOSTNAME" 1.3.6.1.2.1.2.2.1.8.$INTERFACE; exit $?)
RET=$?

[ $RET -ne 0 ] && result 3 "snmpwalk failed with code $RET: $SNMPWALK_RESULT"
[ -z "$SNMPWALK_RESULT" ] && result 2 "No matching entry found."

GENPERFDATA=1

INOCTETS=$(walk 10 | extract_val)
OUTOCTETS=$(walk 16 | extract_val)

INERRORS=$(walk 14 | extract_val)
OUTERRORS=$(walk 20 | extract_val)

sed -n 's/.*=\(.*\)/\1/p' <<< "$SNMPWALK_RESULT" | grep "up\|1" > /dev/null && result 0 "Interface $INTERFACE is up."

result 2 "Interface $INTERFACE is not up: $SNMPWALK_RESULT"