-
Notifications
You must be signed in to change notification settings - Fork 3
/
create-snmp-hosts-from-csv.py
executable file
·138 lines (122 loc) · 5.2 KB
/
create-snmp-hosts-from-csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# import hosts from txt file to Zabbix via API
# and assign template and host group to them
# python3 required
# dnf install python3-pip
# pip3 install zabbix_api
# script is tested and works with module 'zabbix_api'
# which is not the same as 'pyzabbix' !
import csv
from zabbix_api import ZabbixAPI
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import sys
sys.path.insert(0,'/var/lib/zabbix')
# pip install config
import config
ZABBIX_SERVER = config.url
zapi = ZabbixAPI(ZABBIX_SERVER)
zapi.session.verify=False
zapi.login(config.username, config.password)
file = open("hostlist.csv",'rt')
reader = csv.DictReader( file )
# take the file and read line by line
for line in reader:
# check if this host exists in zabbix
if not zapi.host.get({"filter":{"host" :line['name']}}):
print (line['name'],"not yet registred")
if zapi.proxy.get({"output": "proxyid","selectInterface": "extend","filter":{"host":line['proxy']}}):
proxy_id=zapi.proxy.get({"output": "proxyid","selectInterface": "extend","filter":{"host":line['proxy']}})[0]['proxyid']
print (line['proxy'])
if int(proxy_id)>0:
templates=line['template'].split(";")
groups=line['group'].split(";")
# take first group from group array
group_id = zapi.hostgroup.get({"filter" : {"name" : groups[0]}})[0]['groupid']
# take first template from template array
template_id = zapi.template.get({"filter" : {"name" : templates[0]}})[0]['templateid']
# crete a host an put hostid instantly in the 'hostid' variable
hostid = zapi.host.create ({
"host":line['name'],"interfaces":[{"type":2,"dns":"","main":1,"ip": line['address'],"port": 161,"useip": 1,"details":{"version":3,"bulk":0,"securityname":"mysecurityname","contextname":"","securitylevel":1}}],
"groups": [{ "groupid": group_id }],
"proxy_hostid":proxy_id,
"templates": [{ "templateid": template_id }]})['hostids']
# add additional templates
if len(templates)>1:
# skip the first element in array
for one_template in templates[1:]:
try:
tid=zapi.template.get({"filter" : {"name" : one_template}})[0]['templateid']
if tid:
# link new template
try:
nt=zapi.template.massadd({"templates":tid,"hosts":hostid})
except Exception as nt:
print ("template",one_template,"probably already linked")
else:
print ("Temnplate:",one_template,"does not exist")
except Exception as tid:
print("Temnplate:",one_template,"does not exist")
# add additional groups
if len(groups)>1:
for one_hostgroup in groups[1:]:
try:
gid=zapi.hostgroup.get({"filter" : {"name" : one_hostgroup}})[0]['groupid']
if gid:
# link new hostgroup
try:
nhg=zapi.hostgroup.massadd({"groups":gid,"hosts":hostid})
except Exception as nhg:
print ("Hostgroup",one_hostgroup,"probably already linked")
else:
print ("Hostgroup",one_hostgroup,"does not exist")
except Exception as gid:
print ("Hostgroup",one_hostgroup,"does not exist")
# if there are no proxy
else:
print ("proxy does not exist. creating with none")
templates=line['template'].split(";")
groups=line['group'].split(";")
# take first group from group array
group_id = zapi.hostgroup.get({"filter" : {"name" : groups[0]}})[0]['groupid']
# take first template from template array
template_id = zapi.template.get({"filter" : {"name" : templates[0]}})[0]['templateid']
# crete a host an put hostid instantly in the 'hostid' variable
hostid = zapi.host.create ({
"host":line['name'],"interfaces":[{"type":2,"dns":"","main":1,"ip": line['address'],"port": 161,"useip": 1,"details":{"version":3,"bulk":0,"securityname":"mysecurityname","contextname":"","securitylevel":1}}],
"groups": [{ "groupid": group_id }],
"templates": [{ "templateid": template_id }]})['hostids']
# add additional templates
if len(templates)>1:
# skip the first element in array
for one_template in templates[1:]:
try:
tid=zapi.template.get({"filter" : {"name" : one_template}})[0]['templateid']
if tid:
# link new template
try:
nt=zapi.template.massadd({"templates":tid,"hosts":hostid})
except Exception as nt:
print ("template",one_template,"probably already linked")
else:
print ("Temnplate:",one_template,"does not exist")
except Exception as tid:
print("Temnplate:",one_template,"does not exist")
# add additional groups
if len(groups)>1:
for one_hostgroup in groups[1:]:
try:
gid=zapi.hostgroup.get({"filter" : {"name" : one_hostgroup}})[0]['groupid']
if gid:
# link new hostgroup
try:
nhg=zapi.hostgroup.massadd({"groups":gid,"hosts":hostid})
except Exception as nhg:
print ("Hostgroup",one_hostgroup,"probably already linked")
else:
print ("Hostgroup",one_hostgroup,"does not exist")
except Exception as gid:
print ("Hostgroup",one_hostgroup,"does not exist")
else:
print (line['name'],"already exist")
file.close()