👻

箱庭にPrometheusを入れてみた(設定編)

に公開

SNMP_Exproterを使ってみる

ダウンロード

ばっちくしたくないので /home/ban/ 配下に作った prometheus ディレクトリに移動

ban@UoVb:~$ cd prometheus/
ban@UoVb:~/prometheus$

作成当時の最新は0.21

https://github.com/prometheus/snmp_exporter

ban@UoVb:~/prometheus$ wget https://github.com/prometheus/snmp_exporter/releases/download/v0.21.0/snmp_exporter-0.21.0.linux-amd64.tar.gz

ban@UoVb:~/prometheus$ ls -l snmp*
-rw-rw-r-- 1 ban ban 8468035 11月 23 00:23 snmp_exporter-0.21.0.linux-amd64.tar.gz
ban@UoVb:~/prometheus$

解凍

ban@UoVb:~/prometheus$ tar xf snmp_exporter-0.21.0.linux-amd64.tar.gz
ban@UoVb:~/prometheus$

ban@UoVb:~/prometheus$ ls -l snmp*
-rw-rw-r-- 1 ban ban 8468035 11月 23 00:23 snmp_exporter-0.21.0.linux-amd64.tar.gz

snmp_exporter-0.21.0.linux-amd64:
合計 16916
-rw-r--r-- 1 ban ban    11357 11月 23 00:20 LICENSE
-rw-r--r-- 1 ban ban       63 11月 23 00:20 NOTICE
-rw-r--r-- 1 ban ban  1435911 11月 23 00:20 snmp.yml
-rwxr-xr-x 1 ban ban 15867098 11月 23 00:14 snmp_exporter
ban@UoVb:~/prometheus$

snmp.ymlをいじくるのにはちょっと敷居が高いのでgeneraterなるものを使ってみる

https://github.com/prometheus/snmp_exporter/tree/main/generator

まずはgoのインストール

ban@UoVb:~/prometheus$ apt info golang



Depends: golang-1.13, golang-doc (>= 2:1.13~1ubuntu2), golang-go (>= 2:1.13~1ubuntu2), golang-src (>= 2:1.13~1ubuntu2)



ちと古い・・・
google先生にお伺いするとPPA追加でaptでインストール可能って記事が結構出てるけどちょっとPPAは気が引けたので普通に公式からtarでインストール

ban@UoVb:~$ sudo wget https://dl.google.com/go/go1.17.linux-amd64.tar.gz

ban@UoVb:~$ sudo tar -C /usr/local -xzf go1.17.linux-amd64.tar.gz

.profileの編集(PATHを通す)

ban@UoVb:~$ vim .profile

export PATH=$PATH:/usr/local/go/bin #末尾に追記

profileの再読み込み

ban@UoVb:~$ source .profile

goの確認

ban@UoVb:~$ go version
go version go1.17 linux/amd64
ban@UoVb:~$

NetSNMPのbuild

ban@UoVb:~/prometheus$ sudo apt-get install unzip build-essential libsnmp-dev

snmp_exporter generatorのゲット

ban@UoVb:~$ go get github.com/prometheus/snmp_exporter/generator

juniperのmibファイルをゲット

https://bestmonitoringtools.com/mibdb/mibdb_search.php?mib=JUNIPER-MIB

mibファイルを補完してsnmp.confに読み込みディレクトリの追加

ban@UoVb:~$ mkdir /etc/snmp/vender_mib


ban@UoVb:~$ sudo vim /etc/snmp/snmp.conf

mibdirs /usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf:/etc/snmp/vender_mib
mibs all

snmp_exproterは /home/.snmp/mibs を読み込むらしいのでシンボリックリンクを作っておく

ban@UoVb:~$ mkdir .snmp

generatorのインストールして実行ファイルがあるか確認

ban@UoVb:~$ cd prometheus/

ban@UoVb:~/prometheus$ go install github.com/prometheus/snmp_exporter/generator@latest

ban@UoVb:~/prometheus$ cd ${GOPATH-$HOME/go}/bin/

