Net-SNMP で受けたTrapをMariaDBに記録する

Issue

RedhatでSNMPTrapの受信サーバを作ろうとしました。

Redhat7ではMySQLが標準DBから外され、互換性のあるMariaDBに変わっています。

Net-SNMPの標準機能であるMySQL出力機能をMariaDBに置き換えて利用できるかなと試してみました。

Environment

  • Redhat EL 7.3
    [root@localhost ~]# cat /etc/redhat-release
    Red Hat Enterprise Linux Server release 7.3 (Maipo)
  • MariaDB
    [root@localhost ~]# mysql -V
    mysql  Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1
  • Net-SNMP
    [root@localhost ~]# rpm -q net-snmp
    net-snmp-5.7.2-24.el7_2.1.x86_64

Resolution

1.Redhatのインストール

インストール、インストール後の初期設定については割愛します。

※インストール時に次のようにパケージを選択しておけば、後から追加パケージを導入しなくてもよいですね。

2.snmpd.confを設定

/etc/snmp/snmpd.confを以下のように編集します。

# com2secディレクティブ:コミュニティ名とリクエスト元のペアにセキュリティ名を付ける
# com2sec "セキュリティ名" "リクエスト元" "コミュニティ名"
com2sec localhost localhost       public
com2sec localnet  192.168.1.0/24  public

# groupディレクティブ:セキュリティ名とセキュリティモデルのペアにグループ名を付ける
# group "グループ名" "セキュリティモデル" "セキュリティ名"
group MyROGroup v1         localnet
group MyROGroup v2c        localnet
group MyROGroup v1         localhost
group MyROGroup v2c        localhost

# viewディレクティブ:MIBツリーの範囲を定義して名前を付ける
# view "ビュー名" "includedまたはexclude" ツリーの基点 マスク
view all    included  .1                               0

# accessディレクティブ:グループ名とビュー名を紐付ける
access MyROGroup ""      any       noauth    exact  all    none   none

syscontact yoshipon@xxx.com
syslocation Kyoto

3.snmptrapd.confを設定

/etc/snmp/snmptrapd.confを以下のように設定します。

snmpTrapdAddr udp:162,tcp:162
doNotRetainNotificationLogs yes
doNotLogTraps no
doNotFork no
pidFile /var/run/snmptrapd.pid

# authCommunity   TYPES COMMUNITY   [SOURCE [OID | -v VIEW ]]
authCommunity     log   public
disableAuthorization no

sqlMaxQueue 5
sqlSaveInterval 0

4.trapを発行、受信できるかテスト

  1. systemctlでsnmpdとsnmptrapdを起動します。

    # systemctl start snmpd

    # systemctl start snmptrapd

  2. テストトラップを発行してみます。

    # snmptrap -v 2c -c public localhost  '' .1.3.6.1.4.1.8072.99999 .1.3.6.1.4.1.8072.99999.1 s "v2c send test"
  3. システムログに出力されたことを確認します。

    # tail /var/log/messages

    Dec 12 14:58:00 localhost snmptrapd[2643]: 2016-12-12 14:57:59 <UNKNOWN> [UDP: [192.168.1.X]:50131->[192.168.1.X]:162]:#012.1.3.6.1.2.1.1.3.0 = Timeticks: (49834) 0:08:18.34#011.1.3.6.1.6.3.1.1.4.1.0 = OID: .1.3.6.1.4.1.8072.99999#011.1.3.6.1.4.1.8072.99999.1 = STRING: "v2c send test"

 

※もし、上手く受信できない時はfirewallを確認ください。

 

# firewall-cmd --get-active-zones  …アクティブゾーンの確認

# firewall-cmd --zone=[アクティブゾーン名] --list-all  …定義情報確認
必要に応じて、162/udp、161/udpで通信ができるようにしてください。

