2014年2月19日水曜日

zabbixに格納されているデータをsqlを操作して抽出


zabbixに格納されているデータはGUIからでも抽出できるのだがその数に制限がある。
データベースを参照して格納されているデータをエクスポートする。

バックエンドにはmysqlを使っているとする。
ユーザ名はzabbix, パスワードはpasswordとする。


目的とするホスト名とアイテム名からのデータ参照方法

1. hostsテーブルからhostidの抽出

$ mysql -u zabbix -ppassword zabbix -e "select host, hostid from hosts"

+-------+--------+
| host  | hostid |
+-------+--------+
| host1 |  10140 |
| host2 |  10141 |


2. itemsテーブルからitemidの抽出
$ mysql -u zabbix -ppassword zabbix -e "select name, itemid from items where hostid=10140"

1.8系ではnameカラムではなく、descriptionカラムである。

+-------+--------+
| name  | itemid |
+-------+--------+
| item1 |  25497 |
| item2 |  25498 |


3. history系のテーブルにてvalueの抽出

$ mysql -u zabbix -ppassword zabbix -e "select from_unixtime(clock), value from history where itemid=25497 limit 100"

データの型ごとにテーブルが異なる。
(例)
history – numeric (float)
history_uint – numeric (unsigned integers)

+----------------------+---------+
| from_unixtime(clock) |  value  |
+----------------------+---------+
| 2014-01-01 00:00:00  | 10.8300 |
| 2014-01-01 00:00:00  | 11.5700 |




スクリーンに登録されたグラフからのデータ参照方法

1. screensテーブルにてscreenidの抽出

$ mysql -u zabbix -ppassword zabbix -e "select name, screenid from screens"

+---------+----------+
| name    | screenid |
+---------+----------+
| screen1 |       16 |
| screen2 |       17 |


2. screens_itemsテーブルからresourceid(graphsid)を抜き出し、
 graphsテーブルからgraphidを抽出

graphid == resourceidのようである。

$ mysql -u zabbix -ppassword zabbix -e "\
select graphid, name from graphs where graphid
in(select resourceid from screens_items where screenid=16)"

+--------+---------+
| name   | graphid |
+--------+---------+
| graph1 |     524 |
| graph2 |     525 |


3. graphs_itemsテーブルからitemidを抜き出し、
 itemsテーブルから必要なitemidを確認

$ mysql -u zabbix -ppassword zabbix -e "\
select name, itemid from items where itemid 
in(select itemid from graphs_items where graphid=524)"

1.8系ではnameカラムではなく、descriptionカラムである。

+-------+--------+
| name  | itemid |
+-------+--------+
| item1 |  25497 |
| item2 |  25498 |


あとは先に記載した、
"history系のテーブルにてvalueの抽出"手順を実施すればよい。