import os
try: import json
except ImportError: import simplejson as json
import agent_util
import datetime
def get_unbound_stats():
unbound_stats = {}
unbound_binary = agent_util.which("unbound-control")
ret, out = agent_util.execute_command("%s stats" % unbound_binary)
if ret != 0:
print("ERROR RUNNING UNBOUND: %s" % out)
return {"error": str(out)}, False
tmp = out.strip().split('\n')
for line in tmp:
k, v = line.split('=')
unbound_stats[k] = float(v)
return unbound_stats, True
def get_unbound_options(unbound_stats):
options = []
for opt in unbound_stats.keys():
name = opt.split('.')[0]
if name == 'time': continue
options.append(name)
return list(set(options))
class UnboundDNS(agent_util.Plugin):
"""
WARNING:
You MUST SET AT LEAST 755 ON '/etc/unbound/unbound_control.*'
AND '/etc/unbound/unbound_server.*' FOR THIS PLUGIN TO WORK PROPERLY
"""
textkey = "unbound_dns"
label = "Unbound DNS Resolver"
@classmethod
def get_metadata(self, config):
status = agent_util.SUPPORTED
msg = None
unbound_options = ["total"]
# check to confirm unbound is installed
unbound_installed = agent_util.which("unbound-control")
if not unbound_installed:
status = agent_util.UNSUPPORTED
msg = "Command unbound-control not installed"
return {}
else:
unbound_stats, passed = get_unbound_stats()
if passed is False:
self.log.error("ERROR with Unbound:\n%s" % unbound_stats['error'])
stats = agent_util.MISCONFIGURED
msg = unbound_stats['error']
else:
self.log.debug("Found unbound stats!\n%s" % unbound_stats)
unbound_options = get_unbound_options(unbound_stats)
self.log.error("Found %s unbound DNS keys: %s" % (len(unbound_options), unbound_options))
metadata = {
"num.queries": {
"label": "Number of queries",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": ""
},
"num.cachehits": {
"label": "Number of cache hits",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": ""
},
"num.cachemiss": {
"label": "Cache misses",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": ""
},
"num.recursivereplies": {
"label": "Recursive replies",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": ""
},
"requestlist.avg": {
"label": "Average queued queries",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": ""
},
"requestlist.max": {
"label": "Max queued queries",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": ""
},
"recursion.time.avg": {
"label": "Average recursion time",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": "seconds"
},
"recursion.time.median": {
"label": "Median recursion time",
"options": unbound_options,
"status": status,
"error_message": msg,
"unit": "seconds"
},
"time.up": {
"label": "Service uptime",
"options": None,
"status": status,
"error_message": msg,
"unit": "seconds"
},
}
return metadata
def check(self, textkey, data, config):
unbound_stats, passed = get_unbound_stats()
self.log.debug(unbound_stats)
if data is None:
key = textkey
else:
key = "%s.%s" % (str(data),str(textkey))
self.log.debug(str(agent_util.which("unbound-control")))
self.log.debug("Checking for Unbound key %s" % key)
return unbound_stats.get(key, None)