原创作者:运维工程师 谢晋
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)