-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_export.py
102 lines (78 loc) · 2.9 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
#!/usr/bin/python
import argparse
import logging
import time
import os
import json
from zabbix.api import ZabbixAPI
from sys import exit
from datetime import datetime
parser = argparse.ArgumentParser(
description='This is a simple tool to export zabbix templates for backup. Please note it will always set the data on export to 1/1/2016 so git wont update unless something substantial happens.')
parser.add_argument(
'--templates', help='Name of specific template to export', default='All')
parser.add_argument(
'--out-dir', help='Directory to output templates to.', default='./templates')
parser.add_argument(
'--debug', help='Enable debug mode, this will show you all the json-rpc calls and responses', action="store_true")
parser.add_argument(
'--url', help='URL to the zabbix server (example: https://monitor.example.com/zabbix)', required=True)
parser.add_argument('--user', help='The zabbix api user', required=True)
parser.add_argument(
'--password', help='The zabbix api password', required=True)
args = parser.parse_args()
if args.debug:
logging.basicConfig(
level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logger = logging.getLogger(__name__)
def main():
global args
global parser
if None == args.url:
print "Error: Missing --url\n\n"
exit(2)
if None == args.user:
print "Error: Missing --user\n\n"
exit(3)
if None == args.password:
print "Error: Missing --password\n\n"
exit(4)
if False == os.path.isdir(args.out_dir):
os.mkdir(args.out_dir)
zm = ZabbixTemplates(args.url, args.user, args.password)
zm.exportTemplates(args)
class ZabbixTemplates:
def __init__(self, _url, _user, _password):
self.zapi = ZabbixAPI(url=_url, user=_user, password=_password)
def exportTemplates(self, args):
request_args = {
"output": "extend"
}
if args.templates != 'All':
request_args.filter = {
"host": [args.tempaltes]
}
result = self.zapi.do_request('template.get', request_args)
if not result['result']:
print "No matching host found for '{}'".format(hostname)
exit(-3)
if result['result']:
for t in result['result']:
dest = args.out_dir + '/' + t['host'] + '.json'
self.exportTemplate(t['templateid'], dest)
def exportTemplate(self, tid, oput):
print "tempalteid:", tid, " output:", oput
args = {
"options": {
"templates": [tid]
},
"format": "json"
}
result = self.zapi.do_request('configuration.export', args)
jsondump = json.dumps(result['result'])
template = json.loads(jsondump)
f = open(oput, 'w+')
f.write(template)
f.close()
if __name__ == '__main__':
main()