ZabbixAPI触ってみた

仕様はこっち
http://www.zabbix.com/documentation/1.8/api

基本的には「api_jsonrpc.phpJSONでPOST」すればいい。
認証のところだけ注意がいりそうです。


telnetで動作を確認してみましょう

まずは認証
[zabbix@admin01 ~]$ telnet localhost 80
POST /zabbix/api_jsonrpc.php HTTP/1.1
Content-Type: application/json-rpc
Host: localhost
Content-Length: 114

{"jsonrpc":"2.0","method":"user.authenticate","params":{"user":"Admin","password":"password"},"auth":null,"id":1}

レスポンス

HTTP/1.1 200 OK
Date: Sat, 30 Apr 2011 11:40:06 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.1.6
Set-Cookie: zbx_sessionid=ef3be72c8c11859ad67ea4282354a1f6
Set-Cookie: zbx_sessionid=3d1f76bf5e21ef3a241237ff64df93d2
Set-Cookie: zbx_sessionid=deleted; expires=Fri, 30-Apr-2010 11:40:06 GMT
Content-Length: 68
Connection: close
Content-Type: application/json

{"jsonrpc":"2.0","result":"ef3be72c8c11859ad67ea4282354a1f6","id":1}
目的のAPIを叩く

ホストグループの一覧を取得してみましょう。
authに認証で返ってきた値をつっこみます。

[zabbix@admin01 ~]$ telnet localhost 80
POST /zabbix/api_jsonrpc.php HTTP/1.1
Content-Type: application/json-rpc
Host: localhost
Content-Length: 122

{"jsonrpc":"2.0","method":"hostgroup.get","params":{"output": "extend"},"auth":"ef3be72c8c11859ad67ea4282354a1f6","id":1}

レスポンス

HTTP/1.1 200 OK
Date: Sat, 30 Apr 2011 11:40:45 GMT
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.1.6
Content-Length: 404
Connection: close
Content-Type: application/json

{"jsonrpc":"2.0","result":[
{"groupid":"1","name":"Templates","internal":"0"},
{"groupid":"2","name":"Linux servers","internal":"0"},
{"groupid":"3","name":"Windows servers","internal":"0"},
{"groupid":"4","name":"Zabbix servers","internal":"0"},
{"groupid":"5","name":"Discovered hosts","internal":"1"},
{"groupid":"6","name":"Engineer","internal":"0"},
{"groupid":"7","name":"Twitter","internal":"0"}
],"id":1}

(見づらいので改行を追加してます。)


という感じでした。けっこう簡単ですね(APIの仕様は複雑怪奇ですが・・・)。


プルグラムから触ってみたい場合はZabbix 1.8 API – PHP Class v1.0などがあります。