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)
相关推荐
姚永强4 天前
zabbix安装教学
笔记·zabbix
姚永强5 天前
zabbix实际运用(有图片教学)
zabbix
姚永强6 天前
zabbix排查思路
运维·zabbix
wzq11_6667 天前
ansible部署项目——基于tidb数据库安装zabbix_server
ansible·zabbix·tidb
B1538939 天前
Day 21-Ansible 进阶与 Zabbix TiDB 迁移学习记录
ansible·zabbix·tidb
蜀道山老天师11 天前
Zabbix监控MySQL与Redis应用实践完整指南
linux·运维·redis·mysql·zabbix
NJCloud11 天前
Ansible(三)——Zabbix 监控系统部署与敏感信息加密
linux·运维·chrome·信息可视化·ansible·zabbix
小张同学a.11 天前
练习:用 playbook 部署 zabbix 和 tidb
ansible·zabbix·tidb
蜀道山老天师12 天前
Zabbix监控Apache与Nginx应用实践完整指南
linux·运维·nginx·apache·zabbix
叮咚侠12 天前
Ubuntu 24.04 安装zabbix-agent 5.0 服务
linux·ubuntu·zabbix·zabbix-agent