|
1 |
| -""" |
| 1 | +""" |
2 | 2 | Import Zabbix XML templates
|
3 |
| -""" |
4 |
| - |
| 3 | +""" |
| 4 | + |
5 | 5 | 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] |
7 | 14 |
|
8 | 15 | # The hostname at which the Zabbix web interface is available
|
9 |
| -ZABBIX_SERVER = 'https://zabbix.example.com' |
| 16 | +ZABBIX_SERVER = 'https://zabbix.example.org' |
10 | 17 |
|
11 | 18 | zapi = ZabbixAPI(ZABBIX_SERVER)
|
12 | 19 |
|
13 | 20 | # Login to the Zabbix API
|
14 |
| -zapi.login("admin", "zabbix") |
| 21 | +#zapi.session.verify = False |
| 22 | +zapi.login("Admin", "zabbix") |
15 | 23 |
|
16 | 24 | rules = {
|
17 | 25 | 'applications': {
|
18 |
| - 'createMissing': 'true', |
19 |
| - 'updateExisting': 'true' |
| 26 | + 'createMissing': True, |
20 | 27 | },
|
21 | 28 | 'discoveryRules': {
|
22 |
| - 'createMissing': 'true', |
23 |
| - 'updateExisting': 'true' |
| 29 | + 'createMissing': True, |
| 30 | + 'updateExisting': True |
24 | 31 | },
|
25 | 32 | 'graphs': {
|
26 |
| - 'createMissing': 'true', |
27 |
| - 'updateExisting': 'true' |
| 33 | + 'createMissing': True, |
| 34 | + 'updateExisting': True |
28 | 35 | },
|
29 | 36 | 'groups': {
|
30 |
| - 'createMissing': 'true' |
| 37 | + 'createMissing': True |
31 | 38 | },
|
32 | 39 | 'hosts': {
|
33 |
| - 'createMissing': 'true', |
34 |
| - 'updateExisting': 'true' |
| 40 | + 'createMissing': True, |
| 41 | + 'updateExisting': True |
35 | 42 | },
|
36 | 43 | 'images': {
|
37 |
| - 'createMissing': 'true', |
38 |
| - 'updateExisting': 'true' |
| 44 | + 'createMissing': True, |
| 45 | + 'updateExisting': True |
39 | 46 | },
|
40 | 47 | 'items': {
|
41 |
| - 'createMissing': 'true', |
42 |
| - 'updateExisting': 'true' |
| 48 | + 'createMissing': True, |
| 49 | + 'updateExisting': True |
43 | 50 | },
|
44 | 51 | 'maps': {
|
45 |
| - 'createMissing': 'true', |
46 |
| - 'updateExisting': 'true' |
| 52 | + 'createMissing': True, |
| 53 | + 'updateExisting': True |
47 | 54 | },
|
48 | 55 | 'screens': {
|
49 |
| - 'createMissing': 'true', |
50 |
| - 'updateExisting': 'true' |
| 56 | + 'createMissing': True, |
| 57 | + 'updateExisting': True |
51 | 58 | },
|
52 | 59 | 'templateLinkage': {
|
53 |
| - 'createMissing': 'true', |
54 |
| - 'updateExisting': 'true' |
| 60 | + 'createMissing': True, |
55 | 61 | },
|
56 | 62 | 'templates': {
|
57 |
| - 'createMissing': 'true', |
58 |
| - 'updateExisting': 'true' |
| 63 | + 'createMissing': True, |
| 64 | + 'updateExisting': True |
59 | 65 | },
|
60 | 66 | 'templateScreens': {
|
61 |
| - 'createMissing': 'true', |
62 |
| - 'updateExisting': 'true' |
| 67 | + 'createMissing': True, |
| 68 | + 'updateExisting': True |
63 | 69 | },
|
64 | 70 | 'triggers': {
|
65 |
| - 'createMissing': 'true', |
66 |
| - 'updateExisting': 'true' |
| 71 | + 'createMissing': True, |
| 72 | + 'updateExisting': True |
| 73 | + }, |
| 74 | + 'valueMaps': { |
| 75 | + 'createMissing': True, |
| 76 | + 'updateExisting': True |
67 | 77 | },
|
68 | 78 | }
|
69 | 79 |
|
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