-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphite2zabbix.py
executable file
·402 lines (335 loc) · 13.5 KB
/
graphite2zabbix.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
#
# -*- coding: utf-8 -*-
# graphite2zabbix
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; only version 2 of the License is applicable.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Author:
# Lior Goikhburg <goikhburg at gmail.com >
#
# Description:
# This program sends graphite metrics to zabbix
#
# Graphite
# http://graphite.wikidot.com/
#
# Zabbix
# http://www.zabbix.com/
__author__ = 'Lior Goikhburg'
import argparse
import logging
import json
import os
import re
import requests
import sys
import time
import urllib
from pyzabbix import ZabbixAPI
from zabbixtrapper import ZabbixTrapper
class Graphite(object):
def __init__(self, host='localhost', port='80',
scheme='http', username='', password='', uri='/render'):
self.uri = uri
self.host = host
self.port = port
self.scheme = scheme
self.session = requests.Session()
if username:
self.session.auth = (username, password)
if self.scheme == 'https':
self.session.verify = False
def _get_data(self, params):
"""
Request data from graphite.
"""
request_uri = '?'.join([self.uri, params]) + '&format=json'
url = "%s://%s:%s%s" % (self.scheme, self.host, self.port, request_uri)
logger.debug("Requestig %s from Graphite API" % url)
try:
resp = self.session.get(url)
except Exception as error:
logger.error("Graphite API Error: %s" % error.message)
return None
if resp.status_code != 200:
logger.error("Graphite API status code: %s (%s)" % (
resp.status_code, resp.reason))
return None
return resp.json()
def get_metrics(self, metrics, _from, until='now'):
"""
From metrics list create uri params, add time period and request data
"""
targets = '&target=' + '&target='.join([urllib.quote(metric)
for metric in metrics])
period = "from=%s&until=%s" % (_from, until)
params = '&'.join([targets, period])
return self._get_data(params)
def get_last_val(datapoints):
"""
Get last (non-null) datapoint from list of graphite datapoints.
If all values are null - return None
"""
for datapoint in reversed(datapoints):
if datapoint[0] is not None:
return datapoint
return None
def parse_zabbix_info(info):
"""
Parses Zabbix trapper info line into a dict
"""
info_parsed = {}
for section in info.split(';'):
key, delim, val = section.partition(':')
if delim:
info_parsed[key.strip()] = float(val)
return info_parsed
def send_zabbix(traps):
"""
Submit traps to Zabbix
"""
try:
ztrapper = ZabbixTrapper(config['zabbix']['trapper']['host'],
config['zabbix']['trapper']['port'])
trapper_result = ztrapper.send_traps(traps)
except Exception as error:
logger.error("Error sending traps to zabbix: %s" % error.message)
return False
response = trapper_result['response']
info = trapper_result['info']
if response != 'success':
logger.error("Error sending traps to zabbix: %s" % info)
return False
logger.debug("Traps accepted (%s)" % info)
info_parsed = parse_zabbix_info(info)
if info_parsed['failed']:
logger.warning("%i traps were not accepted by Zabbix" % info_parsed['failed'])
return True
# extract metric name from zabbix key name
def extract_metric(zapi, item):
"""
Extract metric name from key, resolve user macros if any
"""
global item_cache
key = item['key_']
# resolve macros in key
macros = zabbix_macro_re.findall(key)
if macros:
logger.debug("Key %s has user macros. Expansion is needed." % key)
macro_dict = {element: '' for element in macros}
# look up macros:
# 1. host
# 2. directly linked template (to host)
# 3. a template linked to template etc, etc.
process = True
while process:
hostid = item['hostid']
try:
user_macros = zapi.usermacro.get(output=['macro', 'value'],
hostids=[hostid])
except Exception as error:
logger.error("Zabbix API Error: %s", error.message)
return ''
for user_macro in user_macros:
macro_name = user_macro['macro']
macro_value = user_macro['value']
if macro_name in macro_dict and not macro_dict[macro_name]:
logger.debug("Resolved User macro %s to %s" % (macro_name, macro_value))
macro_dict[macro_name] = macro_value
parentid = item['templateid']
if parentid == '0':
process = False
else:
# fetch item from cache, or request from api
if parentid not in item_cache:
try:
item_cache[parentid] = zapi.item.get(output=['hostid', 'templateid'],
itemids=[parentid])[0]
except Exception as error:
logger.error("Zabbix API Error: %s", error.message)
return ''
item = item_cache[parentid]
for macro_name, macro_value in macro_dict.iteritems():
if not macro_value:
logger.warning("Failed to resolve a user macro %s in key %s - skipping it." %
(macro_name, key))
return ''
key = key.replace(macro_name, macro_value)
# extract metric part
match = metric_name_re.search(key)
metric = ''
if match and len(match.groups()) == 1:
metric = match.group(1)
return metric
def main():
# Connect to Zabbix API
logger.debug('Connecting to Zabbix API')
zapi = ZabbixAPI(server="%s://%s:%s%s" % (config['zabbix']['api']['scheme'],
config['zabbix']['api']['host'],
config['zabbix']['api']['port'],
config['zabbix']['api']['uri']))
if config['zabbix']['api']['scheme'] == 'https':
zapi.session.verify = False
try:
zapi.login(config['zabbix']['api']['username'],
config['zabbix']['api']['password'])
except Exception as error:
logger.error("Login to Zabbix Error: %s", error.message)
return False
# Query Zabbix to retrieve only hosts that match the following criteria
# 1. Host is being monitored
# 2. Host has items
# Data returned will contain:
# 1 host ids
# 2 host names
logger.debug('Looking for hosts in Zabbix')
try:
zhosts = zapi.host.get(output='shorten',
monitored_hosts=True,
with_items=True,
)
except Exception as error:
logger.error("Zabbix API Error: %s", error.message)
return False
if not zhosts:
logger.error('No configured Zabbix hosts found')
return False
hostids = [host_entry['hostid'] for host_entry in zhosts]
logger.debug("Found %s relevant hosts" % len(zhosts))
# Query Zabbix to retrieve only items which match the following criteria
# 1. Item is of the 'zabbix trapper' (2) type
# 2. The key_ name begins with config['zabbix']['key_prefix']
# 3. The item belongs to one of the zhosts (this will rule out items linked to templates).
# Data returned will contain:
# 1. Item id
# 2. key_ name
# 3. host id of the linked host
# 4. templateid id of the parent ITEM
logger.debug('Looking for zabbix items')
try:
zitems = zapi.item.get(output=['key_', 'hostid', 'templateid'],
filter={'type': 2},
search={'key_': config['zabbix']['key_prefix']},
startSearch=True,
hostids=hostids,
selectHosts=['name'])
except Exception as error:
logger.error("Zabbix API Error: %s", error.message)
return False
if not zitems:
logger.error('No configured Zabbix items found')
return False
logger.debug("Found %s relevant items" % len(zitems))
# Create dict of dicts of zabbix data (only missing values)
zabbix_data = {}
for item in zitems:
fqdn = item['hosts'][0]['name']
hostname = fqdn.partition('.')[0]
logger.debug("Processing Key: %s:%s" % (fqdn, item['key_']))
metric = extract_metric(zapi, item)
if not metric:
logger.warning("Incorrectly configured key %s" % item['key_'])
continue
metric = "%s.%s" % (hostname, metric)
zabbix_data[metric] = {'host': fqdn, 'key': item['key_'], 'clock': int(time.time())}
graphite = Graphite(host=config['graphite']['host'],
port=config['graphite']['port'],
scheme=config['graphite']['scheme'],
username=config['graphite']['username'],
password=config['graphite']['password'],
uri=config['graphite']['uri'])
logger.debug('Requesting metrics from graphite')
targets = graphite.get_metrics(zabbix_data.keys(), config['graphite']['period'])
if not targets:
logger.error('No metrics matching configured zabbix keys found in graphite')
return False
logger.debug("Received %s metrics" % len(targets))
if len(targets) < len(zabbix_data):
logger.warning('Graphite returned less metrics, than requested')
# reformat graphite data into a dict with metric_name -> [[datapoints]]
graphite_metrics = {}
for target in targets:
metric_name = target['target']
graphite_metrics[metric_name] = target['datapoints']
# Build traps list, while checking for all metric configured in zabbix.
# If metric exists in graphite checks if its last value is null
traps = []
for metric in zabbix_data.keys():
if metric not in graphite_metrics.keys():
logger.warning("Metric %s doesn't exist in graphite" % metric)
continue
else:
last_val = get_last_val(graphite_metrics[metric])
if not last_val:
logger.warning("No data for metric %s " % metric)
continue
else:
zabbix_data[metric].update({'value': last_val[0]})
traps.append(zabbix_data[metric])
logger.debug("Sending %s traps to Zabbix" % len(traps))
return send_zabbix(traps)
if __name__ == '__main__':
config = {
'zabbix': {
'key_prefix': 'graphite.metric',
'api': {
'host': 'zabbix.host.com',
'port': '443',
'scheme': 'https',
'username': 'api-username',
'password': 'api-password',
'uri': '/'
},
'trapper': {
'host': 'zabbix.trapper.host.com',
'port': 10051,
'status_key': 'graphite2zabbix.status'
}
},
'graphite': {
'host': 'graphite.host.com',
'port': '443',
'scheme': 'https',
'username': 'graphite-user',
'password': 'graphite-password',
'uri': '/render',
'period': '-3minutes'
}
}
metric_name_re = re.compile(r'\[([^\[]+)\]')
zabbix_macro_re = re.compile(r'{\$[^}]+}')
item_cache = {}
parser = argparse.ArgumentParser(prog=os.path.basename(__file__),
description='Graphite 2 Zabbix Bridge',
epilog="Example: %(prog)s -v")
parser.add_argument('-v', '--verbose', help='Verbose', action='count')
args = parser.parse_args()
LogLevel = logging.WARNING
if args.verbose:
LogLevel = logging.DEBUG
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s: %(message)s',
datefmt='%F %H:%M:%S', level=logging.WARNING)
logger = logging.getLogger(os.path.basename(__file__))
logger.setLevel(LogLevel)
exit_status = 0 if main() else 1
if 'status_key' in config['zabbix']['trapper'] and config['zabbix']['trapper']['status_key']:
# send program status to zabbix
logger.debug("Sending program status <%s> to Zabbix" % exit_status)
result = send_zabbix([{'host': config['zabbix']['trapper']['host'],
'key': config['zabbix']['trapper']['status_key'],
'value': exit_status,
'clock': int(time.time())}])
exit_status = 0 if result and exit_status == 0 else 1
sys.exit(exit_status)