API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
(1)查看zabbix监控系统的api接口
[root@server1 ~]# vim zabbix-api
curl -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' http://172.25.33.1/zabbix/api_jsonrpc.php | python -m json.tool
[root@server1 ~]# chmod +x zabbix-api
[root@server1 ~]# ./zabbix-api
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 229 100 68 100 161 560 1327 --:--:-- --:--:-- --:--:-- 1437
{
"id": 1,
"jsonrpc": "2.0",
"result": "1a02d0d7367ccef9a713e7c3a1ec97ed"
}
(2)查看主机信息
[root@server1 ~]# vim zabbix-api
curl -s -XPOST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host":[
"Zabbix server",
"server2",
"server3"
]
}
},
"auth": "1a02d0d7367ccef9a713e7c3a1ec97ed",
"id": 1
}' http://172.25.33.1/zabbix/api_jsonrpc.php | python -m json.tool
[root@server1 ~]# ./zabbix-api
(3)删除主机
[root@server1 ~]# vim zabbix-api
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10265" ##要删除主机的hostid,在监控界面上点击对应的主机,在url可以看到hostid
],
"auth": "5e8ad3bef69ff4695f58771dac1e016e",
"id": 1
}' http://172.25.33.1/zabbix/api_jsonrpc.php | python -m json.tool
[root@server1 ~]# ./zabbix-api
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10265"
]
}
}
(4)添加主机
[root@server1 ~]# vim zabbix-api
curl -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server2",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.33.2",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2" ##所要加入群组的id
}
],
"template": [
{
"templateid": "10001" ##所要链接
}
]
},
"auth": "5e8ad3bef69ff4695f58771dac1e016e",
"id": 1
}' http://172.25.33.1/zabbix/api_jsonrpc.php | python -m json.tool