Skip to content

Commit b71d9f2

Browse files
committed
Merge remote-tracking branch 'perceival/add-openwrt-temperature-sensors-clean'
2 parents 5dace8e + 2dc9e22 commit b71d9f2

11 files changed

Lines changed: 1329 additions & 54 deletions

snmp/Openwrt/LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

snmp/Openwrt/lm-sensors-pass.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/sh
2+
3+
# lm-sensors-pass.sh
4+
# SNMP pass script for LM-SENSORS-MIB thermal sensors
5+
# Provides proper MIB structure at .1.3.6.1.4.1.2021.13.16.2.1
6+
7+
BASE_OID=".1.3.6.1.4.1.2021.13.16.2.1"
8+
9+
# Function to get all thermal zone data
10+
# Output format: index:name:temp
11+
# Re-indexes zones sequentially starting from 1
12+
get_zones() {
13+
idx=0
14+
for zone in /sys/devices/virtual/thermal/thermal_zone*; do
15+
[ -d "$zone" ] || continue
16+
idx=$((idx + 1))
17+
zone_type=$(cat "$zone/type" 2>/dev/null || echo "unknown")
18+
zone_temp=$(cat "$zone/temp" 2>/dev/null || echo "0")
19+
echo "$idx:$zone_type:$zone_temp"
20+
done | sort -t':' -k1 -n
21+
}
22+
23+
case "$1" in
24+
-g)
25+
# GET request - return exact OID match
26+
REQ_OID="$2"
27+
FOUND=0
28+
29+
while IFS=':' read -r idx name temp; do
30+
case "$REQ_OID" in
31+
"$BASE_OID.1.$idx")
32+
echo "$REQ_OID"
33+
echo "integer"
34+
echo "$idx"
35+
FOUND=1
36+
break
37+
;;
38+
"$BASE_OID.2.$idx")
39+
echo "$REQ_OID"
40+
echo "string"
41+
echo "$name"
42+
FOUND=1
43+
break
44+
;;
45+
"$BASE_OID.3.$idx")
46+
echo "$REQ_OID"
47+
echo "gauge"
48+
echo "$temp"
49+
FOUND=1
50+
break
51+
;;
52+
esac
53+
done << EOF
54+
$(get_zones)
55+
EOF
56+
57+
[ "$FOUND" -eq 0 ] && echo "NONE"
58+
;;
59+
60+
-n)
61+
# GETNEXT request - return next OID after requested
62+
REQ_OID="$2"
63+
64+
# Create temporary file with all OIDs
65+
TMP_FILE="$(mktemp /tmp/snmp_oids.XXXXXX 2>/dev/null || echo "/tmp/snmp_oids.$$")"
66+
TMP_SORTED="${TMP_FILE}.sorted"
67+
TMP_LIST="${TMP_FILE}.list"
68+
: > "$TMP_FILE"
69+
trap 'rm -f "$TMP_FILE" "$TMP_SORTED" "$TMP_LIST"' EXIT INT TERM
70+
71+
get_zones | while IFS=':' read -r idx name temp; do
72+
# Pad index to ensure proper numeric sorting
73+
# Format: column.index where index is zero-padded to 3 digits
74+
{
75+
printf '%d.%03d|%s.1.%s|integer|%s\n' 1 "$idx" "$BASE_OID" "$idx" "$idx"
76+
printf '%d.%03d|%s.2.%s|string|%s\n' 2 "$idx" "$BASE_OID" "$idx" "$name"
77+
printf '%d.%03d|%s.3.%s|gauge|%s\n' 3 "$idx" "$BASE_OID" "$idx" "$temp"
78+
} >> "$TMP_FILE"
79+
done
80+
81+
# Sort by our padded key, then extract and compare OIDs
82+
sort -t'|' -k1 "$TMP_FILE" > "$TMP_SORTED"
83+
cut -d'|' -f2- "$TMP_SORTED" > "$TMP_LIST"
84+
85+
FOUND=0
86+
while IFS='|' read -r oid type value; do
87+
# Use awk for proper numeric OID comparison
88+
is_greater=$(awk -v req="$REQ_OID" -v curr="$oid" '
89+
BEGIN {
90+
# Split OIDs into arrays
91+
req_len = split(req, req_parts, ".");
92+
curr_len = split(curr, curr_parts, ".");
93+
max_len = (curr_len > req_len) ? curr_len : req_len;
94+
95+
# Compare each part numerically
96+
for (i = 1; i <= max_len; i++) {
97+
req_val = (i <= req_len) ? req_parts[i] : 0;
98+
curr_val = (i <= curr_len) ? curr_parts[i] : 0;
99+
100+
if (curr_val > req_val) {
101+
print "1";
102+
exit;
103+
} else if (curr_val < req_val) {
104+
print "0";
105+
exit;
106+
}
107+
}
108+
print "0";
109+
}
110+
')
111+
112+
if [ "$is_greater" = "1" ]; then
113+
echo "$oid"
114+
echo "$type"
115+
echo "$value"
116+
FOUND=1
117+
break
118+
fi
119+
done < "$TMP_LIST"
120+
121+
if [ "$FOUND" -eq 0 ]; then
122+
echo "NONE"
123+
fi
124+
;;
125+
126+
*)
127+
echo "Usage: $0 -g|-n OID" >&2
128+
exit 1
129+
;;
130+
esac

