- 编写脚本获取身份令牌
[root@server1 ~]# vi zabbix.php
[root@server1 ~]# vim zabbix-api
编辑内容如下:
curl -s XPOST -H "Content-Type:application/json-rpc" -d ' //启用curl命令模拟json格式的post请求
{
"jsonrpc": "2.0", //是标准的JSON RPC参数以标示协议版本。
"method": "user.login", //获取接口方法名
"params": {
"user": "Admin",
"password": "zabbix" ##zabbix系统监控的用户名和密码
},
"id": 1, //任意数
"auth": null //用户的身份令牌,因为还没有设置为null
}' http://172.25.32.1/zabbix/api_jsonrpc.php | python -m json.tool
使用post方法向该网址发送此JSON对象
api_jsonrpc.php是调用API的php脚本,可以安装可视化前端的目录下查到
给脚本添加可执行权限后执行
得到的result"e5d5108193dc19cc146e5734d359748f"为身份令牌
2.使用身份令牌检索主机
curl -s XPOST -H "Content-Type:application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.get", //使用该接口进行检索
"params": { //设置检索方法
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 1,
"auth":"e5d5108193dc19cc146e5734d359748f"
}' http://172.25.32.1/zabbix/api_jsonrpc.php | python -m json.tool
执行脚本获取已配置主机的hostname,hostid及ip等信息
3.利用获取到的hostid来对管理的机器进行操作。
server2的检索信息如下
server3的检索信息如下
删除server2
curl -s XPOST -H "Content-Type:application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10269" //server3的hostid
],
"id": 1,
"auth":"976dfec4133b974d7cbeb71b2f3c6352"
}' http://172.25.32.1/zabbix/api_jsonrpc.php | python -m json.tool
执行脚本返回值
在网页查看
删除server3
脚本
curl -s XPOST -H "Content-Type:application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10268" //server3的hostid
],
"id": 1,
"auth":"976dfec4133b974d7cbeb71b2f3c6352"
}' http://172.25.32.1/zabbix/api_jsonrpc.php | python -m json.tool
脚本返回值
网页查看结果
创建server3
脚本
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.32.3",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"id": 1,
"auth":"976dfec4133b974d7cbeb71b2f3c6352"
}' http://172.25.32.1/zabbix/api_jsonrpc.php | python -m json.tool
执行脚本返回值
网页查看添加成功
使用相同的方法添加server2