- 查看zabbix监控系统的api接口
1.编写脚本,查看zabbix监控系统的api接口
[root@server1 ~]# vim zabbix-api
curl -s -XPOST -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' http://172.25.21.1/zabbix/api_jsonrpc.php | python -m json.tool
2.给脚本添加执行权限,执行脚本
[root@server1 ~]# chmod +x zabbix-api
[root@server1 ~]# ./zabbix-api
{
"id": 1,
"jsonrpc": "2.0",
"result": "e89ea181aaa952b532e7bb3f46936322"
}
- 查看zabbix主机和被监控主机的信息
1.编写脚本
[root@server1 ~]# vim zabbix-api
curl -s -XPOST -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "e89ea181aaa952b532e7bb3f46936322"
}' http://172.25.21.1/zabbix/api_jsonrpc.php | python -m json.tool
2.执行脚本得到被监控主机的信息
[root@server1 ~]# ./zabbix-api
{
"id": 2,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084",
"interfaces": [
{
"interfaceid": "1",
"ip": "127.0.0.1"
}
]
},
{
"host": "server2",
"hostid": "10265",
"interfaces": [
{
"interfaceid": "4",
"ip": "172.25.21.2"
}
]
},
{
"host": "server3",
"hostid": "10266",
"interfaces": [
{
"interfaceid": "5",
"ip": "172.25.21.3"
}
]
}
]
}
- 删除zabbix监控的主机
1.浏览器可以查看到被监控的主机有三个
2.编写脚本,删除被监控主机server3
[root@server1 ~]# vim zabbix-api
curl -s -XPOST -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10266" ##删除的主机 id
],
"id": 2,
"auth": "e89ea181aaa952b532e7bb3f46936322"
}' http://172.25.21.1/zabbix/api_jsonrpc.php | python -m json.tool
3.执行脚本
[root@server1 ~]# ./zabbix-api
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10266"
]
}
}
4.浏览器查看被监控主机server3已经删除了
- 添加zabbix监控的主机
1.编写脚本,添加被监控主机server3
[root@server1 ~]# vim zabbix-api
curl -s -XPOST -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server3", ##添加的主机名
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "172.25.21.3", ##添加的主机ip
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2" ##添加的主机加入的群组id
}
],
"templates": [
{
"templateid": "10001" ##添加的主机使用的模版id
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"id": 2,
"auth": "e89ea181aaa952b532e7bb3f46936322"
}' http://172.25.21.1/zabbix/api_jsonrpc.php | python -m json.tool
补充:如何获得groupid和templateid
可以在zabbix的界面打开群组和模板,在上边的地址栏即可获得
2.执行脚本
[root@server1 ~]# ./zabbix-api
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10268"
]
}
}
3.浏览器查看,成功添加被监控主机server3
来源:https://blog.csdn.net/qq_44236589/article/details/90183561