snmp/Openwrt/setup-snmpd.sh

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
# setup-snmpd.sh
5+
# Install OpenWrt LibreNMS helper scripts and optionally apply generated
6+
# SNMP extend configuration.
7+
8+
SCRIPT_DIR="/etc/librenms"
9+
BACKUP_DIR="$SCRIPT_DIR/backup"
10+
SOURCE_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
11+
12+
AUTO_YES=0
13+
NO_RESTART=0
14+
15+
while [ "$#" -gt 0 ]; do
16+
case "$1" in
17+
-y|--yes) AUTO_YES=1 ;;
18+
--no-restart) NO_RESTART=1 ;;
19+
-h|--help)
20+
cat <<'EOF'
21+
Usage: setup-snmpd.sh [--yes|-y] [--no-restart]
22+
23+
-y, --yes Apply generated snmpd config without prompt
24+
--no-restart Do not restart snmpd after applying config
25+
EOF
26+
exit 0
27+
;;
28+
*)
29+
echo "Unknown option: $1" >&2
30+
exit 2
31+
;;
32+
esac
33+
shift
34+
done
35+
36+
echo "OpenWrt SNMPD Setup Script"
37+
echo "=========================="
38+
echo ""
39+
40+
remove_managed_snmpd_sections() {
41+
tmp_clean=$(mktemp)
42+
awk '
43+
BEGIN { RS=""; ORS="\n\n" }
44+
{
45+
block = $0
46+
managed = 0
47+
48+
if (block ~ /LIBRENMS_OPENWRT_AUTOGEN_BEGIN/ || block ~ /LIBRENMS_OPENWRT_AUTOGEN_END/) {
49+
managed = 1
50+
}
51+
if (block ~ /config extend/ && block ~ /option name '\''interfaces'\''/) {
52+
managed = 1
53+
}
54+
if (block ~ /config extend/ && block ~ /option name '\''clients-wlan'\''/) {
55+
managed = 1
56+
}
57+
if (block ~ /config extend/ && block ~ /option name '\''(clients|wl-clients|frequency|noise-floor|rate|snr)-[^'\'']+'\''/) {
58+
managed = 1
59+
}
60+
if (block ~ /config pass/ && (block ~ /option name '\''lm-sensors'\''/ || block ~ /option prog '\''\/etc\/librenms\/lm-sensors-pass.sh'\''/)) {
61+
managed = 1
62+
}
63+
64+
if (!managed) {
65+
print block
66+
}
67+
}
68+
' /etc/config/snmpd > "$tmp_clean"
69+
mv "$tmp_clean" /etc/config/snmpd
70+
}
71+
72+
has_extend_name() {
73+
name="$1"
74+
grep -Eq "^[[:space:]]*option[[:space:]]+name[[:space:]]+'$name'[[:space:]]*$" /etc/config/snmpd
75+
}
76+
77+
ensure_base_os_extends() {
78+
if ! has_extend_name distro; then
79+
cat >> /etc/config/snmpd <<'EOF'
80+
81+
config extend
82+
option name 'distro'
83+
option prog '/bin/sh'
84+
option args "-c '. /etc/os-release; echo \"\$PRETTY_NAME\"'"
85+
EOF
86+
echo " + Added missing extend: distro"
87+
fi
88+
89+
if ! has_extend_name hardware; then
90+
cat >> /etc/config/snmpd <<'EOF'
91+
92+
config extend
93+
option name 'hardware'
94+
option prog '/bin/cat'
95+
option args '/sys/firmware/devicetree/base/model'
96+
EOF
97+
echo " + Added missing extend: hardware"
98+
fi
99+
}
100+
101+
ensure_system_section() {
102+
if ! grep -Eq "^[[:space:]]*config[[:space:]]+system[[:space:]]+'system'[[:space:]]*$" /etc/config/snmpd; then
103+
hostname=$(uname -n 2>/dev/null || echo openwrt)
104+
cat >> /etc/config/snmpd <<EOF
105+
106+
config system 'system'
107+
option sysLocation 'unknown'
108+
option sysName '$hostname'
109+
option sysContact 'root@localhost'
110+
option sysDescr 'OpenWrt'
111+
EOF
112+
echo " + Added missing section: config system 'system'"
113+
fi
114+
}
115+
116+
apply_generated_snmpd_block() {
117+
tmp_block=$(mktemp)
118+
tmp_new=$(mktemp)
119+
120+
"$SCRIPT_DIR/snmpd-config-generator.sh" > "$tmp_block"
121+
122+
# Remove previously managed sections first (legacy and marker-based).
123+
remove_managed_snmpd_sections
124+
125+
# Append exactly one fresh generated block.
126+
cat /etc/config/snmpd "$tmp_block" > "$tmp_new"
127+
mv "$tmp_new" /etc/config/snmpd
128+
rm -f "$tmp_block"
129+
}
130+
131+
# Create directories
132+
echo "Creating directories..."
133+
mkdir -p "$SCRIPT_DIR"
134+
mkdir -p "$BACKUP_DIR"
135+
136+
# Backup existing config if it exists
137+
if [ -f /etc/config/snmpd ]; then
138+
timestamp=$(date +%Y%m%d_%H%M%S)
139+
echo "Backing up existing /etc/config/snmpd to $BACKUP_DIR/snmpd.$timestamp"
140+
cp /etc/config/snmpd "$BACKUP_DIR/snmpd.$timestamp"
141+
else
142+
touch /etc/config/snmpd
143+
fi
144+
145+
# Copy scripts to /etc/librenms/
146+
echo "Installing monitoring scripts to $SCRIPT_DIR..."
147+
148+
scripts="wlInterfaces.sh wlClients.sh wlFrequency.sh wlNoiseFloor.sh wlRate.sh wlSNR.sh lm-sensors-pass.sh snmpd-config-generator.sh"
149+
150+
for script in $scripts; do
151+
src="$SOURCE_DIR/$script"
152+
dst="$SCRIPT_DIR/$script"
153+
if [ -f "$src" ]; then
154+
if [ "$src" = "$dst" ]; then
155+
chmod +x "$dst"
156+
echo " ✓ Using existing $script"
157+
else
158+
cp "$src" "$SCRIPT_DIR/"
159+
chmod +x "$dst"
160+
echo " ✓ Installed $script"
161+
fi
162+
else
163+
echo " ✗ Warning: $script not found in $SOURCE_DIR"
164+
fi
165+
done
166+
167+
# Generate sample config
168+
echo ""
169+
echo "Generating SNMPD configuration..."
170+
echo "Run the following command to see the generated config:"
171+
echo ""
172+
echo " $SCRIPT_DIR/snmpd-config-generator.sh"
173+
echo ""
174+
echo "To apply the configuration:"
175+
echo " 1. Backup your current config: cp /etc/config/snmpd /etc/config/snmpd.backup"
176+
echo " 2. Replace old LibreNMS wireless sections with the generated block"
177+
echo " 3. Restart snmpd: /etc/init.d/snmpd restart"
178+
echo ""
179+
echo "Setup complete!"
180+
181+
if [ "$AUTO_YES" -eq 1 ]; then
182+
answer="y"
183+
else
184+
printf "Do you want to update the SNMP configuration? [Y/n]: "
185+
read -r answer
186+
answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')
187+
fi
188+
189+
if [ -z "$answer" ] || [ "$answer" = "y" ]; then
190+
echo "Updating snmpd configuration..."
191+
192+
# Extra one-shot backup before write
193+
cp /etc/config/snmpd /etc/config/snmpd-backup
194+
195+
# Write exactly one fresh generated LibreNMS block.
196+
chmod +x "$SCRIPT_DIR/snmpd-config-generator.sh"
197+
ensure_base_os_extends
198+
ensure_system_section
199+
apply_generated_snmpd_block
200+
201+
if [ "$NO_RESTART" -eq 0 ]; then
202+
/etc/init.d/snmpd restart
203+
echo "Done! Service restarted."
204+
else
205+
echo "Skipped snmpd restart (--no-restart)."
206+
echo "Done! Configuration updated."
207+
fi
208+
else
209+
echo "Aborted. No changes made."
210+
exit 1
211+
fi

0 commit comments

Comments
 (0)