-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpyzabbix_create_host.py
More file actions
71 lines (48 loc) · 1.42 KB
/
pyzabbix_create_host.py
File metadata and controls
71 lines (48 loc) · 1.42 KB
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
from pyzabbix import ZabbixAPI
import os
zapi = ZabbixAPI('http://127.0.0.1') ## Ip address of zabbix server
zapi.session.verify = False
api_auth_token = zapi.login('Admin', 'zabbixpass') ## Admin is sample username of zabbix server, zabbixpass is the sample password.
zapi.user.get(userids=-1)
csvfile = open('list.csv', 'r') ## Here you may change name of CSV file
def get_hostgroup_id(hostgroup):
data = zapi.hostgroup.get(filter={'name': hostgroup})
if data != []:
hostgroupid = data[0]['groupid']
else:
raise Exception('Could not find hostgroupID for: ' + hostgroup)
return str(hostgroupid)
def add_host(ip, hostname, hostgroups):
interface_list = [{
"type": 1,
"main": 1,
"useip": 1,
"ip": str(ip),
"dns": '',
"port": "10050"
}]
groups = list()
for g in hostgroups:
groups.append({"groupid": str(get_hostgroup_id(g))})
query = {
'host': str(hostname),
'groups': groups,
'proxy_hostid': '0',
'status': '0',
'interfaces': interface_list,
'inventory_mode': 1,
'inventory': {
'name': str(hostname)
}
}
result = zapi.host.create(**query)
for l in csvfile.readlines():
if l.strip().startswith('#') or not ',' in l:
continue
myargs = l.strip().split(',')
try:
add_host(myargs[0], myargs[1], myargs[2].split(':'))
print("Added %s" %myargs[0])
except:
print("Failed to add %s" %myargs[0])
pass