-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix_export.py
190 lines (154 loc) · 5.64 KB
/
zabbix_export.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
from zabbix_utils import ZabbixAPI
from zipfile import ZipFile as zip
import logging as log
import sys, os, argparse, time, atexit
ZBX_OBJS = {
'hosts': {
'zbx_obj': 'host',
'zbx_obj_id': 'hostid',
'zbx_exp_opt': 'hosts'
},
'hostgroups': {
'zbx_obj': 'hostgroup',
'zbx_obj_id': 'groupid',
'zbx_exp_opt': 'host_groups'
},
'maps': {
'zbx_obj': 'map',
'zbx_obj_id': 'sysmapid',
'zbx_exp_opt': 'maps'
},
'images': {
'zbx_obj': 'image',
'zbx_obj_id': 'imageid',
'zbx_exp_opt': 'images'
},
'mediatypes': {
'zbx_obj': 'mediatype',
'zbx_obj_id': 'mediatypeid',
'zbx_exp_opt': 'mediaTypes'
},
'templategroups': {
'zbx_obj': 'templategroup',
'zbx_obj_id': 'groupid',
'zbx_exp_opt': 'template_groups'
},
'templates': {
'zbx_obj': 'template',
'zbx_obj_id': 'templateid',
'zbx_exp_opt': 'templates'
},
}
zip_files = []
@atexit.register
def deleteItermFiles():
for _file in zip_files:
if os.path.exists(_file):
os.remove(_file)
def full_path (relative_path: str) -> str:
P_WD = os.path.dirname(__file__)
return os.path.join(P_WD, relative_path)
def mkdir(path: str) -> None:
if not os.path.isdir(path):
os.makedirs(path)
log.info(f"Created directory '{path}'")
def readConfig(filepath: str, delim: str = '=') -> dict:
try:
with open(filepath) as f:
l = [line.split(delim) for ln in f.readlines() \
if (line := ln.strip()) and not line.startswith('#')]
return {key.strip().strip('"'): value.strip().strip('"') for key, value in l}
except FileNotFoundError as e:
log.error(e)
sys.exit(1)
def export(zbx_api: object,
zbx_obj: str,
zbx_obj_id: str,
zbx_exp_opt,
format: str = 'yaml') -> str:
"""
Export configuration data as a serialized string.
Args:
zbx_api: the ZabbixAPI object
zbx_obj: the zabbix objects type
zbx_obj_id: the zabbix object's id name
zbx_exp_opt: the zabbix object's output option
format: the serialized output format; possible values: yaml, json, xml
Returns:
The serialized string, or None in case of errors
"""
try:
# Retrieve the objects
obj_api = eval('api.' + zbx_obj)
obj_ids = obj_api.get( output=[zbx_obj_id] )
# flatten the ids in a list
list_ids = [ i[zbx_obj_id] for i in obj_ids ]
return api.configuration.export(
options={
zbx_exp_opt: list_ids
},
format=format, prettyprint=True )
except Exception as e:
log.error(f"Failed to export '{zbx_obj}' objects")
log.error(e)
def parse_args():
lTypes = ['all'] + list(ZBX_OBJS.keys())
types = ', '.join(lTypes)
parser = argparse.ArgumentParser(description =
"""
Export configurations from Zabbix server.
""")
parser.add_argument('-d', '--dir', action = "store",
help = "Directory where to save the exported configuration (default: current dir)",
default = '.')
parser.add_argument('-c', '--config', action = "store",
help = "The configuration file, relative to the program's location (default: %(default)s)",
default = 'zbx_config.ini')
parser.add_argument('-t', '--types', nargs = '+', choices = lTypes,
help = f"Which type(s) of configuration to export. Possible values: {types}. Default: %(default)s",
default = ['hosts'])
parser.add_argument('-f', '--format', choices = ['yaml', 'xml', 'json'],
help = "The output format. Default: %(default)s",
default = 'yaml')
parser.add_argument('-z', '--zip', action = 'store_true',
help = "Compress the exported configutation data")
parser.add_argument('-v', '--verbose', action = 'store_true',
help = "Enable debug mode")
return parser.parse_args()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~+~~ MAIN ~~+~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == "__main__":
args = parse_args()
log_lvl = log.DEBUG if args.verbose else log.INFO
log.basicConfig(level=log_lvl, format='%(asctime)s :: %(levelname)s :: %(message)s')
ZABBIX_AUTH = readConfig(full_path(args.config))
# Create an instance of the ZabbixAPI class with the specified authentication details
if 'token' not in ZABBIX_AUTH:
log.error("No authentication token is provided in configuration file")
sys.exit(1)
api = ZabbixAPI(**ZABBIX_AUTH)
mkdir(args.dir)
if 'all' in args.types:
args.types = list(ZBX_OBJS.keys())
# Collect the exported configuration data
timestamp = time.strftime("%y%m%d%H%M")
tmstmp = '_' + timestamp if not args.zip else ''
for _type in args.types:
data = export(api, **ZBX_OBJS[_type], format = args.format)
if data is not None:
output_file = os.path.join(args.dir, f"zbx_{_type}{tmstmp}.{args.format}")
with open(output_file, 'w') as f:
f.write(data)
if args.zip:
zip_files.append(output_file)
else:
log.info(f"Written configuration file: {output_file}")
# Compress the exported files
if len(zip_files):
zipfile = os.path.join(args.dir, f"zbx_export_{timestamp}.zip")
with zip(zipfile, mode='w') as archive:
for f in zip_files:
archive.write(f, arcname = os.path.basename(f))
log.info(f"Compressed configuration file: {zipfile}")