forked from tarnfeld/collectd-unbound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unbound.py
75 lines (56 loc) · 2.01 KB
/
unbound.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
#! /usr/bin/python
# Copyright 2014 Tom Arnfeld
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collectd
import subprocess
PREFIX = "unbound"
STATS_CMD = ["/usr/sbin/unbound-control", "stats"]
VERBOSE_LOGGING = False
def configure_callback(conf):
"""Received configuration information"""
global STATS_CMD, VERBOSE_LOGGING
for node in conf.children:
if node.key == 'StatsCmd':
STATS_CMD = node.values[0].split(" ")
elif node.key == 'Verbose':
VERBOSE_LOGGING = bool(node.values[0])
else:
collectd.warning('unbound plugin: Unknown config key: %s.' % node.key)
def fetch_stats():
proc = subprocess.Popen(STATS_CMD, stdout=subprocess.PIPE)
return_code = proc.wait()
if return_code > 0:
collectd.error('ubnound plugin: Error code collecting stats %d' % return_code)
else:
for line in proc.stdout:
yield parse_stat(line.rstrip())
def parse_stat(stat_line):
return stat_line.split("=")
def dispatch_stat(key, value):
val = collectd.Values(plugin='unbound')
val.type = "gauge"
val.type_instance = key.replace(".", "/")
val.values = [value]
val.dispatch()
def read_callback():
log_verbose('Read callback called')
stats = fetch_stats()
for key, value in stats:
dispatch_stat(key, value)
def log_verbose(msg):
if not VERBOSE_LOGGING:
return
collectd.info('mesos plugin [verbose]: %s' % msg)
collectd.register_config(configure_callback)
collectd.register_read(read_callback)