-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubnt-hal-snmp.sh
executable file
·85 lines (76 loc) · 1.9 KB
/
ubnt-hal-snmp.sh
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
85
#!/bin/sh
usage() {
echo "Usage: $0 (-g | -n) OID"
exit 1
}
if [ "$1" != "-g" ] && [ "$1" != "-n" ]; then
usage
elif [ "$#" != 2 ]; then
usage
fi
OID_PREFIX=".1.3.6.1.4.1.4413.1.1.43.1"
CACHE_FILE="/dev/shm/$(basename "$0").cache"
CACHE_MAX_AGE=1 # minutes
fan_oid_list() {
sudo /usr/sbin/ubnt-hal getFanTach | sort | {
awk -F": " -v prefix="${OID_PREFIX}.6.1" -v idx=1 '/^FAN [0-9]+: [0-9]+ RPM$/ {
sub(/ RPM$/, "", $2)
print prefix ".1." idx ".0 integer " idx
print prefix ".2." idx ".0 integer 1"
print prefix ".3." idx ".0 integer 2"
print prefix ".4." idx ".0 integer " $2
print prefix ".5." idx ".0 integer 50"
print prefix ".6." idx ".0 integer 1"
idx++
}'
}
}
power_oid_list() {
sudo /usr/sbin/ubnt-hal getPowerSlot | sort | {
awk -F": " -v prefix="${OID_PREFIX}.7.1" -v idx=1 '$1 ~ / Source$/ {
print prefix ".1." idx ".0 integer " idx
print prefix ".2." idx ".0 integer 1"
if ($2 == "yes")
print prefix ".3." idx ".0 integer 2"
else
print prefix ".3." idx ".0 integer 3"
print prefix ".4." idx ".0 integer 1"
idx++
}'
}
}
temp_oid_list() {
sudo /usr/sbin/ubnt-hal getTemp 2> /dev/null | sort | {
awk -F: -v prefix="${OID_PREFIX}.8.1" -v idx=1 '$2 != "" {
sub(/[^0-9].*$/, "", $2)
print prefix ".1." idx ".0 integer 1"
print prefix ".2." idx ".0 integer " idx
print prefix ".3." idx ".0 integer 1"
print prefix ".4." idx ".0 integer 1"
print prefix ".5." idx ".0 integer " $2
idx++
}'
}
}
oid_list() {
fan_oid_list
power_oid_list
temp_oid_list
}
cached_oid_list() {
if [ -z "$(find "$CACHE_FILE" -mmin "-${CACHE_MAX_AGE}" 2> /dev/null)" ]; then
oid_list > "${CACHE_FILE}.$$"
mv "${CACHE_FILE}.$$" "$CACHE_FILE"
fi
cat "$CACHE_FILE"
}
{
cached_oid_list
if [ "$1" = "-n" ]; then
echo "$2 "
fi
} | sort | if [ "$1" = "-g" ]; then
grep -F "$2"
elif [ "$1" = "-n" ]; then
grep -A1 -F "$2 " | grep -vF "$2 "
fi | tr ' ' '\n'