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