你还在为研究zabbix数据库结构而发愁么?nonono,其实zabbix已经提供出了一套非常全面的api。现在玩的还不是很深,刚开始接触,记录下。

官方地址:https://www.zabbix.com/documentation/2.0/manual/appendix/api/api

添加主机:

添加主机分为两步:1是认证并且取得加密字段,2是用加密字段将json数据传给zabbix的php脚本。

1:获取认证字符串

curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"auth": null,"id":0}' http://192.168.122.103/zabbix/api_jsonrpc.php

之后你会得到一串输出:{"jsonrpc":"2.0","result":"24640d45a3413e304cd60c8dc526559e","id":0}。很好,记住这段输出

2:添加主机:

curl -i -X POST -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0","method": "host.create","params": {"host": "172.16.220.243","interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "172.16.220.243","dns": "","port": "10050"}],"groups": [{"groupid": "2"}],"templates": [{"templateid": "10087"}]},"auth": "24640d45a3413e304cd60c8dc526559e ","id": 1}' http://192.168.122.103/zabbix/api_jsonrpc.php

之后当你得到这样的输出:{"jsonrpc":"2.0","result":{"hostids":["10090"]},"id":1}。恭喜你,添加成功了。

值得注意的是,即使你不想配置dns,这里也要写上,必选项。