API使用方法
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin", #指定zabbix server中的用户名
"password": "linux" #指定用户对应的密码
},
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
#要想通过api访问数据,需要登录,获取到token;
-s静默模式;
-X指定方法;
-H自定义报文头部信息,Content-Type:application/json指定内容为json格式;
-d指定需要上传的内容
把返回的数据通过管道传递给python,python指定json模块进行序列化;
此命令的作用是从zabbix server上获取到token
{
"jsonrpc": "2.0",
"result": "6cc24877264e7910f9ad6aed3be7da93",
"id": 1
}
#此为返回的数据
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"filter": {
"host": [
"192.168.3.202",
"192.168.3.204"
]
}
},
"auth": "6cc24877264e7910f9ad6aed3be7da93", #指定之前获取到的token
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
#获取指定的两台主机的信息
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "6cc24877264e7910f9ad6aed3be7da93",
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
#查看所有主机的hostid及hostname
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "user.get",
"params": {
"output": "extend"
},
"auth": "6cc24877264e7910f9ad6aed3be7da93",
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
#查看所有用户信息
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"template-nginx"
]
}
},
"auth": "6cc24877264e7910f9ad6aed3be7da93",
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
#查看单个模板信息
基于脚本获取token
#!/usr/bin/python
# -*- coding:utf-8 -*-
import requests
import json
url = ‘http://192.168.3.200/zabbix/api_jsonrpc.php‘
post_data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "linux"
},
"id": 1
}
post_header = {‘Content-Type‘: ‘application/json‘}
ret = requests.post(url, data=json.dumps(post_data), headers=post_header)
zabbix_ret = json.loads(ret.text)
if not zabbix_ret.has_key(‘result‘):
print ‘login error‘
else:
print zabbix_ret.get(‘result‘)
通过zabbix server的API接口创建主机
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "host.create", #定义方法为创建主机
"params": {
"host": "192.168.3.205", #此名称需要与agent配置文件中的hostname一致
"name": "192.168.3.205-test", #指定主机的可见名称
"proxy_hostid": "10275", #如果想要添加proxy,则指定proxy的ID
"interfaces": [
{
"type": 1, #类型为1表示agent,2是SNMP,3是IMPI,4是JMX
"main": 1, #主要接口
"useip": 1, #0是使用DNS,1是使用IP地址
"ip": "192.168.3.205", #添加的zabbix agent的IP地址
"dns": "",
"port": "10050" #agent端口
}
],
"groups": [
{
"groupid": "15" #添加到的组的ID
}
],
"templates": [
{
"templateid": "10272" #关联的模板的ID;如果指定的ID的模板,也链接了别的模板,则新
} 创建的主机也相当于关联了多个模板;如果一个模板链接了多个模
] 板,则模板ID指向最初的基础模板ID即可
},
"auth": "9c924e27deadda87158510d801cb1e88",
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
通过脚本批量创建主机
vim api_install_agent.sh
#!/bin/bash
IPS="
192.168.3.150
192.168.3.151
192.168.3.152
192.168.3.153
192.168.3.154
192.168.3.155
192.168.3.156
"
for node_ip in ${IPS};do
curl -s -X POST -H ‘Content-Type:application/json‘ -d ‘
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "‘$node_ip‘",
"name": "‘$node_ip‘-test",
"proxy_hostid": "10275",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "‘$node_ip‘",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "15"
}
],
"templates": [
{
"templateid": "10272"
}
]
},
"auth": "b48e45edb838d75af31f50b1a989a283",
"id": 1
}‘ http://192.168.3.200/zabbix/api_jsonrpc.php | python3 -m json.tool
done
zabbix-通过API部署主机