準備
Amazon Linux release 2 (Karoo)
# amazon-linux-extras install -y docker=18.03.1のようにバージョン指定でインストールも可能
$ sudo amazon-linux-extras install -y docker
# dockerサービス起動
$ sudo systemctl start docker
# DockerはUnixソケットをTCPポートにバインドするため、
# rootユーザーでしか動作しません。sudoコマンドを
# 使いたくないという場合は、dockerグループを作成して
# そこにユーザーを追加してください
# なお、sudoコマンドでdockerコマンドを実行できるように
# するには、dockerグループにユーザー追加後にログオフして
# 再度ログインする必要があります
$ sudo usermod -a -G docker $USER
# 確認コマンド
$ cat /etc/group | grep docker
# dockerサービスの自動起動を有効にする
$ sudo systemctl enable docker
# Python3のインストール
sudo yum install python3
cd
python3 -m venv ~/myvenv/
SNMP Trapの受信サーバーのDockerコンテナを起動
$ docker run -it --rm -p 162:162/udp --name docker-snmptrap sig9/snmptrapd
Created directory: /var/lib/net-snmp/mib_indexes
NET-SNMP version 5.7.3
別のターミナルで作業
$ mkdir pywork
$ cd pywork
$ git clone https://github.com/SUSE/prometheus-webhook-snmp.git
$ cd prometheus-webhook-snmp
# Python 仮想環境に切り替え
$ source ~/myvenv/bin/activate
$ pip install -r requirements.txt
$ python prometheus-webhook-snmp test
Traceback (most recent call last):
File "prometheus-webhook-snmp", line 6, in <module>
import click
ImportError: No module named click
# clickを追加
$ pip install click
# テスト実行
$ python prometheus-webhook-snmp test
SNMP Trapの受信サーバーのDockerコンテナを起動したターミナル
Created directory: /var/lib/net-snmp/mib_indexes
NET-SNMP version 5.7.3
2019-07-10 16:32:39 172.17.0.1 [UDP: [172.17.0.1]:44698->[172.17.0.3]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.50495.15.1.2.1 SNMPv2-SMI::enterprises.50495.15.1.1.1 = STRING: "load_0" SNMPv2-SMI::enterprises.50495.15.1.1.2 = STRING: "firing" SNMPv2-SMI::enterprises.50495.15.1.1.3 = STRING: "info"SNMPv2-SMI::enterprises.50495.15.1.1.4 = STRING: "localhost:9100" SNMPv2-SMI::enterprises.50495.15.1.1.5 = STRING: "node-exporter" SNMPv2-SMI::enterprises.50495.15.1.1.6 = STRING: "Instance localhost:9100 load is over 0!" SNMPv2-SMI::enterprises.50495.15.1.1.7 = STRING: "{\"foo\": \"bar\", \"xyz\": \"abc\"}" SNMPv2-SMI::enterprises.50495.15.1.1.8 = Timeticks: (1562776359) 180 days, 21:02:43.59 SNMPv2-SMI::enterprises.50495.15.1.1.9 = STRING: "{\"status\": \"firing\", \"labels\": {\"foo\": \"bar\", \"xyz\": \"abc\"}, \"annotations\": {}, \"startsAt\": \"2019-07-10T16:32:39.391469Z\", \"endsAt\": \"0001-01-01T00:00:00Z\", \"generatorURL\": \"http://foo:9090/graph?...\"}"
実行
# 受信サーバーが同じlocalhostでポートが162の場合は、以下のコマンドラインパラメータは不要
# --snmp-host ホスト名(またはIPアドレス) -- snmp-port ポート番号
#
# python prometheus-webhook-snmp --snmp-host localost -- snmp-port 162 run でも同じ
$ python prometheus-webhook-snmp run
curl http://localhost:9099/ -X POST -H "Content-Type: application/json" -d @- << EOF
{
"receiver": "bar",
"status": "firing",
"alerts": [
{
"status": "firing",
"labels": {
"alertname": "load_0",
"instance": "localhost:9100",
"job": "node-exporter",
"severity": "info",
"foo": "bar",
"xyz": "abc"
},
"annotations": {
"description": "localhost:9100 of job node-exporter load is over 0!",
"summary": "Instance localhost:9100 load is over 0!"
},
"startsAt": "2019-07-10T18:20:48.855644Z",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://foo:9090/graph?..."
}
],
"groupLabels": {},
"commonLabels": {},
"commonAnnotations": {},
"externalURL": "",
"version": "4",
"groupKey": "{}:{}"
}
EOF
$ docker run -it --rm -p 162:162/udp --name docker-snmptrap sig9/snmptrapd
Created directory: /var/lib/net-snmp/mib_indexes
NET-SNMP version 5.7.3
2019-07-10 18:24:49 172.17.0.1 [UDP: [172.17.0.1]:58558->[172.17.0.3]:162]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (0) 0:00:00.00 SNMPv2-MIB::snmpTrapOID.0 = OID: SNMPv2-SMI::enterprises.50495.15.1.2.1 SNMPv2-SMI::enterprises.50495.15.1.1.1 = STRING: "load_0" SNMPv2-SMI::enterprises.50495.15.1.1.2 = STRING: "firing" SNMPv2-SMI::enterprises.50495.15.1.1.3 = STRING: "info"SNMPv2-SMI::enterprises.50495.15.1.1.4 = STRING: "localhost:9100" SNMPv2-SMI::enterprises.50495.15.1.1.5 = STRING: "node-exporter" SNMPv2-SMI::enterprises.50495.15.1.1.6 = STRING: "Instance localhost:9100 load is over 0!" SNMPv2-SMI::enterprises.50495.15.1.1.7 = STRING: "{\"foo\": \"bar\", \"xyz\": \"abc\"}" SNMPv2-SMI::enterprises.50495.15.1.1.8 = Timeticks: (1562782848) 180 days, 21:03:48.48 SNMPv2-SMI::enterprises.50495.15.1.1.9 = STRING: "{\"status\": \"firing\", \"labels\": {\"foo\": \"bar\", \"xyz\": \"abc\"}, \"annotations\": {}, \"startsAt\": \"2019-07-10T18:20:48.855644Z\", \"endsAt\": \"0001-01-01T00:00:00Z\", \"generatorURL\": \"http://foo:9090/graph?...\"}"