Prometheus SNMP Exporter導入メモ

目次

PrometheusでSNMP pollingを行うため、Prometheus SNMP Exporterを導入した時の記録

Prometheus SNMP Exporter (https://github.com/prometheus/snmp_exporter)を導入していく。

今回は現時点で最新のバージョン0.20.0を導入したが、

NOTE: This is a safe harbor release. Future releases will have breaking changes to the configuration format.

という記述があるため、次のリリースを使用する際などには、一部設定の修正が必要かもしれない。

導入

wget https://github.com/prometheus/snmp_exporter/releases/download/v0.20.0/snmp_exporter-0.20.0.linux-amd64.tar.gz
tar xvf snmp_exporter-0.20.0.linux-amd64.tar.gz
sudo cp snmp_exporter-0.20.0.linux-amd64/snmp_exporter /usr/local/bin/prometheus-snmp-exporter
sudo cp snmp_exporter-0.20.0.linux-amd64/snmp.yml /etc/prometheus/snmp.yml

sudo mkdir -p /var/log/prometheus
sudo mkdir -p /var/lib/prometheus

# ubuntu
if ! getent passwd prometheus > /dev/null; then
    sudo adduser --system --home /var/lib/prometheus --no-create-home --group --gecos "Prometheus daemon" prometheus
fi

# centos
if ! getent passwd prometheus > /dev/null; then
    sudo useradd --system --home-dir /var/lib/prometheus --no-create-home --comment "Prometheus daemon" prometheus
fi

sudo chown prometheus:prometheus /var/log/prometheus
sudo chown prometheus:prometheus /var/lib/prometheus

# 後始末
rm -rf snmp_exporter-0.20.0.linux-amd64
  • /lib/systemd/system/prometheus-snmp-exporter.service
    • 作成後、ll /lib/systemd/system/prometheus-snmp-exporter.serviceでroot:root 644になっていることを確認。
[Unit]
Description=Prometheus exporter for SNMP-enabled devices
Documentation=https://github.com/prometheus/snmp_exporter
After=network.target

[Service]
Restart=always
User=prometheus
ExecStart=/usr/local/bin/prometheus-snmp-exporter --config.file="/etc/prometheus/snmp.yml" $ARGS
ExecReload=/bin/kill -HUP $MAINPID
TimeoutStopSec=20s
SendSIGKILL=no

[Install]
WantedBy=multi-user.target

コミュニティ名を指定するため、手動でsnmp.ymlを書き換える。
今回はif_mibを使用するため、以下のように、if_mibの下にauth -> communityを入れる。
# 本当はgeneratorを使って生成する?ちゃんと調べていないのでよくわからない

suuei@mon:~$ diff -u snmp_exporter-0.20.0.linux-amd64/snmp.yml /etc/prometheus/snmp.yml
--- snmp_exporter-0.20.0.linux-amd64/snmp.yml   2021-02-12 20:51:40.000000000 +0900
+++ /etc/prometheus/snmp.yml    2021-02-13 20:06:59.732110892 +0900
@@ -6082,6 +6082,8 @@
     help: Describes whether the amount of available swap space (as reported by 'memAvailSwap(4)'),
       is less than the desired minimum (specified by 'memMinimumSwap(12)'). - 1.3.6.1.4.1.2021.4.101
 if_mib:
+  auth:
+    community: public
   walk:
   - 1.3.6.1.2.1.2
   - 1.3.6.1.2.1.31.1.1

配置したファイルの起動と、自動起動の設定。

sudo systemctl daemon-reload
sudo systemctl enable --now prometheus-snmp-exporter

Prometheusへの設定

SNMP Exporterが起動したところで、今度はPrometheusに設定を入れていく。

/etc/prometheus/prometheus.ymlに以下のように追記。

# snmp
- job_name: 'snmp'
static_configs:
  - targets:
    - c3560cg    # ホスト名とか
    - 127.0.0.1  # IPアドレスとか
metrics_path: /snmp
params:
  module: [if_mib]
relabel_configs:
  - source_labels: [__address__]
    target_label: __param_target
  - source_labels: [__param_target]
    target_label: instance
  - target_label: __address__
    replacement: 127.0.0.1:9116  # The SNMP exporter's real hostname:port.

sudo service prometheus restartで反映。