-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix_template_exporter.py
executable file
·168 lines (141 loc) · 6.54 KB
/
zabbix_template_exporter.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Little program to export all Zabbix Templates either to to one tar.gz or to individual xml files'
"""
__description__ = 'Little program to export Zabbix Templates either to to one tar.gz or to individual xml files'
__author__ = "Christian Anton, secadm GmbH <[email protected]>"
__copyright__ = "Copyright 2017, secadm GmbH, Im Krautgarten 22, 82216 Maisach"
__license__ = "MIT"
__version__ = "0.1.0"
__maintainer__ = "Christian Anton"
__email__ = "[email protected]"
__status__ = "Production"
import os
import re
import tempfile
import tarfile
import datetime
import shutil
import sys
import logging
import argparse
import json
import xml.dom.minidom
from zabbix.api import ZabbixAPI
loglevel = logging.WARNING
def set_up_logging(loglevel, log_to_foreground, program_name=None):
if log_to_foreground is True:
logging.basicConfig(level=loglevel, format='%(levelname)s:%(message)s')
else:
# logging.basicConfig(level=loglevel, format='%(asctime)s %(levelname)s:%(message)s')
class ContextFilter(logging.Filter):
hostname = socket.gethostname()
username = pwd.getpwuid(os.getuid())[0]
def filter(self, record):
record.hostname = ContextFilter.hostname
record.username = ContextFilter.username
return True
syslog_socket = "/dev/log"
if 'darwin' == sys.platform:
# Hint for macOS:
# To configure syslog to writhe DEBUG messages to /var/log/system.log
# - the agent must be started in daemon mode
# - the agent must be started with debug option
# - add the following line must be added to /etc/asl.conf:
# ? [= Sender EdwardAgent] [<= Level debug] file system.log
# - syslogd must be restarted
syslog_socket = '/var/run/syslog'
syslog_format_string = '[%(process)d]: [%(levelname)s][%(funcName)s] %(message)s'
rootLogger = logging.getLogger()
rootLogger.setLevel(loglevel)
syslogFilter = ContextFilter()
rootLogger.addFilter(syslogFilter)
if program_name is None:
program_name = os.path.basename(__file__)
syslog = SysLogHandler(address=syslog_socket, facility=SysLogHandler.LOG_USER)
syslogFormatter = logging.Formatter('%s %s' % (program_name, syslog_format_string), datefmt='%b %d %H:%M:%S')
syslog.setFormatter(syslogFormatter)
rootLogger.addHandler(syslog)
def parse_arguments():
global loglevel
parser = argparse.ArgumentParser(description=__description__)
parser.add_argument('-d', '--debug', action='store_true', help='set log level to DEBUG')
parser.add_argument('-D', '--directory', type=str, default='.', help='directory to dump template files to')
parser.add_argument('-n', '--name', type=str, required=False, help='technical name of template to export')
parser.add_argument('-v', '--visible-name', type=str, required=False, help='visible name of the template to export')
parser.add_argument('-t', '--tarball', action='store_true', help='create tarball of all exported templates in given directory')
parser.add_argument('-r', '--reset-date', action='store_true', help='reset export date to 2000-01-01T00:00:00Z')
parser.add_argument('-j', '--json', action='store_true', help='format of output')
parser.add_argument('-y', '--yaml', action='store_true', help='format of output')
# parser.add_argument('somearg', type=str, default='bla', help='this is something important')
args = parser.parse_args()
if args.debug:
loglevel = logging.DEBUG
return args
def main():
if args.tarball:
tmp_dir = tempfile.mkdtemp()
today = datetime.date.today().strftime('%Y-%m-%d')
zapi = ZabbixAPI(os.environ['ZABBIX_URL'], user=os.environ['ZABBIX_USERNAME'], password=os.environ['ZABBIX_PASSWORD'])
if args.json:
export_format='json'
elif args.yaml:
export_format='yaml'
else:
export_format='xml'
if args.name:
templates = zapi.template.get(filter={'host': args.name})
elif args.visible_name:
templates = zapi.template.get(filter={'name': args.visible_name})
else:
templates = zapi.template.get()
for template in templates:
logging.info('found template "%s" (%s) with id %s' % (template['name'], template['host'], template['templateid']))
#template_file_basename = re.sub(r'[ \.]', '_', template['name'])
template_file_basename = re.sub(r'\/', '#', template['host'])
if args.tarball:
backup_file_path = os.path.join(tmp_dir, '%s.%s' % (template_file_basename, export_format))
else:
backup_file_path = os.path.join(args.directory, '%s.%s' % (template_file_basename, export_format))
f = open(backup_file_path, 'w')
template_output = zapi.configuration.export(format=export_format, options={'templates': [template['templateid']]})
if args.json:
j = json.loads(template_output)
if args.reset_date:
j['zabbix_export']['date'] = '2000-01-01T00:00:00Z'
f.write(json.dumps(j, sort_keys=True, indent=4, ensure_ascii=False))
if args.yaml:
# TODO implement overwrite of date
f.write(template_output)
else:
dom = xml.dom.minidom.parseString(template_output)
if args.reset_date:
date = dom.firstChild.getElementsByTagName('date')
date[0].firstChild.data = '2000-01-01T00:00:00Z'
pretty_xml = dom.toprettyxml(indent=" ", encoding='utf-8').decode('utf-8')
pretty_xml = re.sub(r'(<[^/]+)/>', r'\1 />', pretty_xml)
f.write(pretty_xml)
if len(templates) == 0:
raise Exception('no templates found')
if args.tarball:
logging.info('creating tarball in %s' % args.directory)
tarfile_path = os.path.join(args.directory, 'templates_%s.tar.gz' % today)
tar = tarfile.open(tarfile_path, 'w:gz')
tar.add(tmp_dir, arcname='')
tar.close()
shutil.rmtree(tmp_dir)
if __name__ == '__main__':
args = parse_arguments()
log_to_foreground = False
if sys.stdin.isatty():
if int(loglevel) >= int(logging.INFO):
loglevel = logging.INFO
log_to_foreground = True
set_up_logging(loglevel, log_to_foreground, program_name='zabbix_template_exporter')
try:
sys.exit(main())
except Exception as e:
raise
logging.error(e)
sys.exit(255)