zabbix12 ---- API批量部署agent
zabbix12 ---- API批量部署agent
zabbix API
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/template/get
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/massadd
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/create
Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。它广泛用于:
- 创建新的应用程序以使用Zabbix;
- 将Zabbix与第三方软件集成;
- 自动执行常规任务。
Zabbix API是基于Web的API,作为Web前端的一部分提供。它使用JSON-RPC 2.0协议,这意味着两件事:
- 该API包含一组独立的方法;
- 客户端和API之间的请求和响应使用JSON格式进行编码。
curl 命令:
-s: 静默模式
-X:指定操作方法
-H:自定义header内容
-d:指定提交的http 内容
提供用户密码密码验证并发送post请求到zabbix server端,获取result返回值token
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1
}' http://192.168.10.81/zabbix/api_jsonrpc.php | python -m json.tool
会返回一个result,记录此值,此值会定时更新
{
"id": 1,
"jsonrpc": "2.0",
"result": "663bdb2475f82e71e885f5a2af5711d0"
}
使用返回的token从zabbix server获取所有主机列表信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "663bdb2475f82e71e885f5a2af5711d0",
"id": 1
}' http://192.168.10.81/zabbix/api_jsonrpc.php | python -m json.tool
以下为返回的部分主机信息
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084"
},
{
"host": "192.168.10.83",
"hostid": "10272"
},
{
"host": "192.168.10.84",
"hostid": "10273"
}
]
}
通过filter获取模板的指定信息
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "template.get",
"params": {
"output": "extend",
"filter": {
"host": [
"Template OS Linux"
]
}
},
"auth": "663bdb2475f82e71e885f5a2af5711d0",
"id": 1
}' http://192.168.10.81/zabbix/api_jsonrpc.php | python -m json.tool
返回模板相关信息
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"auto_compress": "1",
"available": "0",
"description": "",
"disable_until": "0",
"error": "",
"errors_from": "0",
"flags": "0",
"host": "Template OS Linux",
"ipmi_authtype": "-1",
"ipmi_available": "0",
"ipmi_disable_until": "0",
"ipmi_error": "",
"ipmi_errors_from": "0",
"ipmi_password": "",
"ipmi_privilege": "2",
"ipmi_username": "",
"jmx_available": "0",
"jmx_disable_until": "0",
"jmx_error": "",
"jmx_errors_from": "0",
"lastaccess": "0",
"maintenance_from": "0",
"maintenance_status": "0",
"maintenance_type": "0",
"maintenanceid": "0",
"name": "Template OS Linux",
"proxy_address": "",
"proxy_hostid": "0",
"snmp_available": "0",
"snmp_disable_until": "0",
"snmp_error": "",
"snmp_errors_from": "0",
"status": "3",
"templateid": "10001",
"tls_accept": "1",
"tls_connect": "1",
"tls_issuer": "",
"tls_psk": "",
"tls_psk_identity": "",
"tls_subject": ""
}
]
}
通过python脚本调用API或token
pip install requests
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import json
url = 'http://192.168.10.81/zabbix/api_jsonrpc.php'
post_data = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"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_token.py
aa5ab5e2a64fd52f85fbe0b1d7a26bd8
通过API添加主机
https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/create
获取组ID groupid
查看URL,groupid为2
http://192.168.10.81/zabbix/hostgroups.php?form=update&groupid=2
获取模板ID
查看URL,templateid为10275
http://192.168.10.81/zabbix/templates.php?form=update&templateid=10275&groupid=0
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.create", #定义方法
"params": {
"host": "192.168.10.83", #zabbix agent主机名称
"interfaces": [
{
"type": 1, #类型为1表示agent,2是SNMP,3是IMPI,4是JMX
"main": 1, #more接口
"useip": 1, #0是DNS,1是IP地址
"ip": "192.168.10.83", #添加的zabbix agent的IP地址
"dns": "",
"port": "10050" #agent端口
}
],
"groups": [
{
"groupid": "2" #添加到的组的ID
}
],
"templates": [
{
"templateid": "10275" #关联的模板的ID
}
]
},
"auth": "663bdb2475f82e71e885f5a2af5711d0", #token超时后需要重新获取
"id": 1
}' http://192.168.10.81/zabbix/api_jsonrpc.php | python -m json.tool
返回结果
id为1表示成功
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10289"
]
}
}
验证
自动注册
HostMetadataItem
https://www.zabbix.com/documentation/4.0/zh/manual/discovery/auto_registration
zabbix_agent.conf
#Hostname=192.168.10.84
HostnameItem=system.hostname
自动发现
zabbix_agent.conf
HostnameItem=system.hostname