LoginSignup
2
1

More than 3 years have passed since last update.

Zabbix5.0 のテンプレートを覗いてみた

Last updated at Posted at 2020-12-06

はじめに

Zabbix のテンプレートは XML 形式でインポート・エクスポートできるようになっていますが、Zabbix 5.0 ではどのように定義されるのか、標準テンプレートの一つである 「Template App Apache by HTTP」を題材に確認してみました。

ちなみにマニュアルは 2 Templates [Zabbix Documentation 5.0] にあります。

テンプレートの出力

Zabbix の管理画面にログインした後、設定 > テンプレート から「Template App Apache by HTTP」を選択し、エクスポートします。(zbx_export_templates.xml というファイル名でエクスポートされます)

Template App Apache by HTTP の中身の確認

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
    <version>5.0</version>
    <date>2020-12-06T03:38:24Z</date>
    <groups>
        <group>
            <name>Templates/Applications</name>
        </group>
    </groups>
  • <date>...</date> は、エクスポートした時の日時です
  • <groups>...</groups> は、エクスポートしたテンプレートのグループを定義します
    <templates>
        <template>
            <template>Template App Apache by HTTP</template>
            <name>Template App Apache by HTTP</name>
            <description>Get metrics from mod_status module using HTTP agent.&#13;
https://httpd.apache.org/docs/current/mod/mod_status.html&#13;
&#13;
You can discuss this template or leave feedback on our forum https://www.zabbix.com/forum/zabbix-suggestions-and-feedback/384764-discussion-thread-for-official-zabbix-template-apache&#13;
&#13;
Template tooling version used: 0.37</description>
            <groups>
                <group>
                    <name>Templates/Applications</name>
                </group>
            </groups>
            <applications>
                <application>
                    <name>Apache</name>
                </application>
                <application>
                    <name>Zabbix raw items</name>
                </application>
            </applications>
  • <templates>...</templates> にテンプレートを定義します
  • <template>...</template> にここのテンプレートを定義します
  • <template>...</template><name>...</name> には テンプレート名を定義しますが、前者は required な要素でユニークである必要があります。
  • <description>...</description> にはテンプレートの説明を定義します
  • <groups>...</groups> にグループを定義します
  • <group><name>...</name></group> にグループ名を記述します。これは複数定義できます
  • <application><name>...</name></application> にアプリケーション名を定義します。これは複数定義できます
            <items>
                <item>
                    <name>Apache: Total bytes</name>
                    <type>DEPENDENT</type>
                    <key>apache.bytes</key>
                    <delay>0</delay>
                    <history>7d</history>
                    <value_type>FLOAT</value_type>
                    <units>B</units>
                    <description>Total bytes served</description>
                    <applications>
                        <application>
                            <name>Apache</name>
                        </application>
                    </applications>
                    <preprocessing>
                        <step>
                            <type>JSONPATH</type>
                            <params>$[&quot;Total kBytes&quot;]</params>
                        </step>
                        <step>
                            <type>MULTIPLIER</type>
                            <params>1024</params>
                        </step>
                    </preprocessing>
                    <master_item>
                        <key>apache.get_status</key>
                    </master_item>
                </item>
  • <items>...</items> に監視アイテムの定義をします
  • <item>...</item> に各要素を定義します。required以外のものは省略してもよく、その場合はデフォルト値が定義されます。公式マニュアル によると、以下の要素があります。
    • name: 監視アイテム名。required
    • type: タイプ。デフォルト値は Zabbixエージェント
    • snmp_oid: SNMP object ID。SNMPエージェントの場合は required
    • key: キー。required
    • delay: 監視間隔。デフォルト値は1分
    • history: ヒストリーの保存期間。デフォルト値は90日
    • trends: トレンドの保存期間。デフォルト値は365日
    • status: 有効か無効か。デフォルト値は有効
    • value_type: データ型。デフォルト値は数値(浮動小数)
    • allowed_hosts: 監視アイテムのデータ送信を許可しているホストのIPアドレス(カンマ区切り)のリスト。trapperやHTTPエージェントで使用
    • units: 単位
    • params: 追加パラメータ
    • ipmi_sensor: IPMIセンサー
    • authtype: 認証方式。デフォルト値はパスワード
    • username: ユーザー名
    • password: パスワード
    • publickey: 公開鍵のファイル名
    • privatekey: 秘密鍵のファイル名
    • port: Custom port monitored by the item. SNMPエージェントで使用
    • description: 説明
    • inventory_link: アイテムによって入力されるホストインベントリフィールド。
    • logtimefmt: ログ中の時刻の形式
    • jmx_endpoint: JMXエンドポイント
    • url: URL。HTTPエージェントでrequired
    • allow_traps: トラッピングの有効化。デフォルト値はNO。HTTPエージェントで使用
    • follow_redirects: リダイレクトをたどるかどうか。デフォルト値はYES。HTTPエージェントで使用
  • <application><name>...</name></application> でアプリケーション名を定義します
  • <preprocessiong>...</preprocessiong> で保存前処理を定義します(詳細は割愛します)
  • <master_item><key>...</key></master_item> は依存アイテムの場合の親アイテムを定義します
                <item>
                    <name>Apache: Uptime</name>
                    <type>DEPENDENT</type>
                    <key>apache.uptime</key>
