Zabbix api
对于以上两种方式,有些人都不选,倾向于使用第三种:使用zabbix api加上这个监控
在把这台机器删除了,然后discovery和自动注册的都关闭了
再换一种方式把它加进去,zabbix api两个作用
1、管理zabbix
2、查询一些数,把一些zabbix报警的图形一块发邮件
它支持很多方法
api的配置,在httpd.conf目录下。如下编辑zabbix.conf
[root@linux-node1 ~]# vim /etc/httpd/conf.d/zabbix.conf
[root@linux-node1 zabbix]# cd /usr/share/zabbix/ [root@linux-node1 zabbix]# ls api_jsonrpc.php api_jsonrpc.php [root@linux-node1 zabbix]# ls api_jsonrpc.php -l -rw-r--r-- 1 root root 2342 Apr 20 2016 api_jsonrpc.php [root@linux-node1 zabbix]#
可以看到,首先它是个api,传输模式是rpc的,传输格式是json的
既然是api,那肯定有验证,给api发用户名密码,得返回token吧
后面拿这个token就可以继续请求了,和openstack类似
打开文档看
https://www.zabbix.com/documentation/2.4/manual/api/reference
第一步,因为涉及登录,找用户相关的
Authenticate a user.
Request:
{ "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 }
Response:
{ "jsonrpc": "2.0", "result": "0424bd59b807674191e7d77572075f33", "id": 1 }
通过curl访问
-s 静默
-X POST
-H 接类型
-d 接内容,用单引号引起来
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 }' http://10.0.1.161/zabbix/api_jsonrpc.php {"jsonrpc":"2.0","result":"5733cec620a75d1f7e2ebcf725c7ccec","id":1}[root@linux-node2 ~]# [root@linux-node2 ~]#
看着不舒服,给它处理下,调用python的工具处理下,命令如下
curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool
执行如下
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' > > { > "jsonrpc": "2.0", > "method": "user.login", > "params": { > "user": "Admin", > "password": "zabbix" > }, > "id": 1 > }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool { "id": 1, "jsonrpc": "2.0", "result": "d05f9fa13c8b182d61e462b8aaded670" } [root@linux-node2 ~]#
上面为什么要设置id,因为可能又多个请求,给不同的会话设置不同的id
你才知道这个是你的
把这个字符串记录下来
d05f9fa13c8b182d61e462b8aaded670
你请求的时候还可以加个下面userdata为true,这样还能返回用户信息
命令如下
curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix", "userData": true }, "id": 1 }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool
执行如下
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' > { > "jsonrpc": "2.0", > "method": "user.login", > "params": { > "user": "Admin", > "password": "zabbix", > "userData": true > }, > "id": 1 > }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool { "id": 1, "jsonrpc": "2.0", "result": { "alias": "Admin", "attempt_clock": "0", "attempt_failed": "0", "attempt_ip": "", "autologin": "1", "autologout": "0", "debug_mode": false, "gui_access": "0", "lang": "en_GB", "name": "Zabbix", "refresh": "30", "rows_per_page": "50", "sessionid": "dbfdc9c932d15760f0ed05bb5955511b", "surname": "Administrator", "theme": "default", "type": "3", "url": "", "userid": "1", "userip": "10.0.1.162" } } [root@linux-node2 ~]#
https://www.zabbix.com/documentation/2.4/manual/api/reference/user/get
找一个简单的,只获取hostid,注意auth是sessionid
curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["hostid"] }, "auth": "dbfdc9c932d15760f0ed05bb5955511b", "id": 2 } ' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool
执行结果如下,hostid是10084
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' > { > "jsonrpc": "2.0", > "method": "host.get", > "params": { > "output": ["hostid"] > }, > "auth": "dbfdc9c932d15760f0ed05bb5955511b", > "id": 2 > } ' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool { "id": 2, "jsonrpc": "2.0", "result": [ { "hostid": "10084" } ] } [root@linux-node2 ~]#
hostid就是下面这里的
curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.get", "params": { "output": ["host"] }, "auth": "dbfdc9c932d15760f0ed05bb5955511b", "id": 2 } ' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool
执行结果如下
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' > { > "jsonrpc": "2.0", > "method": "host.get", > "params": { > "output": ["host"] > }, > "auth": "dbfdc9c932d15760f0ed05bb5955511b", > "id": 2 > } ' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool { "id": 2, "jsonrpc": "2.0", "result": [ { "host": "Zabbix server", "hostid": "10084" } ] } [root@linux-node2 ~]#
接下来使用api方式添加被监控主机
https://www.zabbix.com/documentation/2.4/manual/api/reference/host/create
创建的时候可以指定模板,以及主机组
在官网里看到实例如下,添加主机的时候可以指定模板,主机组,资产
{ "jsonrpc": "2.0", "method": "host.create", "params": { "host": "Linux server", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "192.168.3.1", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "50" } ], "templates": [ { "templateid": "20045" } ], "inventory_mode": 0, "inventory": { "macaddress_a": "01234", "macaddress_b": "56768" } }, "auth": "038e1d7b1735c6a5436ee9eae095879e", "id": 1 }
查看linux servers这个主机组,它的组id是2
先获取授权的sessionid
curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool
根据返回的sessionid来创建主机
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "user.login", "params": { "user": "Admin", "password": "zabbix" }, "id": 1 }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool { "id": 1, "jsonrpc": "2.0", "result": "4c0999123f2da1b09b6a079c02a363ad" } [root@linux-node2 ~]#
执行命令如下
curl -s -X POST -H 'Content-Type:application/json' -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "Linux server", "interfaces": [ { "type": 1, "main": 1, "useip": 1, "ip": "10.0.1.162", "dns": "", "port": "10050" } ], "groups": [ { "groupid": "2" } ], "templates": [ { "templateid": "10001" } ] }, "id": 1, "auth": "4c0999123f2da1b09b6a079c02a363ad" }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool
执行结果如下
[root@linux-node2 ~]# curl -s -X POST -H 'Content-Type:application/json' -d ' > { > "jsonrpc": "2.0", > "method": "host.create", > "params": { > "host": "Linux server", > "interfaces": [ > { > "type": 1, > "main": 1, > "useip": 1, > "ip": "10.0.1.162", > "dns": "", > "port": "10050" > } > ], > "groups": [ > { > "groupid": "2" > } > ], > "templates": [ > { > "templateid": "10001" > } > ] > }, > "id": 1, > "auth": "4c0999123f2da1b09b6a079c02a363ad" > }' http://10.0.1.161/zabbix/api_jsonrpc.php |python -mjson.tool { "id": 1, "jsonrpc": "2.0", "result": { "hostids": [ "10116" ] } } [root@linux-node2 ~]#
创建成功
点开看看
模板也自动加上了