zabbix批量添加交换机脚本

原创作者:运维工程师 谢晋

zabbix批量添加交换机脚本

复制代码
#!/usr/bin/env python
import csv
from pyzabbix import ZabbixAPI

# Zabbix API credentials
ZABBIX_SERVER = 'http://10.11.100.41/zabbix'    # 修改为zabbixIP
ZABBIX_USER = 'Admin'                           # 账号
ZABBIX_PASSWORD = 'zabbix'                      # 密码

# Zabbix template and host group names
TEMPLATE_NAME = 'Template Net HP Comware HH3C SNMP'   # 板名称
HOST_GROUP_NAME = 'Switches'                    # 群主名称

# SNMP credentials
SNMP_COMMUNITY = 'jxpr'                         # 团体名称

# Read CSV file with switch names and IP addresses
with open('switches.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        switch_name = row[0]
        switch_ip = row[1]

        # Connect to Zabbix API
        zapi = ZabbixAPI(ZABBIX_SERVER)
        zapi.login(ZABBIX_USER, ZABBIX_PASSWORD)

        # Get template ID
        template_id = zapi.template.get(filter={'name': TEMPLATE_NAME})[0]['templateid']

        # Get host group ID
        hostgroup_id = zapi.hostgroup.get(filter={'name': HOST_GROUP_NAME})[0]['groupid']

        # Create host
        host_create = zapi.host.create(
            host=switch_name,
            interfaces=[{
                "type": 2,
                "main": 1,
                "useip": 1,
                "ip": switch_ip,
                "dns": "",
                "port": 161,  # SNMP port
                "details": {
                    "version": 2,  # SNMP version
                    "community": SNMP_COMMUNITY  # SNMP community string
                }
            }],
            groups=[{
                "groupid": hostgroup_id
            }],
            templates=[{
                "templateid": template_id
            }]
        )

        print("Switch added: %s" % switch_name)
相关推荐
蜀道山老天师2 天前
Docker 实战教程:从基础流程到云桌面、Zabbix 监控、Portainer 可视化部署
运维·docker·云原生·容器·zabbix
万山寒2 天前
zabbix设置中文
zabbix
securitor3 天前
zabbix-agent 安装
zabbix
梁正雄4 天前
zabbix安装-7.4
zabbix·监控·zabbix安装·zabbix7.4·zabbix7.4最新安装包
m0_736034854 天前
zabbix
zabbix
炸炸鱼.6 天前
部署Zabbix企业级分布式监控:从零到实战(知识点大全)
分布式·zabbix
Peace6 天前
【Zabbix】
linux·运维·zabbix
爱莉希雅&&&6 天前
zabbix快速搭建和使用
android·linux·数据库·zabbix·监控
江华森6 天前
Zabbix 6.4 全栈部署与运维完全指南
运维·zabbix
江南风月7 天前
WGCLOUD监控系统的Restful Http接口一览
运维·zabbix·运维开发·prometheus