Skip to content

Commit

Permalink
Status_Traffic_Totals: vnstat 2.0 changes. Issue #9392
Browse files Browse the repository at this point in the history
Backend changes complete, still needs work in frontend to account for
differences in JSON output.

* Enable/disable is now stored in the config
* Added service and management for vnstatd daemon, which runs at all
times rather than through cron
* Moved functions and other shared code into a common .inc file
* Lots of other misc improvements
  • Loading branch information
jim-p committed Jan 2, 2020
1 parent 5457245 commit 716da29
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 210 deletions.
6 changes: 5 additions & 1 deletion net/pfSense-pkg-Status_Traffic_Totals/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# $FreeBSD$

PORTNAME= pfSense-pkg-Status_Traffic_Totals
PORTVERSION= 1.2.4
PORTVERSION= 2.2
CATEGORIES= net
MASTER_SITES= # empty
DISTFILES= # empty
Expand Down Expand Up @@ -32,6 +32,10 @@ do-install:
${MKDIR} ${STAGEDIR}${DATADIR}
${INSTALL_DATA} ${FILESDIR}${PREFIX}/pkg/Status_Traffic_Totals.xml \
${STAGEDIR}${PREFIX}/pkg
${INSTALL_DATA} ${FILESDIR}${PREFIX}/pkg/shortcuts/status_traffic_totals.inc \
${STAGEDIR}${PREFIX}/pkg
${INSTALL_DATA} ${FILESDIR}${PREFIX}/pkg/shortcuts/status_traffic_totals.sc.inc \
${STAGEDIR}${PREFIX}/pkg/shortcuts
${INSTALL_DATA} ${FILESDIR}/etc/inc/priv/status_traffic_totals.priv.inc \
${STAGEDIR}/etc/inc/priv
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/status_traffic_totals.php \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* traffic_totals.xml
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2015-2019 Rubicon Communications, LLC (Netgate)
* Copyright (c) 2015-2020 Rubicon Communications, LLC (Netgate)
* All rights reserved.
*
* Originally based upon pfBlocker by
Expand All @@ -30,12 +30,28 @@
]]>
</copyright>
<description>Traffic Totals Page</description>
<name>Traffic Totals</name>
<name>Traffic Totals</name>
<title>Status: Traffic Totals</title>
<include_file>/usr/local/pkg/status_traffic_totals.inc</include_file>
<menu>
<name>Traffic Totals</name>
<tooltiptext>Traffic totals per interface for different periods of time.</tooltiptext>
<tooltiptext>Traffic totals per interface for different periods of time.</tooltiptext>
<section>Status</section>
<url>/status_traffic_totals.php</url>
</menu>
<service>
<name>vnstatd</name>
<rcfile>vnstatd.sh</rcfile>
<executable>vnstatd</executable>
<description>Status Traffic Totals data collection daemon</description>
</service>
<custom_php_resync_config_command>
vnstat_sync();
</custom_php_resync_config_command>
<custom_php_install_command>
vnstat_install();
</custom_php_install_command>
<custom_php_deinstall_command>
vnstat_deinstall();
</custom_php_deinstall_command>
</packagegui>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

global $shortcuts;

$shortcuts['vnstat'] = array();
$shortcuts['vnstat']['main'] = "status_traffic_totals.php";
$shortcuts['vnstat']['service'] = "vnstatd";

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php
/*
* status_traffic_totals.php
*
* part of pfSense (https://www.pfsense.org)
* Copyright (c) 2008-2020 Rubicon Communications, LLC (Netgate)
* All rights reserved.
*
* originally part of m0n0wall (http://m0n0.ch/wall)
* Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
* All rights reserved.
*
* 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.
*/
require_once("service-utils.inc");
require_once("interfaces.inc");
require_once("ipsec.inc");

global $vnstat_dbdir, $vnstat_config;
$vnstat_dbdir = '/var/db/vnstat';
$vnstat_config = '/usr/local/etc/vnstat.conf';

function vnstat_write_conf() {
global $config, $vnstat_dbdir, $vnstat_config;
init_config_arr(array('installedpackages', 'traffictotals', 'config', 0));
$vnscfg =& $config['installedpackages']['traffictotals']['config'][0];
$startDay = isset($vnscfg['startday']) ? $vnscfg['startday'] : 1;

$vnstat_conf_file = <<<EOF
#########################################
# vnStat 2.2 config file #
# AUTOMATICALLY GENERATED. DO NOT EDIT! #
#########################################
# Location of the database directory
DatabaseDir "{$vnstat_dbdir}"
# On which day should months change
MonthRotate {$startDay}
# Image colors
CBackground "FFFFFF"
CEdge "AEAEAE"
CHeader "606060"
CHeaderTitle "FFFFFF"
CHeaderDate "FFFFFF"
CText "000000"
CLine "B0B0B0"
CLineL "-"
CRx "92CF00"
CTx "606060"
CRxD "-"
CTxD "-"
EOF;

if (file_put_contents($vnstat_config, $vnstat_conf_file) === FALSE) {
log_error("ERROR: Could not write {$vnstat_config}");
exit;
}
}

