-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhostBulkInsert.py
executable file
·283 lines (221 loc) · 8.74 KB
/
hostBulkInsert.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
From a single csv file:
- create hostgroups
- create hosts and
- assign the correct interface
- assign them to their correct groups
- link them to the correct templates
- link them to the correct proxy
- add tags and user macros
Env var support:
# export ZABBIX_SERVER='https://your_zabbix_host/zabbix/'
# export ZABBIX_USERNAME='admin'
# export ZABBIX_PASSWORD='secretPassword'
# ./hostBulkInsert.py -f HOSTS.csv <- stops with error if hits an existing host
# ./hostBulkInsert.py -f HOSTS.csv -s <- check and skips existing hosts
Csv file sample (see README for more):
Hostname;IP Address;Groups;Tags;Description;Proxy;Templates;Interfaces;Macros;DNSName
SomeHost;;Group1,Group2,Routers;TagName=someValue;Device Description;ZabbixProxyName;Template Net Cisco IOS SNMPv2;agent-SNMP;{$SOMETHING}=25;someDNSNAME
SecondHost;8.8.8.8;Group3,Group2,SiteA;FirstTag=value,TagName=anotherValue;Device Description;;Template Net Cisco IOS SNMPv2;agent-SNMP;
"""
from zabbix.api import ZabbixAPI
import json
import csv
import argparse
import getpass
import getopt
import re
import sys
import os
# Class for argparse env variable support
class EnvDefault(argparse.Action):
# From https://stackoverflow.com/questions/10551117/
def __init__(self, envvar, required=True, default=None, **kwargs):
if not default and envvar:
if envvar in os.environ:
default = os.environ[envvar]
if required and default:
required = False
super(EnvDefault, self).__init__(default=default, required=required,
**kwargs)
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)
def jsonPrint(jsonUgly):
print(json.dumps(jsonUgly, indent=4, separators=(',', ': ')))
def ArgumentParser():
parser = argparse.ArgumentParser()
parser.add_argument('-Z',
required=True,
action=EnvDefault,
envvar='ZABBIX_SERVER',
help="Specify the zabbix server URL ie: http://yourserver/zabbix/ (ZABBIX_SERVER environment variable)",
metavar='zabbix-server-url')
parser.add_argument('-u',
required=True,
action=EnvDefault,
envvar='ZABBIX_USERNAME',
help="Specify the zabbix username (ZABBIX_USERNAME environment variable)",
metavar='Username')
parser.add_argument('-p',
required=True,
action=EnvDefault,
envvar='ZABBIX_PASSWORD',
help="Specify the zabbix username (ZABBIX_PASSWORD environment variable)",
metavar='Password')
parser.add_argument('-f',
required=True,
help="CSV filename to ingest",
metavar='csv-file-to-check')
parser.add_argument('-s', action='store_true',
help="Skip existing hosts")
return parser.parse_args()
def parseCSV(fileName, delimiter):
data = []
fileHandle = open(fileName)
reader = csv.DictReader(fileHandle, delimiter=delimiter)
for row in reader:
data.append(row)
return data
def main(argv):
print('-- Host Bulk Insert --')
# Parse arguments and build work variables
args = ArgumentParser()
zabbixURL = args.Z
zabbixUsername = args.u
zabbixPassword = args.p
csvFile = args.f
hostGroupNames = []
templateNames = []
proxyNames = []
hostGroupId = {}
templateId = {}
proxyId = {}
# Static values from zabbix api
interfaceType = {
'agent': 1,
'SNMP': 2,
'IMPI': 3,
'JMX': 4
}
print('Reading CSV file: {}'.format(csvFile))
hostData = parseCSV(csvFile, ';')
# jsonPrint(hostData)
# API Connect
print('Connecting to {}'.format(zabbixURL))
zapi = ZabbixAPI(url=zabbixURL, user=zabbixUsername,
password=zabbixPassword)
print('Parsing templates, proxy and hostgroups')
# Read all host groups and templates
for host in hostData:
for hostgroupName in host['Groups'].split(","):
hostGroupNames.append(hostgroupName)
for templateName in host['Templates'].split(","):
if templateName != 'DO_NOT_ADD':
templateNames.append(templateName)
if len(host['Proxy']) > 0:
proxyNames.append(host['Proxy'])
print('Templates: {}'.format(set(templateNames)))
print('Hostgroups: {}'.format(set(hostGroupNames)))
print('Proxyes: {}'.format(set(proxyNames)))
# Foreach UNIQUE hostgroup, create if missing
for hostgroupName in set(hostGroupNames):
if (zapi.get_id('hostgroup', item=hostgroupName, with_id=False, hostid=None) == None):
print('Creating missing hostgroup: {}'.format(hostgroupName))
zapi.hostgroup.create(name=hostgroupName)
# Create associative array Name=>Id
hostGroupId[hostgroupName] = zapi.get_id(
'hostgroup', item=hostgroupName, with_id=False, hostid=None)
# Foreach UNIQUE template
for templateName in set(templateNames):
# TODO : what if template does not exist ?
# Create associative array Name=>Id
templateId[templateName] = zapi.get_id(
'template', item=templateName, with_id=False, hostid=None)
# Foreach UNIQUE proxy
for proxyName in set(proxyNames):
# TODO : what if proxy does not exist ?
# Create associative array Name=>Id
f = {'host': proxyName}
proxyObj = zapi.proxy.get(filter=f)
proxyId[proxyName] = proxyObj[0]['proxyid']
# Create objects and add hosts
for host in hostData:
h_groups = []
h_templates = []
h_name = host['Hostname']
h_desc = host['Description']
h_ip = host['IP Address']
h_dns = host['DNSName']
h_interfaces = []
h_proxy = host['Proxy']
h_tags = []
h_macros = []
if (host['Interfaces'] == 'DO_NOT_ADD'):
print('Skipping host: {}'.format(h_name))
continue
if len(host['DNSName']) > 0:
h_useip = 0
else:
h_useip = 1
# Check and skip existing hosts
if args.s:
f = {'host': h_name}
hosts = zapi.host.get(
filter=f, output='extend', selectTags='extend')
if hosts:
print('Host {} already exists!! Skpping'.format(h_name))
continue
print('Working on: {}'.format(h_name))
# Build Tags object
if len(host['Tags']) > 0:
for tagString in host['Tags'].split(","):
[tagName, tagValue] = tagString.split("=")
h_tags.append({
'tag': tagName,
'value': tagValue
})
# jsonPrint(h_tags)
# Build Macro object
if len(host['Macros']) > 0:
for macroString in host['Macros'].split(","):
[macroName, macroValue] = macroString.split("=")
h_macros.append({
'macro': macroName,
'value': macroValue
})
# jsonPrint(h_macros)
# Build interface object
for interface in host['Interfaces'].split(","):
if (interfaceType[interface] == 1):
port = 10050
else:
port = 161
h_interfaces.append({
'type': interfaceType[interface],
'main': 1,
'useip': h_useip,
'ip': h_ip,
'dns': h_dns,
'port': port
})
# Build hostgroup object
for hostgroup in host['Groups'].split(","):
h_groups.append({
'groupid': hostGroupId[hostgroup],
})
# Build template object
for template in host['Templates'].split(","):
h_templates.append({
'templateid': templateId[template],
})
if len(h_proxy) > 0:
zapi_result = zapi.host.create(host=h_name, tags=h_tags, description=h_desc, interfaces=h_interfaces,
groups=h_groups, templates=h_templates, proxy_hostid=proxyId[h_proxy], macros=h_macros)
else:
zapi_result = zapi.host.create(
host=h_name, tags=h_tags, description=h_desc, interfaces=h_interfaces, groups=h_groups, templates=h_templates, macros=h_macros)
jsonPrint(zapi_result)
if __name__ == "__main__":
main(sys.argv[1:])