ban@UoVb:~/go/bin$ ll
合計 21224
drwxrwxr-x 2 ban ban     4096 11月 29 13:47 ./
drwxrwxr-x 4 ban ban     4096 11月 29 13:17 ../
-rwxrwxr-x 1 ban ban  6437464 11月 29 16:47 generator*
-rwxrwxr-x 1 ban ban 15285948 11月 29 13:47 snmp_exporter*
ban@UoVb:~/go/bin$

generator.ymlの作成
大体こんな感じ

ban@UoVb:~/go/bin$ vim generator.yml

modules:
        juniper:
                walk:
                        - ifIndex
                version: 2
                retries: 3
                timeout: 3s
                auth:
                        community: banSnmp

snmp.ymlの作成

ban@UoVb:~/go/bin$ ./generator generate
ts=2022-11-29T07:55:10.384Z caller=net_snmp.go:161 level=info msg="Loading MIBs" from=$HOME/.snmp/mibs:/usr/share/snmp/mibs:/usr/share/snmp/mibs/iana:/usr/share/snmp/mibs/ietf
ts=2022-11-29T07:55:10.703Z caller=main.go:119 level=warn msg="NetSNMP reported parse error(s)" errors=315
ts=2022-11-29T07:55:10.894Z caller=main.go:51 level=info msg="Generating config for module" module=juniper
ts=2022-11-29T07:55:10.918Z caller=main.go:66 level=info msg="Generated metrics" module=juniper metrics=1
ts=2022-11-29T07:55:10.918Z caller=main.go:91 level=info msg="Config written" file=/home/ban/go/bin/snmp.yml
ban@UoVb:~/go/bin$

出来上がった snmp.yml を確認

ban@UoVb:~/go/bin$ cat snmp.yml
# WARNING: This file was auto-generated using snmp_exporter generator, manual changes will be lost.
juniper:
  walk:
  - 1.3.6.1.2.1.2.2.1.1
  metrics:
  - name: ifIndex
    oid: 1.3.6.1.2.1.2.2.1.1
    type: gauge
    help: A unique value, greater than zero, for each interface - 1.3.6.1.2.1.2.2.1.1
    indexes:
    - labelname: ifIndex
      type: gauge
  version: 2
  retries: 3
  timeout: 3s
  auth:
    community: banSnmp
ban@UoVb:~/go/bin$

snmp_exproter配下に作成したsnmp.ymlを配置

an@UoVb:~/go/bin$ cp /home/ban/go/bin/snmp.yml /home/ban/prometheus/snmp_exporter-0.21.0.linux-amd64/snmp.yml

ban@UoVb:~/go/bin$ cd /home/ban/prometheus/snmp_exporter-0.21.0.linux-amd64/

ban@UoVb:~/prometheus/snmp_exporter-0.21.0.linux-amd64$ ll
合計 16928
drwxr-xr-x 2 ban ban     4096 11月 29 16:57 ./
drwxrwxr-x 4 ban ban     4096 11月 29 09:59 ../
-rw-r--r-- 1 ban ban    11357 11月 23 00:20 LICENSE
-rw-r--r-- 1 ban ban       63 11月 23 00:20 NOTICE
-rw-rw-r-- 1 ban ban      428 11月 29 16:57 snmp.yml
-rw-r--r-- 1 ban ban  1435911 11月 23 00:20 snmp.yml_ORG
-rwxr-xr-x 1 ban ban 15867098 11月 23 00:14 snmp_exporter*
ban@UoVb:~/prometheus/snmp_exporter-0.21.0.linux-amd64$

prometheus.ymlにsnmp_exproterの設定を追加


snmp_exproterの実行

ban@UoVb:~/prometheus/snmp_exporter-0.21.0.linux-amd64$ ./snmp_exporter

ポート9116でアクセスされるのでufwの穴あけとVirtualboxのポートフォワーディングもしておく

ban@UoVb:~/prometheus/snmp_exporter-0.21.0.linux-amd64$ sudo ufw allow 9116/tcp

Discussion