Skip to content

Commit 0aba01e

Browse files
authored
Merge pull request #99 from colttt/patch-1
a working example for zabbix 3.4 fixes #96
2 parents a251ae5 + fc56184 commit 0aba01e

File tree

1 file changed

+64
-41
lines changed

1 file changed

+64
-41
lines changed

examples/import_templates.py

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,102 @@
1-
"""
1+
"""
22
Import Zabbix XML templates
3-
"""
4-
3+
"""
4+
55
from pyzabbix import ZabbixAPI, ZabbixAPIException
6-
import glob
6+
import os
7+
import sys
8+
9+
if len(sys.argv) <= 1:
10+
print('Please provide directory with templates as first ARG or the XML file with template.')
11+
exit(1)
12+
13+
path = sys.argv[1]
714

815
# The hostname at which the Zabbix web interface is available
9-
ZABBIX_SERVER = 'https://zabbix.example.com'
16+
ZABBIX_SERVER = 'https://zabbix.example.org'
1017

1118
zapi = ZabbixAPI(ZABBIX_SERVER)
1219

1320
# Login to the Zabbix API
14-
zapi.login("admin", "zabbix")
21+
#zapi.session.verify = False
22+
zapi.login("Admin", "zabbix")
1523

1624
rules = {
1725
'applications': {
18-
'createMissing': 'true',
19-
'updateExisting': 'true'
26+
'createMissing': True,
2027
},
2128
'discoveryRules': {
22-
'createMissing': 'true',
23-
'updateExisting': 'true'
29+
'createMissing': True,
30+
'updateExisting': True
2431
},
2532
'graphs': {
26-
'createMissing': 'true',
27-
'updateExisting': 'true'
33+
'createMissing': True,
34+
'updateExisting': True
2835
},
2936
'groups': {
30-
'createMissing': 'true'
37+
'createMissing': True
3138
},
3239
'hosts': {
33-
'createMissing': 'true',
34-
'updateExisting': 'true'
40+
'createMissing': True,
41+
'updateExisting': True
3542
},
3643
'images': {
37-
'createMissing': 'true',
38-
'updateExisting': 'true'
44+
'createMissing': True,
45+
'updateExisting': True
3946
},
4047
'items': {
41-
'createMissing': 'true',
42-
'updateExisting': 'true'
48+
'createMissing': True,
49+
'updateExisting': True
4350
},
4451
'maps': {
45-
'createMissing': 'true',
46-
'updateExisting': 'true'
52+
'createMissing': True,
53+
'updateExisting': True
4754
},
4855
'screens': {
49-
'createMissing': 'true',
50-
'updateExisting': 'true'
56+
'createMissing': True,
57+
'updateExisting': True
5158
},
5259
'templateLinkage': {
53-
'createMissing': 'true',
54-
'updateExisting': 'true'
60+
'createMissing': True,
5561
},
5662
'templates': {
57-
'createMissing': 'true',
58-
'updateExisting': 'true'
63+
'createMissing': True,
64+
'updateExisting': True
5965
},
6066
'templateScreens': {
61-
'createMissing': 'true',
62-
'updateExisting': 'true'
67+
'createMissing': True,
68+
'updateExisting': True
6369
},
6470
'triggers': {
65-
'createMissing': 'true',
66-
'updateExisting': 'true'
71+
'createMissing': True,
72+
'updateExisting': True
73+
},
74+
'valueMaps': {
75+
'createMissing': True,
76+
'updateExisting': True
6777
},
6878
}
6979

70-
path = './templates/*.xml'
71-
files = glob.glob(path)
72-
73-
for file in files:
74-
with open(file, 'r') as f:
75-
template = f.read()
76-
try:
77-
zapi.confimport('xml', template, rules)
78-
except ZabbixAPIException as e:
79-
print(e)
80+
if os.path.isdir(path):
81+
#path = path/*.xml
82+
files = glob.glob(path+'/*.xml')
83+
for file in files:
84+
print(file)
85+
with open(file, 'r') as f:
86+
template = f.read()
87+
try:
88+
zapi.confimport('xml', template, rules)
89+
except ZabbixAPIException as e:
90+
print(e)
91+
print('')
92+
elif os.path.isfile(path):
93+
files = glob.glob(path)
94+
for file in files:
95+
with open(file, 'r') as f:
96+
template = f.read()
97+
try:
98+
zapi.confimport('xml', template, rules)
99+
except ZabbixAPIException as e:
100+
print(e)
101+
else:
102+
print('I need a xml file')

0 commit comments

Comments
 (0)