Zabbix API 
@社内勉強会 2014/12 林原1
Agenda 
1. Zabbix APIについて 
2. サンプル1 
3. サンプル2 
@社内勉強会 2014/12 林原2
Zabbix APIについて 
—軽量なJSON RPC形式 
出来ること 
—Zabbix上のデータの取得・アイテムの登録・更新 
—大体なんでも出来る 
—構成管理/CIツールとの連携(Ansible, Puppet, Chef, 
@社内勉強会 2014/12 林原3
Sample.1 
ホスト一覧を取得し、アイテム一覧からアイテムの値を 
取得する 
@社内勉強会 2014/12 林原4
1. トークン発行 
curl -s -XGET -H "Content-Type:application/json-rpc" -d  
'{ 
"id": "1", 
"params": { 
"user": "username", 
"password": "password" 
}, 
"method": "user.login", 
"jsonrpc": "2.0" 
}'  
https://zabbix.example.com/api_jsonrpc.php | jq . 
@社内勉強会 2014/12 林原5
1. トークン発行 
{ 
"jsonrpc": "2.0", 
"result": "3f9b110f93368a0786efee16bc907e6f", 
"id": "1" 
} 
resultをトークンとして利用 
@社内勉強会 2014/12 林原6
2. host.get 
curl -s -XGET -H "Content-Type:application/json-rpc" -d  
'{ 
"id": "2", 
"params": { 
"output": "extend" 
}, 
"method": "host.get", 
"jsonrpc": "2.0", 
"auth": "3f9b110f93368a0786efee16bc907e6f" 
}'  
https://zabbix.example.com/api_jsonrpc.php |  
jq -r '.result[]|{hostid,name}| if .name == "hostname" then .hostid,.name else "" end' 
@社内勉強会 2014/12 林原7
3. item.get 
curl -s -XGET -H "Content-Type:application/json-rpc" -d  
'{ 
"id": "2", 
"params": { 
"output": "extend", 
"hostids": "10160" 
}, 
"method": "item.get", 
"jsonrpc": "2.0", 
"auth": "3f9b110f93368a0786efee16bc907e6f" 
}'  
https://zabbix.example.com/api_jsonrpc.php |  
jq -r '.result | .[] | if .key_ == "keyname" then .itemid,.name else "" end' 
@社内勉強会 2014/12 林原8
4. history.get 
curl -s -XGET -H "Content-Type:application/json-rpc" -d  
'{ 
"id": "2", 
"params": { 
"output": "extend", 
"itemids": "56553", 
"history": 1, 
"limit": 100, 
"sortfield": "clock" 
}, 
"method": "history.get", 
"jsonrpc": "2.0", 
"auth": "3f9b110f93368a0786efee16bc907e6f" 
}'  
https://zabbix.example.com/api_jsonrpc.php | jq . 
@社内勉強会 2014/12 林原9
Sample.2 
グループをメンテナンス状態にする 
@社内勉強会 2014/12 林原10
1. hostgroup.get 
curl -s -XGET -H "Content-Type:application/json-rpc" -d  
'{ 
"id": "2", 
"params": { 
"output": "extend" 
}, 
"method": "hostgroup.get", 
"jsonrpc": "2.0", 
"auth": "3f9b110f93368a0786efee16bc907e6f" 
}'  
https://zabbix.example.com/api_jsonrpc.php |  
jq -r '.result[]' 
@社内勉強会 2014/12 林原11
2. maintenance.create 
curl -s -XGET -H "Content-Type:application/json-rpc" -d  
'{ 
"jsonrpc": "2.0", 
"method": "maintenance.create", 
"params": { 
"name": "maintenance test", 
"active_since": 1417583941, 
"active_till": 1417670353, 
"groupids": [ 
"34" 
], 
"timeperiods": [ 
{ 
"timeperiod_type": 0, 
"start_time": 64800, 
"period": 3600 
} 
] 
}, 
"auth": "3f9b110f93368a0786efee16bc907e6f", 
"id": 1 
}'  
https://zabbix.example.com/api_jsonrpc.php | jq . 
@社内勉強会 2014/12 林原12
クライアントライブラリ 
—Python 
https://github.com/gescheit/scripts/tree/master/ 
zabbix 
—Ruby 
zabbixapi 
https://github.com/express42/zabbixapi 
@社内勉強会 2014/12 林原13
—PHP 
PhpZabbixApi 
http://zabbixapi.confirm.ch/ 
@社内勉強会 2014/12 林原14
おまけ 
—おすすめChromeプラグイン.1 
Zabbix-notifier 
https://chrome.google.com/ 
webstore/detail/zabbix-notifier/ 
ikeijbmpddnkaeejokgifioccbcijjf 
o?hl=ja 
@社内勉強会 2014/12 林原15
—おすすめChromeプラグイン.2 
Chromix 
https://chrome.google.com/webstore/detail/ 
chromix/odjpdjeegacmncmodjbeboldofhljjjf/ 
reviews?hl=ja 
@社内勉強会 2014/12 林原16

