一、API官方文档
https://www.zabbix.com/documentation/4.0/zh/manual/api
https://www.zabbix.com/documentation/5.0/zh/manual/api
二、使用示例
1、获取zabbix token
curl -s -X POST http://192.168.1.1/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "user.login","params": {"user": "Admin","password": "zaAdmin!@#$2019"},"id": 1,"auth": null
}' | python -m json.tool
返回用户身份验证令牌:
{
"id": 1,
"jsonrpc": "2.0",
"result": "8ef873a332822d8acdf6b6f6e4fb2398"
}
2、使用api检索主机
curl -s -X POST http://192.168.1.1/zabbix/api_jsonrpc.php -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "host.get","params": {"output": ["hostid","host"],"selectInterfaces": ["interfaceid","ip"]},"id": 1,"auth": "8ef873a332822d8acdf6b6f6e4fb2398"
}' | python -m json.tool
检索指定ip主机
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "host.get","params": {"filter": {"host": ["10.2.33.100"]},"selectInterfaces": ["interfaceid","ip"]},"id": 1,"auth": "8ef873a332822d8acdf6b6f6e4fb2398"
}' http://192.168.1.1/zabbix/api_jsonrpc.php | python -m json.tool
3、使用api添加主机
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "host.create","params": {"host": "servername-192.168.1.2""interfaces": [{"type": 1,"main": 1,"useip": 1,"ip": "192.168.1.2","dns": "","port": "10050"}],"groups": [{"groupid": "2"}],"tags": [{"tag": "Host name","value": "servername-192.168.1.2"}],"templates": [{"templateid": "10001"}]},"id": 1,"auth": "8ef873a332822d8acdf6b6f6e4fb2398"
}' http://192.168.1.1/zabbix/api_jsonrpc.php | python -m json.tool
4、使用api删除主机
curl -s -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0","method": "host.delete","params": ["10065"],"id": 1,"auth": "8ef873a332822d8acdf6b6f6e4fb2398"
}' http://192.168.1.1/zabbix/api_jsonrpc.php | python -m json.tool