function vnstat_create_nic_dbs() {
//TODO: code that allows you to just add new interfaces (check exsisting first and compare)
//List can be obtained from vnstat --iflist.
foreach(vnstat_portlist() as $interface => $details) {
unset($test);
exec('/usr/local/bin/vnstat' .
' -i ' . escapeshellarg($details['if']) .
' --setalias ' . escapeshellarg($details['descr']) .
' --add', $test);

//TODO check output array for errors
//print_r($test);
}
restart_service_if_running('vnstatd');
}

function vnstat_delete_nic_dbs() {
global $vnstat_dbdir;
stop_service('vnstatd');
unlink_if_exists("{$vnstat_dbdir}/vnstat.db");
}

function vnstat_read() {
$json_string = '';
$fd = popen("/usr/local/bin/vnstat --json", "r");
$error = "";

$json_string = str_replace("\n", ' ', fgets($fd));
if(substr($json_string, 0, 5) === "Error") {
throw new Exception(substr($json_string, 7));
}

while (!feof($fd)) {
$json_string .= fgets($fd);

if(substr($json_string, 0, 5) === "Error") {
throw new Exception(str_replace("\n", ' ', substr($json_string, 7)));
break;
}
}

pclose($fd);
return $json_string;
}

function vnstat_install() {
global $config, $vnstat_dbdir;

// Remove cron job from old versions, it does not work this way any longer.
install_cron_job("/usr/local/bin/vnstat -u", false);

// Detect if enabled
init_config_arr(array('installedpackages', 'traffictotals', 'config', 0));
$vnscfg =& $config['installedpackages']['traffictotals']['config'][0];
try {
$data = vnstat_read();
// Enabled
$vnscfg['enabled'] = true;
} catch (Exception $e) {
if (isset($vnscfg['enabled'])) {
// Enabled but missing data, so reinitialize
vnstat_write_conf();
vnstat_create_nic_dbs();
} else {
// Disabled
}
}
}

function vnstat_deinstall() {
// Nothing yet
}

function vnstat_write_rcfile() {
global $g, $config, $vnstat_dbdir, $vnstat_config;
$rundir = "{$g['varrun_path']}/vnstat";
$pidfile = "{$rundir}/vnstat.pid";
$user = 'vnstat';
$group = 'vnstat';

$rc = array();
$rc['file'] = 'vnstatd.sh';
$rc['stop'] = '/usr/bin/killall vnstatd';
$rc['start'] = <<<EOD
if [ ! -d {$vnstat_dbdir} ]; then
install -d -o {$user} -g {$group} {$vnstat_dbdir};
fi
if [ ! -d "{$rundir}" ]; then
install -d -o {$user} -g {$group} {$rundir};
fi
/usr/local/sbin/vnstatd -d -p {$pidfile} --config {$vnstat_config}
EOD;
write_rcfile($rc);
}

function vnstat_portlist() {
global $config;
$portlist = $config['interfaces'];
$ifdescrs = get_configured_interface_with_descr();

if (ipsec_enabled()) {
$portlist['enc0']['if'] = "enc0";
$portlist['enc0']['descr'] = gettext("IPsec");
}

foreach (array('server', 'client') as $mode) {
if (is_array($config['openvpn']["openvpn-{$mode}"])) {
foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
if (!isset($setting['disable'])) {
$portlist['ovpn' . substr($mode, 0, 1) . $setting['vpnid']]['if'] = 'ovpn' . substr($mode, 0, 1) . $setting['vpnid'];
$portlist['ovpn' . substr($mode, 0, 1) . $setting['vpnid']]['descr'] = gettext("OpenVPN") . " " . $mode . ": ".htmlspecialchars($setting['description']);
}
}
}
}

foreach ($portlist as $interface => $details) {
if ($details['descr']) {
//do nothing
} elseif ($ifdescrs[$interface]) {
$portlist[$interface]['descr'] = $ifdescrs[$interface];
} else {
$portlist[$interface]['descr'] = $interface;
}
}

return $portlist;
}

function vnstat_sync() {
global $config;
init_config_arr(array('installedpackages', 'traffictotals', 'config', 0));
$vnscfg =& $config['installedpackages']['traffictotals']['config'][0];

if (isset($vnscfg['enabled'])) { // Enabled
//make the directory for the nterface databases
safe_mkdir('/var/db/vnstat');
//overwrite vnstat conf
vnstat_write_conf();
//loop through interfaces and create databases
vnstat_create_nic_dbs();
vnstat_write_rcfile();
restart_service('vnstatd');
} else {
// disable
//loop through interfaces and delete databases
vnstat_delete_nic_dbs();
unlink_if_exists('/usr/local/etc/rc.d/vnstatd.sh');
}
}

function vnstat_reset() {
//loop through interfaces and delete databases
vnstat_delete_nic_dbs();
//loop through interfaces and re-create databases
vnstat_create_nic_dbs();
}
Loading

0 comments on commit 716da29

Please sign in to comment.