SNMPで自宅内ネットワークの監視
Ubuntu Server 24
code:sh
sudo apt install snmp
RTX1200に入れておく。prometheusサーバのIP
code:config
snmpv2c host <server IP>
snmpv2c community read-only public
やってみる
code:sh
snmpwalk -v 2c -c public <RTX1200のIP>
→いっぱい流れてきた。
iso.3.6.1.2.1.11.30.0 みたいなidと項目を関連付けるものがMIBと呼ばれているらしい。
たとえばホスト名を出してみる
code:txt
prometheus@prometheus:~$ snmpwalk -v 2c -c public 192.168.100.1 .1.3.6.1.2.1.1.5.0
iso.3.6.1.2.1.1.5.0 = STRING: "rtx1200"
標準mibはデフォルトで無効化されてるらしい。
/etc/snmp/snmp.conf をいじる。
code:diff
# As the snmp packages come without MIB files due to license reasons, loading
# of MIBs is disabled by default. If you added the MIBs you can reenable
# loading them by commenting out the following line.
-# mibs :
+mibs :
# If you want to globally change where snmp libraries, commands and daemons
# look for MIBS, change the line below. Note you can set this for individual
# tools with the -M option or MIBDIRS environment variable.
#
# mibdirs /usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf
めちゃくちゃエラーが出る。
https://scrapbox.io/files/6737745318c87a6212419269.png
ここにいろいろ描いてある。
MIBはここから落とせるらしい。
とりあえず入れてみる
code:sh
sudo cp yamaha-private-mib/* /usr/share/snmp/mibs/
→解決しない
https://scrapbox.io/files/6737759eb5fe8575ab858d9d.png
https://scrapbox.io/files/673776000a4cb35e0c378515.png
code:sh
apt -y install snmp snmp-mibs-downloader
で解決。
https://scrapbox.io/files/67377a44356bf845f2c8008b.png
https://scrapbox.io/files/67377aa318c87a621241aa50.png
snmp_exporterを入れる。
generatorする
code:sh
sudo apt-get install unzip build-essential libsnmp-dev
go install github.com/prometheus/snmp_exporter/generator@latest
よくわからん
これでやってみる
code:sh
git clone git@github.com:prometheus/snmp_exporter.git
cd snmp_exporter/generator
make mibs
code:sh
docker run -it -v "${PWD}:/opt/" prom/snmp-generator generate # build
~/snmp_exporter-0.26.0.linux-amd64/snmp_exporter --config.file=./snmp.yml # run
code:sh
~/go/bin/generator generate -m ./mibs -g ./generator.yml -o ./snmp.yml
これでもOK。
エラーが出たので悪いやつを消す。
https://scrapbox.io/files/6737812726c76ede85b7f859.png
code:generator.yml
auths:
auth_name:
version: 2 # SNMP version to use. Defaults to 2.
# 1 will use GETNEXT, 2 and 3 use GETBULK.
# Community string is used with SNMP v1 and v2. Defaults to "public".
community: public
# v3 has different and more complex settings.
# Which are required depends on the security_level.
# The equivalent options on NetSNMP commands like snmpbulkwalk
# and snmpget are also listed. See snmpcmd(1).
username: user # Required, no default. -u option to NetSNMP.
security_level: noAuthNoPriv # Defaults to noAuthNoPriv. -l option to NetSNMP.
# Can be noAuthNoPriv, authNoPriv or authPriv.
password: pass # Has no default. Also known as authKey, -A option to NetSNMP.
# Required if security_level is authNoPriv or authPriv.
auth_protocol: MD5 # MD5, SHA, SHA224, SHA256, SHA384, or SHA512. Defaults to MD5. -a option to NetSNMP.
# Used if security_level is authNoPriv or authPriv.
priv_protocol: DES # DES, AES, AES192, AES256, AES192C, or AES256C. Defaults to DES. -x option to NetSNMP.
# Used if security_level is authPriv.
priv_password: otherPass # Has no default. Also known as privKey, -X option to NetSNMP.
# Required if security_level is authPriv.
context_name: context # Has no default. -n option to NetSNMP.
# Required if context is configured on the device.
modules:
yamaha_rt_v2c: # SNMPv2cでのサンプル
walk:
#- 1.3.6.1.4.1.1182.2.1.4 # yrhMemoryUtil - sysUpTime
lookups:
lookup: ifAlias
overrides:
ifAlias:
ignore: true # Lookup metric
ifDescr:
ignore: true # Lookup metric
ifName:
ignore: true # Lookup metric
ifType:
type: EnumAsInfo
# version: 2
max_repetitions: 25
retries: 3
timeout: 10s
snmp_exporterを開くとこれ。
https://scrapbox.io/files/6737824f0ee8c3f9dd31fc55.png
https://scrapbox.io/files/6737833a4c276239b301f8d1.png
出ました
運用が面倒なのでMakefileをかいた
code:Makefile
.PHONY: all generate reload
all: generate reload
generate:
/home/prometheus/go/bin/generator generate -m ./mibs -g ./generator.yml -o /etc/snmp_generator.yml
reload:
systemctl restart snmp_exporter
code:sh
sudo make