LoginSignup
2
3

More than 5 years have passed since last update.

Zabbix テキストログ監視の時間帯抑制

Last updated at Posted at 2014-10-02

目的

Zabbix標準の「メンテナンス機能」はホストまたはホストグループ単位でしか設定が出来ず、対象ホストのアラートが全て抑制されてしまうと都合が悪い場合があります。

今回は、ログ監視機能で「特定の文字列」を「特定の時間帯だけ」監視または監視対象外にする方法を考えたいと思います。

方針

前提としてログ監視アイテムにて「正規表現」によるフィルタを使用しているものとします。

例)アイテムキー
  log[/var/log/pgsql_log,__@PostgreSQL-messages__,UTF-8,100]

尚、「正規表現」の設定については、ZabbixAPIでクラスが用意されていないようです。
そのためスクリプトからSQLで直接レコードを変更してみます。

手順

1.変更したい正規表現名の「regexpid」を以下のSQLで調べます。

例)「PostgreSQL-messages」を変更する場合。下記の結果から「regexpid」は「8」となります。

mysql> select e.expressionid,
     e.regexpid,
     r.name,
     e.expression
          from expressions e
          inner join regexps r on e.regexpid = r.regexpid;

+--------------+----------+---------------------+----------------------------------------------------+
| expressionid | regexpid | name                | expression                                         |
+--------------+----------+---------------------+----------------------------------------------------+
|          260 |        3 | zabbix-agent        | alert,crit,down,emerg,error,fail,kernel,panic,warn |
|          299 |        5 | Linux-messages      | alert,crit,down,emerg,error,fail,panic,warn        |
|          300 |        5 | Linux-messages      | pe-warn-96.bz2                                     |
|          302 |     ★ 8 | PostgreSQL-messages | PANIC,FATAL,ERROR                                  |
+--------------+----------+---------------------+----------------------------------------------------+

2.変更を自動化するためにシェルスクリプトにしました。まずは設定ファイルを作ります。

postgre.conf
# ===設定ファイル見本===
# 1行目 regexpid
# 2行目 条件式
# 3行目 条件式の形式
#         0:文字列が含まれる
#         1:いずれかの文字列が含まれる
#         2:文字列が含まれない
#         3:結果が真
#         4:結果が偽
# 4行目 区切り文字( , . / のいずれか)
# 5行目 大文字小文字を区別(0:区別しない/1:区別する)
###########################################################
8
terminating connection due to administrator command
2
,
1

3.スクリプトとコンフィグファイルを適当なところに配置しcronに登録します。

スクリプト
https://github.com/usiusi360/zabbix_exp_change

  ※第一引数 1の場合:追加
   00 10 * * * /root/tools/zabbix_exp_change/zabbix_exp_change.sh 1 /root/tools/zabbix_exp_change/postgre.conf
  ※第一引数 2の場合:削除
   30 10 * * * /root/tools/zabbix_exp_change/zabbix_exp_change.sh 2 /root/tools/zabbix_exp_change/postgre.conf

4.時間になると自動的に除外文字列が追加されます。
  尚、実際にZabbix-Serverが変更を認識し、エージェントに設定が反映されるには60秒程度掛かります。

mysql> select * from expressions;
+--------------+----------+----------------------------------------------------+-----------------+---------------+----------------+
| expressionid | regexpid | expression                                         | expression_type | exp_delimiter | case_sensitive |
+--------------+----------+----------------------------------------------------+-----------------+---------------+----------------+
|          260 |        3 | alert,crit,down,emerg,error,fail,kernel,panic,warn |               1 | ,             |              0 |
|          299 |        5 | alert,crit,down,emerg,error,fail,panic,warn        |               1 | ,             |              0 |
|          300 |        5 | pe-warn-96.bz2                                     |               2 | ,             |              0 |
|          302 |        8 | PANIC,FATAL,ERROR                                  |               1 | ,             |              1 |
+--------------+----------+----------------------------------------------------+-----------------+---------------+----------------+

                         ↓ ↓ ↓ ↓ ↓

+--------------+----------+-----------------------------------------------------+-----------------+---------------+----------------+
| expressionid | regexpid | expression                                          | expression_type | exp_delimiter | case_sensitive |
+--------------+----------+-----------------------------------------------------+-----------------+---------------+----------------+
|          260 |        3 | alert,crit,down,emerg,error,fail,kernel,panic,warn  |               1 | ,             |              0 |
|          299 |        5 | alert,crit,down,emerg,error,fail,panic,warn         |               1 | ,             |              0 |
|          300 |        5 | pe-warn-96.bz2                                      |               2 | ,             |              0 |
|          302 |        8 | PANIC,FATAL,ERROR                                   |               1 | ,             |              1 |
|          303 |     ★ 8 | terminating connection due to administrator command |               2 | ,             |              1 |
+--------------+----------+-----------------------------------------------------+-----------------+---------------+----------------+
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3