(中略)
                    <triggers>
                        <trigger>
                            <expression>{last()}&lt;10m</expression>
                            <name>Apache: has been restarted (uptime &lt; 10m)</name>
                            <priority>INFO</priority>
                            <description>Uptime is less than 10 minutes</description>
                            <manual_close>YES</manual_close>
                        </trigger>
                    </triggers>
                </item>
  • <item>...</item> の中に <triggres>...</triggers> を定義することができます。この場合、条件式 (expression) はアイテム名を省略して書けるようです
            <discovery_rules>
                <discovery_rule>
                    <name>Event MPM discovery</name>
                    <type>DEPENDENT</type>
                    <key>apache.mpm.event.discovery</key>
                    <delay>0</delay>
                    <description>Additional metrics if event MPM is used&#13;
https://httpd.apache.org/docs/current/mod/event.html</description>
                    <item_prototypes>
                        <item_prototype>
(中略)
                </discovery_rule>
            </discovery_rules>
  • <discovery_rules>...</discovery_rules> にディスカバリルールを定義します。
    • この例 (Template App Apache by HTTP) では、「Event MPM discovery」という「Get status」の依存アイテムによって、各種アイテムやグラフが生成される定義になっています。
            <macros>
                <macro>
                    <macro>{$APACHE.RESPONSE_TIME.MAX.WARN}</macro>
                    <value>10</value>
                    <description>Maximum Apache response time in seconds for trigger expression</description>
                </macro>
(中略)
            </macros>
  • <macros>...</macros> にマクロを定義します
            <screens>
                <screen>
                    <name>Apache performance</name>
                    <hsize>2</hsize>
                    <vsize>3</vsize>
                    <screen_items>
                        <screen_item>
                            <resourcetype>0</resourcetype>
                            <style>0</style>
                            <resource>
                                <name>Apache: Requests per second</name>
                                <host>Template App Apache by HTTP</host>
                            </resource>
                            <width>500</width>
                            <height>100</height>
                            <x>0</x>
                            <y>0</y>
                            <colspan>1</colspan>
                            <rowspan>1</rowspan>
                            <elements>25</elements>
                            <valign>0</valign>
                            <halign>0</halign>
                            <dynamic>0</dynamic>
                            <sort_triggers>0</sort_triggers>
                            <url/>
                            <application/>
                            <max_columns>3</max_columns>
                        </screen_item>
(中略)
                    </screen_items>
                </screen>
            </screens>
  • <screens>...</screens> にスクリーンを定義します
    <graphs>
        <graph>
            <name>Apache: Requests per second</name>
            <graph_items>
                <graph_item>
                    <drawtype>GRADIENT_LINE</drawtype>
                    <color>1A7C11</color>
                    <item>
                        <host>Template App Apache by HTTP</host>
                        <key>apache.requests.rate</key>
                    </item>
                </graph_item>
            </graph_items>
        </graph>
(中略)
    </graphs>
  • <graphs>...</graphs> にグラフを定義します
    <value_maps>
        <value_map>
            <name>Service state</name>
            <mappings>
                <mapping>
                    <value>0</value>
                    <newvalue>Down</newvalue>
                </mapping>
(中略)
            </mappings>
        </value_map>
    </value_maps>
  • <value_maps>...</value_maps> にこのテンプレートで使用する値のマッピングを定義します

テンプレートを自分で書いてみる

Zabbix5.0 で出力されたテンプレートをみると、省略できるものは書かないことで、かなりシンプルに書けることが分かりました。

例えば、 net.tcp.service[httpd] で http (80/tcp) のサービス状態を監視する場合、以下のように書けます。

                <item>
                    <name>サービス状態($1)</name>
                    <key>net.tcp.service[httpd]</key>
                    <delay>60</delay>
                    <history>30d</history>
                    <applications>
                        <application>
                            <name>service</name>
                        </application>
                    </applications>
                    <valuemap>
                        <name>Service state</name>
                    </valuemap>
                    <triggers>
                        <trigger>
                            <expression>{max(#3)}=0</expression>
                            <name>サービス停止($1)</name>
                            <priority>AVERAGE</priority>
                        </trigger>
                    </triggers>
                </item>

TODO

これをもとに、Ansible でテンプレートを生成し、Zabbix にインポートする処理をさせたいと思います。(12/10 に Ansible Advent Calendar 2020 にて公開予定です)
(12/10 追記) 公開しました! ExcelのヒアリングシートからAnsibleでZabbixに監視テンプレートを登録する

2
1
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
1