Zabbix API

  • 1.
  • 2.
    Agenda 1. ZabbixAPIについて 2. サンプル1 3. サンプル2 @社内勉強会 2014/12 林原2
  • 3.
    Zabbix APIについて —軽量なJSONRPC形式 出来ること —Zabbix上のデータの取得・アイテムの登録・更新 —大体なんでも出来る —構成管理/CIツールとの連携(Ansible, Puppet, Chef, @社内勉強会 2014/12 林原3
  • 4.
  • 5.
    1. トークン発行 curl-s -XGET -H "Content-Type:application/json-rpc" -d '{ "id": "1", "params": { "user": "username", "password": "password" }, "method": "user.login", "jsonrpc": "2.0" }' https://zabbix.example.com/api_jsonrpc.php | jq . @社内勉強会 2014/12 林原5
  • 6.
    1. トークン発行 { "jsonrpc": "2.0", "result": "3f9b110f93368a0786efee16bc907e6f", "id": "1" } resultをトークンとして利用 @社内勉強会 2014/12 林原6
  • 7.
    2. host.get curl-s -XGET -H "Content-Type:application/json-rpc" -d '{ "id": "2", "params": { "output": "extend" }, "method": "host.get", "jsonrpc": "2.0", "auth": "3f9b110f93368a0786efee16bc907e6f" }' https://zabbix.example.com/api_jsonrpc.php | jq -r '.result[]|{hostid,name}| if .name == "hostname" then .hostid,.name else "" end' @社内勉強会 2014/12 林原7
  • 8.
    3. item.get curl-s -XGET -H "Content-Type:application/json-rpc" -d '{ "id": "2", "params": { "output": "extend", "hostids": "10160" }, "method": "item.get", "jsonrpc": "2.0", "auth": "3f9b110f93368a0786efee16bc907e6f" }' https://zabbix.example.com/api_jsonrpc.php | jq -r '.result | .[] | if .key_ == "keyname" then .itemid,.name else "" end' @社内勉強会 2014/12 林原8
  • 9.
    4. history.get curl-s -XGET -H "Content-Type:application/json-rpc" -d '{ "id": "2", "params": { "output": "extend", "itemids": "56553", "history": 1, "limit": 100, "sortfield": "clock" }, "method": "history.get", "jsonrpc": "2.0", "auth": "3f9b110f93368a0786efee16bc907e6f" }' https://zabbix.example.com/api_jsonrpc.php | jq . @社内勉強会 2014/12 林原9
  • 10.
  • 11.
    1. hostgroup.get curl-s -XGET -H "Content-Type:application/json-rpc" -d '{ "id": "2", "params": { "output": "extend" }, "method": "hostgroup.get", "jsonrpc": "2.0", "auth": "3f9b110f93368a0786efee16bc907e6f" }' https://zabbix.example.com/api_jsonrpc.php | jq -r '.result[]' @社内勉強会 2014/12 林原11
  • 12.
    2. maintenance.create curl-s -XGET -H "Content-Type:application/json-rpc" -d '{ "jsonrpc": "2.0", "method": "maintenance.create", "params": { "name": "maintenance test", "active_since": 1417583941, "active_till": 1417670353, "groupids": [ "34" ], "timeperiods": [ { "timeperiod_type": 0, "start_time": 64800, "period": 3600 } ] }, "auth": "3f9b110f93368a0786efee16bc907e6f", "id": 1 }' https://zabbix.example.com/api_jsonrpc.php | jq . @社内勉強会 2014/12 林原12
  • 13.
    クライアントライブラリ —Python https://github.com/gescheit/scripts/tree/master/ zabbix —Ruby zabbixapi https://github.com/express42/zabbixapi @社内勉強会 2014/12 林原13
  • 14.
    —PHP PhpZabbixApi http://zabbixapi.confirm.ch/ @社内勉強会 2014/12 林原14
  • 15.
    おまけ —おすすめChromeプラグイン.1 Zabbix-notifier https://chrome.google.com/ webstore/detail/zabbix-notifier/ ikeijbmpddnkaeejokgifioccbcijjf o?hl=ja @社内勉強会 2014/12 林原15
  • 16.
    —おすすめChromeプラグイン.2 Chromix https://chrome.google.com/webstore/detail/ chromix/odjpdjeegacmncmodjbeboldofhljjjf/ reviews?hl=ja @社内勉強会 2014/12 林原16