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)
相关推荐
honsor8 小时前
双网口 + RS485 机柜温湿度传感器,Modbus RTU/TCP 如何接入 Zabbix
网络协议·tcp/ip·zabbix
运维自动化&云计算4 天前
zabbix 7.4.15升级php8.5.12
zabbix
梅菲塔亚4 天前
Zabbix企业级监控系统搭建实战
zabbix
月落汀兰5 天前
服务器出问题没人告警?Zabbix6.0 LNMP 部署,MySQL 监控 + 邮件告警保姆级实战
运维·服务器·zabbix
王琦03188 天前
Zabbix部署
zabbix
王琦03189 天前
zabbix agent 部署与使用
zabbix
Lysander.Jovian10 天前
zabbix监控实战
zabbix
fengkai454511 天前
十三、Zabbix 监控
运维·服务器·zabbix·监控·网络设备
陈聪.13 天前
Zabbix 监控系统知识总结
zabbix
暖核14 天前
企业级开源监控解决方案:Zabbix 6.0 部署与配置全流程详解
开源·zabbix