Commit 2e29de6c authored by Franco Fichtner's avatar Franco Fichtner

unbound: merge statistics API

parent cce9955e
...@@ -368,6 +368,7 @@ ...@@ -368,6 +368,7 @@
/usr/local/opnsense/mvc/app/controllers/OPNsense/TrafficShaper/forms/dialogPipe.xml /usr/local/opnsense/mvc/app/controllers/OPNsense/TrafficShaper/forms/dialogPipe.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/TrafficShaper/forms/dialogQueue.xml /usr/local/opnsense/mvc/app/controllers/OPNsense/TrafficShaper/forms/dialogQueue.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/TrafficShaper/forms/dialogRule.xml /usr/local/opnsense/mvc/app/controllers/OPNsense/TrafficShaper/forms/dialogRule.xml
/usr/local/opnsense/mvc/app/controllers/OPNsense/Unbound/Api/DiagnosticsController.php
/usr/local/opnsense/mvc/app/library/Google/API/Drive.php /usr/local/opnsense/mvc/app/library/Google/API/Drive.php
/usr/local/opnsense/mvc/app/library/OPNsense/Auth/API.php /usr/local/opnsense/mvc/app/library/OPNsense/Auth/API.php
/usr/local/opnsense/mvc/app/library/OPNsense/Auth/AuthenticationFactory.php /usr/local/opnsense/mvc/app/library/OPNsense/Auth/AuthenticationFactory.php
...@@ -624,6 +625,7 @@ ...@@ -624,6 +625,7 @@
/usr/local/opnsense/scripts/systemhealth/fetchData.py /usr/local/opnsense/scripts/systemhealth/fetchData.py
/usr/local/opnsense/scripts/systemhealth/flush_rrd.py /usr/local/opnsense/scripts/systemhealth/flush_rrd.py
/usr/local/opnsense/scripts/systemhealth/listReports.py /usr/local/opnsense/scripts/systemhealth/listReports.py
/usr/local/opnsense/scripts/unbound/unboundctlwrapper
/usr/local/opnsense/service/conf/actions.d/actions_captiveportal.conf /usr/local/opnsense/service/conf/actions.d/actions_captiveportal.conf
/usr/local/opnsense/service/conf/actions.d/actions_configd.conf /usr/local/opnsense/service/conf/actions.d/actions_configd.conf
/usr/local/opnsense/service/conf/actions.d/actions_cron.conf /usr/local/opnsense/service/conf/actions.d/actions_cron.conf
...@@ -646,6 +648,7 @@ ...@@ -646,6 +648,7 @@
/usr/local/opnsense/service/conf/actions.d/actions_system.conf /usr/local/opnsense/service/conf/actions.d/actions_system.conf
/usr/local/opnsense/service/conf/actions.d/actions_systemhealth.conf /usr/local/opnsense/service/conf/actions.d/actions_systemhealth.conf
/usr/local/opnsense/service/conf/actions.d/actions_template.conf /usr/local/opnsense/service/conf/actions.d/actions_template.conf
/usr/local/opnsense/service/conf/actions.d/actions_unbound.conf
/usr/local/opnsense/service/conf/actions_service.conf /usr/local/opnsense/service/conf/actions_service.conf
/usr/local/opnsense/service/conf/configd.conf /usr/local/opnsense/service/conf/configd.conf
/usr/local/opnsense/service/configd.py /usr/local/opnsense/service/configd.py
......
<?php
/**
* Copyright (C) 2017 Fabian Franz
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
namespace OPNsense\Unbound\Api;
use \OPNsense\Base\ApiControllerBase;
use \OPNsense\Core\Backend;
/**
* Class DiagnosticsextensionController
* @package OPNsense\Unbound\Api
*/
class DiagnosticsController extends ApiControllerBase
{
/**
* reconfigure return the stats
*/
public function statsAction()
{
$ret['status'] = "failed";
$backend = new Backend();
$result = trim($backend->configdRun('unbound stats'));
if ($result != "null") {
$ret['status'] = "ok";
$ret['data'] = json_decode($result);
}
return $ret;
}
/**
* return the entries of the cache
*/
public function dumpcacheAction()
{
$ret['status'] = "failed";
$backend = new Backend();
$result = json_decode(trim($backend->configdRun("unbound dumpcache")), true);
if ($result !== null) {
$ret['data'] = $result;
$ret['status'] = 'ok';
}
return $ret;
}
public function dumpinfraAction()
{
$ret['status'] = "failed";
$backend = new Backend();
$result = json_decode(trim($backend->configdRun("unbound dumpinfra")), true);
if ($result !== null) {
$ret['data'] = $result;
$ret['status'] = 'ok';
}
return $ret;
}
public function listlocaldataAction()
{
$ret['status'] = "failed";
$backend = new Backend();
$result = json_decode(trim($backend->configdRun("unbound listlocaldata")), true);
if ($result !== null) {
$ret['data'] = $result;
$ret['status'] = 'ok';
}
return $ret;
}
public function listlocalzonesAction()
{
$ret['status'] = "failed";
$backend = new Backend();
$result = json_decode(trim($backend->configdRun("unbound listlocalzones")), true);
if ($result !== null) {
$ret['data'] = $result;
$ret['status'] = 'ok';
}
return $ret;
}
public function listinsecureAction()
{
$ret['status'] = "failed";
$backend = new Backend();
$result = json_decode(trim($backend->configdRun("unbound listinsecure")), true);
if ($result !== null) {
$ret['data'] = $result;
$ret['status'] = 'ok';
}
return $ret;
}
}
#!/usr/local/bin/python2.7
"""
Copyright (c) 2017 Ad Schellevis
Copyright (C) 2017 Fabian Franz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""
import os
import sys
import re
import tempfile
import subprocess
import argparse
import json
def unbound_control_reader(action):
with tempfile.NamedTemporaryFile() as output_stream:
subprocess.call(['/usr/sbin/unbound-control', '-c', '/var/unbound/remotecontrol.conf', action],
stdout=output_stream, stderr=open(os.devnull, 'wb'))
output_stream.seek(0)
for line in output_stream:
yield line
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--cache', help='Dump cache', action="store_true", default=False)
parser.add_argument('-i', '--infra', help='Dump infrastructure cache', action="store_true", default=False)
parser.add_argument('-s', '--stats', help='Dump stats', action="store_true", default=False)
parser.add_argument('-l', '--list-local-zones', help='List local Zones', action="store_true", default=False)
parser.add_argument('-I', '--list-insecure', help='List Domain-Insecure Zones', action="store_true", default=False)
parser.add_argument('-d', '--list-local-data', help='List local data', action="store_true", default=False)
parser.add_argument('-f', '--format', help='output format', action='store', choices=['json'], default='json')
args = parser.parse_args()
#
output = None
if args.cache:
output = list()
for line in unbound_control_reader('dump_cache'):
parts = re.split('^(\S+)\s+(?:([\d]*)\s+)?(IN)\s+(\S+)\s+(.*)$', line)
if line.find('IN') > -1 and not line.startswith('msg') and len(parts) > 5:
output.append({'host': parts[1], 'ttl': parts[2], 'type': parts[3], 'rrtype': parts[4], 'value': parts[5]})
elif args.infra:
output = list()
for line in unbound_control_reader('dump_infra'):
parts = line.split()
if len(parts) > 2:
record = {'ip': parts.pop(0), 'host': parts.pop(0)}
while len(parts) > 0:
key = parts.pop(0)
if key == 'lame':
record['lame'] = True
continue
record[key] = parts.pop(0)
output.append(record)
elif args.stats:
output = dict()
for line in unbound_control_reader('stats'):
full_key, value = line.split('=')
keys = full_key.split('.')
if keys[0] == 'histogram':
if 'histogram' not in output:
output['histogram'] = list()
output['histogram'].append({
'from': (int(keys[1]), int(keys[2])),
'to': (int(keys[4]), int(keys[5])),
'value': value.strip()
})
else:
ptr = output
while len(keys) > 0 :
key = keys.pop(0)
if len(keys) == 0:
ptr[key] = value.strip()
elif key not in ptr:
ptr[key] = dict()
ptr = ptr[key]
elif args.list_local_zones:
output = list()
for line in unbound_control_reader('list_local_zones'):
parts = line.split()
if len(parts) >= 2:
output.append({'zone': parts[0], 'type': parts[1]})
elif args.list_insecure:
output = list()
for line in unbound_control_reader('list_insecure'):
output.append(line)
elif args.list_local_data:
output = list()
for line in unbound_control_reader('list_local_data'):
parts = line.split()
if len(parts) >= 5:
output.append({'name': parts[0], 'ttl': parts[1], 'type': parts[2], 'rrtype': parts[3], 'value': parts[4]})
else:
parser.print_help()
sys.exit(1)
# flush output
if args.format == 'json':
print (json.dumps(output))
[dumpcache]
command:/usr/local/opnsense/scripts/unbound/unboundctlwrapper -c
parameters:
type:script_output
message:dumping name server cache
[dumpinfra]
command:/usr/local/opnsense/scripts/unbound/unboundctlwrapper -i
parameters:
type:script_output
message:dumping infrastructure cache
[stats]
command:/usr/local/opnsense/scripts/unbound/unboundctlwrapper -s
parameters:
type:script_output
message:loading stats
[listinsecure]
command:/usr/local/opnsense/scripts/unbound/unboundctlwrapper -I
parameters:
type:script_output
message:list isecure local zones
[listlocalzones]
command:/usr/local/opnsense/scripts/unbound/unboundctlwrapper -l
parameters:
type:script_output
message:list local zones
[listlocaldata]
command:/usr/local/opnsense/scripts/unbound/unboundctlwrapper -d
parameters:
type:script_output
message:list local data
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment