zabbix的api
程序员文章站
2022-07-11 11:59:10
...
目录标题
1. zabbix—api
概览
Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。它广泛用于:
创建新的应用程序以使用Zabbix;
将Zabbix与第三方软件集成;
自动执行常规任务。
Zabbix API是基于Web的API,作为Web前端的一部分提供。它使用JSON-RPC 2.0协议,这意味着两件事:
该API包含一组独立的方法;
客户端和API之间的请求和响应使用JSON格式进行编码。
有关协议和JSON的更多信息可以在 JSON-RPC 2.0 规范 和 JSON 格式主页中找到。
- 获取身份令牌
[aaa@qq.com ~]# vim zabbix-api # 注意空格
curl -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.60.4/zabbix/api_jsonrpc.php | python -m json.tool
[aaa@qq.com ~]# chmod +x zabbix-api
[aaa@qq.com ~]# ./zabbix-api
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:--100 189 100 68 100 121 216 385 --:--:-- --:--:-- --:--:--100 189 100 68 100 121 216 385 --:--:-- --:--:-- --:--:-- 385
{
"id": 1,
"jsonrpc": "2.0",
"result": "19c5d70856be653409cb2b4d36292d62"
}
- 检索主机
[aaa@qq.com ~]# vim zabbix-api
curl -XPOST -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "19c5d70856be653409cb2b4d36292d62"
}' http://172.25.60.4/zabbix/api_jsonrpc.php | python -m json.tool
[aaa@qq.com ~]# ./zabbix-api
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:--100 600 100 309 100 291 3382 3185 --:--:-- --:--:-- --:--:-- 3395
{
"id": 2,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084",
"interfaces": [
{
"interfaceid": "1",
"ip": "127.0.0.1"
}
]
},
{
"host": "server2",
"hostid": "10306",
"interfaces": [
{
"interfaceid": "3",
"ip": "172.25.60.2"
}
]
},
{
"host": "server3",
"hostid": "10308",
"interfaces": [
{
"interfaceid": "5",
"ip": "172.25.60.3"
}
]
}
]
}
- 删除主机
[aaa@qq.com ~]# vim zabbix-api
curl -s -XPOST -H "Content-Type: application/json-rpc" -d ' # 请求zabbix的api
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10308"
],
"id": 2,
"auth": "19c5d70856be653409cb2b4d36292d62" # 令牌
}' http://172.25.60.4/zabbix/api_jsonrpc.php | python -m json.tool
关闭自动注册
[aaa@qq.com ~]# ./zabbix-api
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10308" # server3的hostid
]
}
}
主机中server3删除
- 创建主机
[aaa@qq.com ~]# 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.60.3",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"id": 2,
"auth": "19c5d70856be653409cb2b4d36292d62"
}' http://172.25.60.4/zabbix/api_jsonrpc.php | python -m json.tool
[aaa@qq.com ~]# ./zabbix-api
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10311"
]
}
}
上一篇: zabbix的Java API(一)
下一篇: 高并发的解决方案—负载均衡—LVS搭建