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)
相关推荐
婷儿z2 天前
部署 Zabbix 企业级分布式监控
分布式·zabbix
再看扣你眼5 天前
部署 Zabbix 企业级分布式监控笔记
笔记·分布式·zabbix
行止65 天前
部署 Zabbix 企业级分布式监控
分布式·zabbix
果子⌂7 天前
Zabbix 企业级高级应用
linux·运维·nginx·zabbix
归梧谣7 天前
部署Zabbix企业级分布式监控
分布式·zabbix
forestqq10 天前
zabbix平台无法删除已停用主机的处理案例
运维·服务器·zabbix
菜萝卜子11 天前
【Zabbix】Ansible批量部署ZabbixAgent
ansible·zabbix
东窗西篱梦14 天前
Zabbix 6.0 监控AWS全栈实战|EC2至Lambda的无缝监控
云计算·zabbix·aws
孙克旭_14 天前
day062-监控告警方式与Grafana优雅展示
linux·运维·zabbix·grafana
小周学学学15 天前
zabbix-agent静默安装
zabbix