自宅のzabbixでSNMP Trapを受信し、Trapをzabbix上に表示するようにしたので、その時設定したときのメモを残しておく。
めちゃめちゃ長くなるので2部構成です。。。
この記事ではSNMP Trapを受信し、ログに残すところまでを行う。
目次
概要
SNMP Trapをzabbixのトリガー画面に表示させるには、ざっくり以下のような感じで表示を行っている。
1.監視している機器がSNMP Trapを送信
2.「snmptrapd」というデーモンがSNMP Trapを受け取る
3.「snmptrapd」はTrapを受信したら「snmptthandler」というスクリプトを実行する
4.スクリプトはMIBファイルをもとに生成した設定ファイルを基に「snmptt.log」にログを書き込む
5.zabbixが「snmptt.log」にログが書き込まれたのを検知した時、ログを基にzabbixのトリガー画面に表示する
つまり、zabbix単体ではSNMP Trapの監視を行うことができず、これを実現するには「snmptrapd」「snmptt」という2つのサービスを導入する必要がある。
導入環境
導入するサーバ/NW機器の環境は以下の通り。zabbixはあらかじめ導入されている。
zabbixは/opt配下に配置されており、今回導入する「snmptrapd」「snmptt」も/opt配下へ導入する。(バックアップが容易になるため)
/opt/
L openssl-1.0.2k/
L zabbix-server3.0.8/
snmptrapd導入
1. http://www.net-snmp.org/download.htmlから最新バージョンのnet-snmpをダウンロードする。(この記事を書いたときのバージョンは5.7.3)
2. ダウンロードしたアーカイブを展開し、コンパイルする。prefixで/opt配下にインストールされるように指定する。また、./configure実行後、対話形式で設定値を入力する場面がでてくるので、必要に応じて入力。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#tar xvf net-snmp-5.7.3.tar.gz #cd net-snmp-5.7.3/ #./configure --prefix=/opt/net-snmp-5.7.3 --exec-prefix=/opt/net-snmp-5.7.3 --with-logfile=/opt/net-snmp-5.7.3/var --with-persistent-directory=/opt/net-snmp-5.7.3/var/net-snmp --with-default-snmp-version="2" --with-openssl=/opt/openssl-1.0.2k/lib checking what to build and install... agent apps man local mibs using default persistent mask 077 using default temporary file pattern /tmp/snmpdXXXXXX using default AgentX socket /var/agentx/master using default "enterprise.net-snmp" using default enterprise sysOID "NET-SNMP-MIB::netSnmpAgentOIDs..." using default notifications "NET-SNMP-MIB::netSnmpNotifications" using OS default send buffer size for server sockets using OS default recv buffer size for server sockets using OS default send buffer size for client sockets using OS default recv buffer size for client sockets checking whether to prompt for values... configure: ************** Configuration Section ************** You are about to be prompted with a series of questions. Answer them carefully, as they determine how the SNMP agent and related applications are to function. After the configure script finishes, you can browse the newly created net-snmp-config.h file for further - less important - parameters to modify. Be careful if you re-run configure though, since net-snmp-config.h will be overwritten. -Press return to continue- <<Enterを押す yes checking Default version of SNMP to use... (cached) 2 checking System Contact Information... *** System Contact Information: Describes who should be contacted about the host the agent is running on. This information is available in the MIB-II tree. This can also be over-ridden using the "syscontact" syntax in the agent's configuration files. Providing the --with-sys-contact="contact" parameter to ./configure will avoid this prompt. System Contact Information (@@no.where): <<連絡先を入力。必要なければそのままEnter setting System Contact Information to... @@no.where checking System Location... *** System Location: Describes the location of the system. This information is available in the MIB-II tree. this can also be over-ridden using the "syslocation" syntax in the agent's configuration files. Providing the --with-sys-location="location" parameter to ./configure will avoid this prompt. System Location (Unknown): <<ロケーションを指定するところ。そのままEnterでOK |
3.make, make installを実行し、インストール
1 2 |
#make -j 4 #make install -j 4 |
4./opt配下へ移動し、設定ファイルやログを格納するディレクトリを作成。
1 2 3 |
#cd /opt/ #mkdir net-snmp-5.7.3/etc/ #mkdir -p net-snmp-5.7.3/var/log |
5.snmptrapd.confをエディタで開き編集する。
1 |
#emacs net-snmp-5.7.3/etc/snmptrapd.conf |
以下の内容を記述。
1 2 3 |
snmpTrapdAddr udp:162 authCommunity execute,log,net public traphandle default /opt/snmptt_1.4/bin/snmptthandler |
6.systemctlコマンドでsnmptrapdを実行できるようにしたいため、「/usr/lib/systemd/system/」配下に以下の内容のファイルを作成する。
1 |
#emacs /usr/lib/systemd/system/snmptrapd.service |
以下の内容を記述。
1 2 3 4 5 6 7 8 9 10 11 12 |
[Unit] Description=Simple Network Management Protocol (SNMP) Trap Daemon. After=syslog.target network.target [Service] Type=forking EnvironmentFile=-/opt/net-snmp-5.7.3/etc/snmptrapd ExecStart=/opt/net-snmp-5.7.3/sbin/snmptrapd $OPTIONS ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target |
7.起動オプションを格納するファイルを作成する。
1 |
#emacs net-snmp-5.7.3/etc/snmptrapd |
以下の内容を記述。
1 |
OPTIONS="-m +ALL -Lsd -On -c /opt/net-snmp-5.7.3/etc/snmptrapd.conf" |
8.iptablesの設定ファイルを開き、udp/162のポートの通信を許可する設定を記述
1 |
#emacs /etc/sysconfig/iptables |
以下の内容を追加。
1 2 |
##SNMP Trap -A INPUT -p udp -m state --state NEW -m udp --dport 162 -j ACCEPT |
9./etc/profileを開き、PATH環境変数に「/opt/net-snmp-5.7.3/sbin/」「/opt/net-snmp-5.7.3/bin/」を追加するように設定する。(サーバが再起動してもパスを保持する為)
また、将来追加するsnmpttのコマンドのパスもあらかじめ渡しておく。(/opt/snmptt_1.4/bin/)
1 |
#emacs /etc/profile |
以下の内容を追加。
1 |
#export PATH=$PATH:/opt/net-snmp-5.7.3/bin/:/opt/net-snmp-5.7.3/sbin/:/opt/snmptt_1.4/bin/ |
10.sourceコマンドで「/etc/profile」を実行し、PATHを通す。
1 |
#source /etc/profile |
11.「systemctl daemon-reload」を実行した後、iptablesを再起動させ、snmptrapを起動させる。
1 2 3 4 |
#systemctl daemon-reload #systemctl restart iptables #systemctl start snmptrapd #systemctl enable snmptrapd |
12.「snmpwalk」コマンドを実行し、結果がちゃんと返ってくればsnmptrapdは正しく導入されている。
1 |
#snmpwalk -v 2c -c public <機器のIPアドレス> |
snmptt導入
まずCPANを導入し、「Config::IniFiles」「Sys::Syslog」をインストールする必要があるので先にそれらを行う。ついでにnet-snmp-libsもインストールする。
1.yumでcpanをインストール
1 |
#yum -y install cpan net-snmp-libs |
2.「Config::IniFiles」「Sys::Syslog」の各モジュールをインストールする。
1 2 |
#cpan install Config::IniFiles #cpan install Sys::Syslog |
※「Can’t call method “http” on unblessed reference at /usr/share/perl5/CPAN/FirstTime.pm line 1866」みたいなエラーが出てきてインストールできない場合
■http://search.cpan.org/dist/CPAN/lib/CPAN/FirstTime.pmからCPAN-X.XX.tar.gzをダウンロードする。(この記事を書いた時点では2.16)
■アーカイブを展開する。
1 |
#tar xvf CPAN-2.16.tar.gz |
■展開したフォルダにある「FirstTime.pm」を「/usr/share/perl5/CPAN/」配下へコピーする。
1 |
#cp CPAN-2.16/lib/CPAN/FirstTime.pm /usr/share/perl5/CPAN/FirstTime.pm |
■項番2を再度実行してみる。
3.https://ja.osdn.net/projects/sfnet_snmptt/releases/から「snmptt_X.X.tgz」をダウンロードし、/opt/download配下配置する。(因みにこの記事を書いている時点での最新バージョンは1.4)
4.アーカイブを展開する。
1 |
#tar xvf snmptt_1.4.tgz |
5.カレントディレクトリを「/opt」へ移動し、snmpttのファイルを所定の位置にコピーする。(/opt/snmptt_1.4/配下へ配置するようにする)
1 2 3 4 5 6 7 8 9 10 11 |
#cd /opt/ #mkdir -p snmptt_1.4/bin #mkdir -p snmptt_1.4/var/log #mkdir snmptt_1.4/var/spool/ #mkdir snmptt_1.4/var/run/ #mkdir snmptt_1.4/etc/ #cp download/snmptt_1.4/snmptt snmptt_1.4/bin/snmptt #cp download/snmptt_1.4/snmptthandler snmptt_1.4/bin/snmptthandler #cp download/snmptt_1.4/snmpttconvertmib snmptt_1.4/bin/snmpttconvertmib #cp download/snmptt_1.4/snmptt.ini net-snmp-5.7.3/etc/snmptt.ini #cp download/snmptt_1.4/examples/snmptt.conf.generic net-snmp-5.7.3/etc/snmptt.conf |
6.「snmptrapd.conf」を以下のように編集し保存する。
1 |
#emacs net-snmp-5.7.3/etc/snmptrapd.conf |
最終行に「traphandle…」を追加する。
1 2 3 4 |
snmpTrapdAddr udp:162 authCommunity log,execute public [snmp] logOption s 7 /opt/net-snmp-5.7.3/var/log/snmptrapd.log traphandle default /opt/snmptt_1.4/bin/snmptthandle <<追加 |
7.以下のMIBファイルをダウンロードして、/opt/net-snmp-5.7.3/share/snmp/mibs/配下へ配置する。
ftp://ftp.cisco.com/pub/mibs/v1/CISCO-GENERAL-TRAPS.my
ftp://ftp.cisco.com/pub/mibs/v1/OLD-CISCO-SYSTEM-MIB.my
ftp://ftp.cisco.com/pub/mibs/v1/OLD-CISCO-INTERFACES-MIB.my
ftp://ftp.cisco.com/pub/mibs/v1/OLD-CISCO-TS-MIB.my
ftp://ftp.cisco.com/pub/mibs/v1/OLD-CISCO-TCP-MIB.my
ftp://ftp.cisco.com/pub/mibs/v2/CISCO-SYSLOG-MIB.my
https://www.cisco.com/iam/PGW_MIBS/973/CISCO-SMI.my
8.「snmpttconvertmib」でMIBファイルから、snmpttの設定ファイルを生成するのだが、このままのフォーマットだとzabbix側から読み取れないフォーマットとなってしまうのでスクリプトの改造を行う。まず、既存の「snmpttconvertmib」のバックアップを取る。
1 |
#cp /opt/snmptt_1.4/bin/snmpttconvertmib /opt/snmptt_1.4/bin/snmpttconvertmib-zbx |
9.「snmpttconvertmib-zbx」を以下のように改造していく。
1 |
#emacs /opt/snmptt_1.4/bin/snmpttconvertmib-zbx |
814行付近に修正する行がある。エディタの検索機能を使って該当行を探し出す。
1 2 3 4 5 6 7 8 9 |
print OUTPUTFILE "FORMAT $formatexec\n"; ↓↓以下に変更 print OUTPUTFILE "FORMAT ZBXTRAP \$aA \$ar \$1 $formatexec\n"; print OUTPUTFILE "FORMAT \$*\n"; ↓↓以下に変更 print OUTPUTFILE "FORMAT ZBXTRAP \$aA \$ar \$1 \$*\n" |
10.MIBファイルからsnmptt設定ファイルを生成する。
1 2 |
#snmpttconvertmib-zbx --in=/opt/net-snmp-5.7.3/share/snmp/mibs/CISCO-GENERAL-TRAPS.my --out=/opt/net-snmp-5.7.3/share/snmp/CISCO-GENERAL-TRAPS.conf #snmpttconvertmib-zbx --in=/opt/net-snmp-5.7.3/share/snmp/mibs/CISCO-SYSLOG-MIB.my --out=/opt/net-snmp-5.7.3/share/snmp/CISCO-SYSLOG-MIB.conf |
11.「snmptt.ini」を編集する。
1 |
#emacs net-snmp-5.7.3/etc/snmptt.ini |
変更をした設定値のみ下に表示している。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[General] mode = daemon [DaemonMode] pid_file = /opt/snmptt_1.4/var/run/snmptt.pid spool_directory = /opt/snmptt_1.4/var/spool/ [Logging] date_time_format = %Y/%m/%d %H:%M:%S log_file = /opt/snmptt_1.4/var/log/snmptt.log log_system_enable = 1 log_system_file = /opt/snmptt_1.4/var/log/snmpttsystem.log unknown_trap_log_enable = 1 unknown_trap_log_file = /opt/snmptt_1.4/var/log/snmpttunknown.log [TrapFiles] snmptt_conf_files = <<END /opt/net-snmp-5.7.3/share/snmp/CISCO-GENERAL-TRAPS.conf /opt/net-snmp-5.7.3/share/snmp/CISCO-SYSLOG-MIB.conf END |
12.systemctlコマンドでsnmpttを実行できるようにしたいため、「/usr/lib/systemd/system/」配下にファイルを作成する。
1 |
#emacs /usr/lib/systemd/system/snmptt.service |
以下の内容で編集する。
1 2 3 4 5 6 7 8 9 10 11 12 |
[Unit] Description=SNMP Trap Translator. After=syslog.target network.target [Service] Type=forking EnvironmentFile=-/opt/snmptt_1.4/etc/snmptt ExecStart=/opt/snmptt_1.4/bin/snmptt $OPTIONS ExecReload=/bin/kill -HUP $MAINPID [Install] WantedBy=multi-user.target |
13.起動オプションを格納するファイルを作成する。
1 |
#emacs snmptt_1.4/etc/snmptt |
以下の内容で編集する。
1 |
OPTIONS="--ini=/opt/net-snmp-5.7.3/etc/snmptt.ini --daemon" |
14.snmptrapdがSNMP Trapを受け取ったときに実行するスクリプト「snmptthandler」を少し改造する。「$ini」の定義の所を実際に配置されているiniのパスを入力する。
1 |
#emacs snmptt_1.4/bin/snmptthandler |
以下の行に変更を加える
1 2 3 |
my $ini = '' ↓↓下のように変更 my $ini = '/opt/net-snmp-5.7.3/etc/snmptt.ini'; |
15.ユーザを作成し、snmptt_1.4フォルダのアクセス権を変更する。
1 2 |
#useradd snmptt -s /bin/false -d /opt/snmptt_1.4/ #chown -R snmptt:snmptt snmptt_1.4 |
16.「systemctl daemon-reload」を実行した後、snmpttを起動させ、さらにsnmptrapdを再起動させる。
1 2 3 4 |
#systemctl daemon-reload #systemctl start snmptt #systemctl enable snmptt #systemctl restart snmptrapd |
Cisco機器側の設定 & SNMP Trap確認
1.Cisco機器側に下のコンフィグを投入する。これだけ設定しておけばSNMP Trapは送信される。
1 2 3 4 5 6 7 8 9 |
!機器のコミュニティストリングを設定 snmp-server community public RO ! !zabbixサーバへSNMP Trapを送信するように設定する snmp-server host <zabbixのIP> version 2c public ! !SNMP Trap送信を有効化する snmp-server enable traps ! |
2.今回は試しにインターフェイスのdown/upのTrapを拾ってみる。好きなポートにno shutdownコンフィグを投入する。
1 2 3 4 5 6 |
!任意のポートの no shutdown状態にする interface g1/0/3 no shutdown exit ! ! |
3.zabbixサーバ側で「/opt/snmptt_1.4/var/log/snmptt.log」をリアルタイムで監視する。
1 |
#tail -f /opt/snmptt_1.4/var/log/snmptt.log |
4.項番2で指定したポートにPCのつながったLANケーブルを挿してみてLinkUPさせる。tailコマンドで監視しているログに以下のようなものが出てくればTrap送信は正常に行われている。(ここでは192.168.100.254がSNMP Trapを送信した機器となる)
1 |
Sat Jan 20 14:50:09 2018 .1.3.6.1.4.1.9.9.41.2.0.1 Normal "Status Events" gateway - ZBXTRAP 192.168.100.254 192.168.100.254 LINK When a syslog message is generated by the device a LINK error UPDOWN Interface GigabitEthernet1/0/3, changed state to down 20:8:39:36.44 |
これで、受信したTrapをログに残すところまでは完了した。次回の記事ではそのログをもとにzabbixで参照し、トリガーが表示される所まで行う。