5.MariaDBにtrap受信用テーブルを作成

  1. MariaDBを起動する

    # systemctl start mariadb

  2. 初期設定

    # mysql_secure_installation
    
  3. MariaDBにログインし、データベースとアカウントを作成

    # mysql -u root -p

    MariaDB> CREATE DATABASE net_snmp;
    Query OK, 1 row affected (0.04 sec)
     
    MariaDB> CREATE USER trapper IDENTIFIED BY 'trapass';
    Query OK, 0 rows affected (0.08 sec)
     
    MariaDB> GRANT ALL ON net_snmp.* TO trapper;
    Query OK, 0 rows affected (0.03 sec)

    MariaDB> select Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv from db where User="trapper";
    +-----------+----------+---------+-------------+-------------+-------------+-------------+
    | Host      | Db       | User    | Select_priv | Insert_priv | Update_priv | Delete_priv |
    +-----------+----------+---------+-------------+-------------+-------------+-------------+
    | localhost | net_snmp | trapper | Y           | Y           | Y           | Y           |
    +-----------+----------+---------+-------------+-------------+-------------+-------------+
    1 row in set (0.02 sec)
     
    MariaDB> \q
    Bye

  4. 作成したアカウントで再ログイン

    # mysql -u trapper -p net_snmp

  5. Trap受信用テーブル作成

    MariaDB> source schema-snmptrapd.sql
    Database changed
    Query OK, 0 rows affected, 1 warning (0.02 sec)
     
    Query OK, 0 rows affected (0.18 sec)
     
    Query OK, 0 rows affected, 1 warning (0.00 sec)
     
    Query OK, 0 rows affected (0.08 sec)
     
    MariaDB> show tables;
    +--------------------+
    | Tables_in_net_snmp |
    +--------------------+
    | notifications      |
    | varbinds           |
    +--------------------+
    2 rows in set (0.00 sec)

※schema-snmptrapd.sqlがマシン上に存在しない場合

Net-SNMPの本家からソースファイルをダウンロードして、zipファイル内から取得します。
https://sourceforge.net/projects/net-snmp/files/net-snmp/5.5.2.1/net-snmp-5.5.2.1.zip

6.Net-SNMPからMariaDBへ接続する設定

/etc/my.cnfに追記

[snmptrapd]
user=trapper
password=trapass
host=localhost

/etc/snmp/snmptrapd.confのsqlMaxQueueとsqlSaveIntervalを修正

snmpTrapdAddr udp:162,tcp:162
doNotRetainNotificationLogs yes
doNotLogTraps no
doNotFork no
pidFile /var/run/snmptrapd.pid

# authCommunity   TYPES COMMUNITY   [SOURCE [OID | -v VIEW ]]
authCommunity     log   public
disableAuthorization no

sqlMaxQueue 140
sqlSaveInterval 9

MariaDBを再起動

# systemctl restart mariadb


snmptrapdを再起動

 

# systemctl restart snmptrapd

7.trapを発行してMariaDBに書きこむテスト

  1. trapを発行

    # snmptrap -v 2c -c public localhost  '' .1.3.6.1.4.1.8072.99999 .1.3.6.1.4.1.8072.99999.1 s "v2c send test"
  2. MariaDBにログインし、テーブル内容を確認

    #  mysql -u trapper -p  net_snmp

MariaDB [net_snmp]> SELECT trap_id,date_time,type,version,snmpTrapOID,transport,security_model from notifications;
+---------+---------------------+-------+---------+-------------------------+-----------------------------------------------+----------------+
| trap_id | date_time           | type  | version | snmpTrapOID             | transport                                     | security_model |
+---------+---------------------+-------+---------+-------------------------+-----------------------------------------------+----------------+
|       1 | 2016-12-12 10:21:20 | trap2 | v2c     | .1.3.6.1.4.1.8072.99999 | UDP: [127.0.0.1]:60008->[127.0.0.1]:162       | snmpV2c        |
---+-------------------------+-----------------------------------------------+----------------+
1 rows in set (0.00 sec)

MariaDB [net_snmp]> select * from varbinds;
+---------+---------------------------+-----------+-----------------------------------------------+
| trap_id | oid                       | type      | value                                         |
+---------+---------------------------+-----------+-----------------------------------------------+
|       1 | .1.3.6.1.2.1.1.3.0        | timeticks | Timeticks: (344061) 0:57:20.61                |
|       1 | .1.3.6.1.6.3.1.1.4.1.0    | oid       | OID: .1.3.6.1.4.1.8072.99999                  |
|       1 | .1.3.6.1.4.1.8072.99999.1 | octet     | STRING: "v2c send test"                     |
+---------+---------------------------+-----------+-----------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [net_snmp]> \q
Bye

Troubleshoot

①SELinuxを停止する

 

以下で一時的にSELinuxを停止できます。

 

# setenforce 0

 

 

②/var/log/messagesなどのログファイルでエラーが出力されていないか確認

 

③サービスが稼働しているか確認

 

# systemctl status snmptrapd

# systemctl status mariadb

 

④firewallを確認し、mariadb、162/udpが解放されているか確認

 

# firewall-cmd --zone=[アクティブゾーン名] --list-all