GrafanaでZabbix API error が出るときの、ZabbixのPHPのメモリ制限設定とVer2系へのTrends APIパッチ導入


やりたいこと

GrafanaでZabbixをデータソースにしてダッシュボードで可視化しているが、
30日以上などの長期間表示で非常に表示が遅かったり、
"Zabbix API error"というのが表示されたりする場合の対処のまとめ

ZabbixのTrendsとHistoryについて

Zabbixは指定の期間に対して、
Historyという期間内のすべての値を返すAPIと、Trendsという期間内の値を丸めたものを返すAPIがある。

Grafanaで長期間の表示をさせるときは、
Trendsが機能しないと、Historyでは応答する値が多すぎて、表示が遅くなったり、エラーになったりする。
ただし、TrendsがAPIとして提供されるのはZabbixのVer3以降で、Ver2系はZBXNEXT-1193のパッチ適用が必要となる。

Zabbixプラグインの公式ページから
Trends APIを推奨する旨の記載

Trends: enable if you use Zabbix 3.x or patch for trends support in Zabbix 2.x (ZBXNEXT-1193). This option strictly recommended for displaying long time periods (more than few days, depending of your item's updating interval in Zabbix) because few days of item history contains tons of points. Using trends will increase Grafana performance.

Grafana上のZabbixデータソースのTrends API設定

ちゃんとenableにチェックが入っていることを確認する。

ZabbixのPHPのメモリ制限

Zabbixの
/var/log/httpd/error_logを見るとPHP Fatal errorが出ている。
Zabbixコミュニティにあるこんな感じのエラー。
http://www.zabbix.jp/node/1544
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted

解決策も上記のコミュニティ回答から
/etc/httpd/conf.d/zabbix.confのPHPのメモリ制限の値を変える。
メモリには余裕があったので128M→1024Mにする

/etc/httpd/conf.d/zabbix.conf
php_value memory_limit 1024M

上記を反映するためにZabbixのApacheを再起動

# service httpd restart

ZabbixのVer2系へのTrends APIのパッチ適用

Zabbixプラグインの公式ページから
ZabbixのVer2系はZBXNEXT-1193のパッチ適用でTrendsのAPI追加が必要。

Trends: enable if you use Zabbix 3.x or patch for trends support in Zabbix 2.x (ZBXNEXT-1193). This option strictly recommended for displaying long time periods (more than few days, depending of your item's updating interval in Zabbix) because few days of item history contains tons of points. Using trends will increase Grafana performance.

パッチ(CTrend_for_Zabbix_2_4_4.zip)を下記からダウンロード。
https://support.zabbix.com/browse/ZBXNEXT-1193

DBやフロントエンドの変更なく、API追加だけなことを確認
https://zabbix.org/wiki/Docs/specs/ZBXNEXT-1193

パッチ適用手順は下記を参考にした。
https://fabianlee.org/2017/01/08/zabbix-enabling-api-fetch-of-trend-data-in-zabbix2/

①tmpに配置し解凍

cd /tmp
unzip CTrend_for_Zabbix_2_4_4.zip

②バックアップ取得

cd /usr/share/zabbix/include
tar cvfz classes.tar.gz classes/

③パッチファイルの適用

cp -r -v /tmp/frontend/include/ /usr/share/zabbix

④再起動

service httpd restart

これで完了