插件窝 干货文章 如何利用zabbix api 来增加监控

如何利用zabbix api 来增加监控

Zabbix response payload 创建 830    来源:    2025-05-06

利用Zabbix API增加监控项

Zabbix API提供了强大的功能来通过编程方式管理监控系统。以下是使用Zabbix API增加监控项的详细步骤:

1. 准备工作

认证与获取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"]

2. 创建监控项

基本监控项创建

# 创建监控项
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())

3. 创建触发器

# 创建触发器
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())

4. 创建监控模板

# 创建模板
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())

5. 将模板链接到主机

# 链接模板到主机
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())

6. 创建自动发现规则

# 创建自动发现规则
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())

7. 创建Web场景监控

# 创建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())

8. 错误处理与验证

# 验证监控项是否创建成功
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())

9. 注销API会话

# 注销
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())

最佳实践

  1. 使用模板:尽可能创建模板而不是直接在主机上创建监控项
  2. 批量操作:对于大量监控项,考虑使用批量创建方法
  3. 错误处理:始终检查API响应中的错误信息
  4. 版本兼容性:注意Zabbix不同版本API的差异
  5. 权限管理:使用具有最小必要权限的API用户

通过Zabbix API,您可以实现监控配置的自动化、批量操作以及与现有IT系统的集成。