File: import_templates.py

package info (click to toggle)
pyzabbix 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: python: 903; makefile: 45
file content (68 lines) | stat: -rw-r--r-- 2,052 bytes parent folder | download
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
"""
Import Zabbix XML templates
"""

import os
import sys

from pyzabbix import ZabbixAPI, ZabbixAPIException

if len(sys.argv) <= 1:
    print(
        "Please provide directory with templates as first ARG or the XML file with template."
    )
    exit(1)

path = sys.argv[1]

# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = "https://zabbix.example.org"

zapi = ZabbixAPI(ZABBIX_SERVER)

# Login to the Zabbix API
# zapi.session.verify = False
zapi.login("Admin", "zabbix")

rules = {
    "applications": {
        "createMissing": True,
    },
    "discoveryRules": {"createMissing": True, "updateExisting": True},
    "graphs": {"createMissing": True, "updateExisting": True},
    "groups": {"createMissing": True},
    "hosts": {"createMissing": True, "updateExisting": True},
    "images": {"createMissing": True, "updateExisting": True},
    "items": {"createMissing": True, "updateExisting": True},
    "maps": {"createMissing": True, "updateExisting": True},
    "screens": {"createMissing": True, "updateExisting": True},
    "templateLinkage": {"createMissing": True},
    "templates": {"createMissing": True, "updateExisting": True},
    "templateScreens": {"createMissing": True, "updateExisting": True},
    "triggers": {"createMissing": True, "updateExisting": True},
    "valueMaps": {"createMissing": True, "updateExisting": True},
}

if os.path.isdir(path):
    # path = path/*.xml
    files = glob.glob(path + "/*.xml")
    for file in files:
        print(file)
        with open(file) as f:
            template = f.read()
            try:
                zapi.confimport("xml", template, rules)
            except ZabbixAPIException as e:
                print(e)
        print("")
elif os.path.isfile(path):
    files = glob.glob(path)
    for file in files:
        with open(file) as f:
            template = f.read()
            try:
                zapi.confimport("xml", template, rules)
            except ZabbixAPIException as e:
                print(e)
else:
    print("I need a xml file")