Zabbix API提供了强大的功能来通过编程方式管理监控系统。以下是使用Zabbix API增加监控项的详细步骤:
首先需要获取API认证令牌:
import requests
import json
# Zabbix服务器信息
zabbix_url = "http://your-zabbix-server/zabbix/api_jsonrpc.php"
zabbix_user = "Admin"
zabbix_password = "zabbix"
# 认证请求
auth_payload = {
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": zabbix_user,
"password": zabbix_password
},
"id": 1
}
response = requests.post(zabbix_url, json=auth_payload)
auth_token = response.json()["result"]
# 创建监控项
item_payload = {
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": "CPU utilization",
"key_": "system.cpu.util[,idle]",
"hostid": "10084", # 目标主机ID
"type": 0, # 0=Zabbix agent
"value_type": 0, # 0=numeric float
"interfaceid": "1", # 主机接口ID
"delay": "30s", # 更新间隔
"history": "90d", # 历史数据保留时间
"trends": "365d", # 趋势数据保留时间
"tags": [
{
"tag": "CPU",
"value": "utilization"
}
],
"preprocessing": [
{
"type": "9", # 自定义乘数
"params": "100"
}
]
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=item_payload)
print(response.json())
items = [
{
"name": "Free disk space on /",
"key_": "vfs.fs.size[/,free]",
"type": 0,
"value_type": 3, # 3=numeric unsigned (bytes)
"units": "B",
"delay": "1m"
},
{
"name": "Memory available",
"key_": "vm.memory.size[available]",
"type": 0,
"value_type": 3,
"units": "B",
"delay": "1m"
}
]
for item in items:
item_payload = {
"jsonrpc": "2.0",
"method": "item.create",
"params": {
**item,
"hostid": "10084",
"interfaceid": "1",
"history": "7d",
"trends": "30d"
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=item_payload)
print(response.json())
# 创建触发器
trigger_payload = {
"jsonrpc": "2.0",
"method": "trigger.create",
"params": {
"description": "High CPU usage on {HOST.NAME}",
"expression": "last(/Linux server/system.cpu.util[,idle])<10",
"priority": 3, # 3=average
"tags": [
{
"tag": "CPU",
"value": "high_usage"
}
],
"manual_close": 1 # 允许手动关闭问题
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=trigger_payload)
print(response.json())
# 创建模板
template_payload = {
"jsonrpc": "2.0",
"method": "template.create",
"params": {
"host": "Template Custom Linux",
"groups": [{"groupid": "1"}], # Linux servers组
"items": [
{
"name": "CPU utilization",
"key_": "system.cpu.util[,idle]",
"type": 0,
"value_type": 0,
"delay": "30s"
}
],
"triggers": [
{
"description": "High CPU usage on {HOST.NAME}",
"expression": "last(/{HOST.HOST}/system.cpu.util[,idle])<10",
"priority": 3
}
]
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=template_payload)
print(response.json())
# 链接模板到主机
link_payload = {
"jsonrpc": "2.0",
"method": "template.massadd",
"params": {
"hosts": [{"hostid": "10084"}], # 目标主机ID
"templates": [{"templateid": "10001"}] # 模板ID
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=link_payload)
print(response.json())
# 创建自动发现规则
lld_payload = {
"jsonrpc": "2.0",
"method": "discoveryrule.create",
"params": {
"name": "Filesystem discovery",
"key_": "vfs.fs.discovery",
"hostid": "10084",
"type": 0,
"interfaceid": "1",
"delay": "1h",
"lifetime": "30d",
"item_prototypes": [
{
"name": "Free disk space on {#FSNAME}",
"key_": "vfs.fs.size[{#FSNAME},free]",
"type": 0,
"value_type": 3,
"units": "B",
"delay": "5m",
"applications": ["5"] # Filesystems应用组
}
],
"trigger_prototypes": [
{
"description": "Low disk space on {#FSNAME} ({ITEM.LASTVALUE})",
"expression": "last(/{HOST.HOST}/vfs.fs.size[{#FSNAME},free])<1G",
"priority": 3
}
]
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=lld_payload)
print(response.json())
# 创建Web场景
web_payload = {
"jsonrpc": "2.0",
"method": "httptest.create",
"params": {
"name": "Website Homepage Check",
"hostid": "10084",
"steps": [
{
"name": "Check homepage",
"url": "http://example.com",
"status_codes": "200",
"no": 1
}
],
"delay": "1m",
"retries": 2,
"agent": "Zabbix"
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=web_payload)
print(response.json())
# 验证监控项是否创建成功
verify_payload = {
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"output": "extend",
"hostids": "10084",
"search": {
"name": "CPU utilization"
}
},
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=verify_payload)
print("Existing items:", response.json())
# 注销
logout_payload = {
"jsonrpc": "2.0",
"method": "user.logout",
"params": [],
"auth": auth_token,
"id": 1
}
response = requests.post(zabbix_url, json=logout_payload)
print(response.json())
通过Zabbix API,您可以实现监控配置的自动化、批量操作以及与现有IT系统的集成。