From e72b4b344a3ef047c59cae8c1f946764a0a9f6dc Mon Sep 17 00:00:00 2001 From: doktornotor Date: Sat, 15 Aug 2015 15:44:46 +0200 Subject: bandwidthd - code style and major improvements - Fix copyright header and indentation - Reorder the GUI and split into logical sections - Improve descriptions - Move the menu entry to Diagnostics section, to match other similar packages (ntop, ntopng, darkstat etc.) - Add another menu entry to access bandwidthd graphs directly from menu - Add input validation --- config/bandwidthd/bandwidthd.xml | 300 +++++++++++++++++++++++++-------------- 1 file changed, 192 insertions(+), 108 deletions(-) (limited to 'config') diff --git a/config/bandwidthd/bandwidthd.xml b/config/bandwidthd/bandwidthd.xml index fc768761..59a08e94 100644 --- a/config/bandwidthd/bandwidthd.xml +++ b/config/bandwidthd/bandwidthd.xml @@ -3,57 +3,61 @@ - . - All rights reserved. - */ -/* ========================================================================== */ + bandwidthd.xml + part of pfSense (https://www.pfSense.org/) + Copyright (C) 2006 Scott Ullrich + Copyright (C) 2009 Bill Marquette + Copyright (C) 2012-2013 Phil Davis + Copyright (C) 2015 ESF, LLC + All rights reserved. +*/ +/* ====================================================================================== */ /* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: + 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. - 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. - 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. - */ -/* ========================================================================== */ + 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. +*/ +/* ====================================================================================== */ ]]> - Describe your package here - Describe your package requirements here - Currently there are no FAQ items provided. bandwidthd - 2.0.1_5 pkg v.0.4 - Bandwidthd + 0.6 + Diagnostics: Bandwidthd /pkg_edit.php?xml=bandwidthd.xml&id=0 /usr/local/pkg/bandwidthd.inc BandwidthD -
Services
+
Diagnostics
+ /bandwidthd/index.html +
+ + BandwidthD Settings + +
Diagnostics
/pkg_edit.php?xml=bandwidthd.xml&id=0
@@ -64,152 +68,232 @@ - BandwidthD + Settings /pkg_edit.php?xml=bandwidthd.xml&id=0 Access BandwidthD - /bandwidthd/index.html" target="_blank + /bandwidthd/index.html + enabled installedpackages->package->bandwidthd /usr/local/pkg/ - 0677 https://packages.pfsense.org/packages/config/bandwidthd/bandwidthd.inc - Enable bandwidthd + General Options + listtopic + + + Enable BandwidthD enable checkbox - Interface + BandwidthD Web Interface active_interface - The interface that bandwidthd will bind to. + Bind the BandwidthD web interface to IP address of the specified interface. interfaces_selection + (loopback|wan) lan - Subnet - subnets_custom - The subnet(s) on which bandwidthd will report. (separate with ';' for multiple subnets, e.g. 192.168.1.0/24;10.0.0.0/24) The ordinary subnet for the selected interface/s is automatically put in the config, do not specify it here. + Subnet(s) for Statistics Collection + interface_array + + + Note: PPPoE interfaces are NOT supported! + ]]> + + interfaces_selection + loopback + 3 + true + + + + Promiscuous + promiscuous + + + Note: If the interface is connected to a switch then the interface will only see the traffic on its port. + ]]> + + checkbox + + + Sensor ID + sensorid + + + (Using the sensor's fully qualified domain name is recommended.) + ]]> + input - Skip intervals - skipintervals - Number of intervals to skip between graphing. Default 0. Each interval is 200 seconds = 3 min 20 sec. + Graph Options + listtopic + + + Draw Graphs + drawgraphs + + + BandwidthD will use very little RAM and CPU if this option is disabled. + ]]> + + checkbox + on + + + Meta Refresh + meta_refresh + + + Default 150, use 0 to disable. + ]]> + input - Graph cutoff - graphcutoff - Graph cutoff is how many KB must be transferred by an IP before it is graphed. Default 1024. + Skip Intervals + skipintervals + + + Default 0. (Each interval is 200 seconds = 3 min 20 sec.) + ]]> + input - Promiscuous - promiscuous - Put interface in promiscuous mode to see traffic that may not be routing through the host machine.<br> - Note: If the interface is connected to a switch then the interface will only see the traffic on its port. - checkbox + Graph Cutoff + graphcutoff + + + Default 1024. + ]]> + + input - output_cdf + Output to CDF outputcdf - Log data to cdf files log*.cdf + Log data to CDF files log*.cdf checkbox - recover_cdf + Recover CDF recovercdf - Read back the cdf files on startup + Read back the CDF files on startup. checkbox - output PostgreSQL + Graph and Log Info + graph_log_info + + + If "Output to CDF" is on, then a cron job is added to rotate the log files at 00:01 each day. 6 log files are kept for each log frequency (daily, weekly, monthly, yearly).  + At the respective rotation intervals, the oldest log is deleted, the others are shuffled back and a new log is created.

+ + + + + + +
Data IntervalGraph SpanLog RotationLog File Name
Daily200 seconds2 days1 daylog.1.[0-5].cdf
Weekly10 minutes7 days7 dayslog.2.[0-5].cdf
Monthly1 hour35 days35 dayslog.3.[0-5].cdf
Yearly12 hours412.5 days412.5 days log.4.[0-5].cdf
+ ]]> +
+ info +
+ + PostgreSQL Options + listtopic + + + Output to PostgreSQL outputpostgresql - Log data to a PostgreSQL database.<br> - Get the postgreSQL schema and PHP files to display the results from: <a target="_new" href="https://github.com/individual-it/bandwidthd-pSQL-frontend">https://github.com/individual-it/bandwidthd-pSQL-frontend</a> + + + Get the PostgreSQL schema and PHP files to display the results from https://github.com/individual-it/bandwidthd-pSQL-frontend + ]]> + + postgresqlhost,postgresqldatabase,postgresqlusername,postgresqlpasswordenc checkbox - Database host + Database Host postgresqlhost - Hostname of the postgreSQL database server. + Hostname of the PostgreSQL database server. input - Database name + Database Name postgresqldatabase - Database on the postgreSQL database server. + Database on the PostgreSQL database server. input Database Username postgresqlusername - Username of the postgreSQL database server. + Username of the PostgreSQL database server. input Database Password - postgresqlpassword - Password of the postgreSQL database server. + postgresqlpasswordenc + Password of the PostgreSQL database server. password + base64 - sensor_id - sensorid - Arbitrary sensor name, I recommend the sensors fully qualified domain name. - input - - - Filter - filter - Libpcap format filter string used to control what bandwidthd sees. Please always include "ip" in the string to avoid strange problems. - input - - - Draw Graphs - drawgraphs - This defaults to true to graph the traffic bandwidthd is recording. Set this to false if you only want cdf output or you are using the database output option. Bandwidthd will use very little RAM and CPU if this is set to false. - checkbox - on - - - Meta Refresh - meta_refresh - Sets the interval (seconds) at which the browser graph display refreshes (default 150, use 0 to disable). - input - - - Graph and Log Info - graph_log_info - If draw graphs is on, then the daily report and graph html data is regenerated every (skip intervals + 1) * 200 seconds. The data volumes in the report are for the same period as the span of the graph.<br> - If output_cdf is on, then a cron job is added to rotate the log files at 00:01 each day. 6 log files are kept for each log frequency (daily, weekly, monthly, yearly). At the respective rotation intervals, the oldest log is deleted, the others are shuffled back and a new log is created.<br> - <table cellpadding=1 cellspacing=0 style="text-align: left;"> <tbody> - <tr><th> </th><th> Data Interval </th><th> Graph Span </th><th> Log Rotation </th><th> Log File Name </th></tr> - <tr><th> Daily </th><td> 200 seconds </td><td> 2 days </td><td> 1 day </td><td> log.1.[0-5].cdf </td></tr> - <tr><th> Weekly </th><td> 10 minutes </td><td> 7 days </td><td> 7 days </td><td> log.2.[0-5].cdf </td></tr> - <tr><th> Monthly </th><td> 1 hour </td><td> 35 days </td><td> 35 days </td><td> log.3.[0-5].cdf </td></tr> - <tr><th> Yearly </th><td> 12 hours </td><td> 412.5 days </td><td> 412.5 days </td><td> log.4.[0-5].cdf </td></tr> - </tbody> </table> + Advanced Filter + advfilter + + pcap-filter(7) for documentation. +
+ NOTE: You should always specify "ip" in the filter to avoid strange results.

+ Example: We only want to account for a certain IP.
+ Filter expression: ip and host 192.0.2.1

+ WARNING: You are completely on your own with this! If misconfigured, BandwidthD will malfunction or even not start at all. + ]]>
- info + textarea + base64 + 65 + 1 +
bandwidthd_install_config(); + bandwidthd_upgrade_config(); bandwidthd_install_config(); bandwidthd_install_deinstall(); + + bandwidthd_validate_input($_POST, $input_errors); +
-- cgit v1.2.3 From 108afc97e279cca541b313fb8c4a299e98d8c99e Mon Sep 17 00:00:00 2001 From: doktornotor Date: Sat, 15 Aug 2015 15:57:12 +0200 Subject: bandwidthd - code style and major improvements - Update copyright headers - Fix code style and indentation - Provide input validation for most of the configuration options -- Check for IPv4 configured on selected interfaces -- Check for PPPoE type interfaces -- Require relevant options to be filled in when PostgreSQL is enabled (also check that the hostname is valid) -- Require sane numeric input for graphs options -- Limit Sensor ID to sane chars - PostgreSQL DB password is now base64-encoded to deal with special chars and not break config.xml - The filtering moved to advanced options, also base64-encoded - The stats interface(s) are now a multiselect instead of asking users to type subnets directly - Upgrade function provided to preserve the old settings - Use {start,stop,restart}_service() functions instead of calling the rc script directly - Probably other small things... --- config/bandwidthd/bandwidthd.inc | 420 ++++++++++++++++++++------------------- 1 file changed, 220 insertions(+), 200 deletions(-) (limited to 'config') diff --git a/config/bandwidthd/bandwidthd.inc b/config/bandwidthd/bandwidthd.inc index b57b2732..6edd255b 100644 --- a/config/bandwidthd/bandwidthd.inc +++ b/config/bandwidthd/bandwidthd.inc @@ -1,9 +1,11 @@ " . $bandwidthd_index_file); } - if (($bandwidthd_enable) && ($output_cdf)) { + /* Cron job for graphs */ + if (($bandwidthd_config['enable']) && ($output_cdf)) { // Use cron job to rotate logs every day at 00:01 install_cron_job("/bin/kill -HUP `cat /var/run/bandwidthd.pid`", true, "1", "0"); - } - else - { - // Remove the cron job, if it is there + } else { + // Remove the cron job if it is there install_cron_job("/bin/kill -HUP `cat /var/run/bandwidthd.pid`", false); } + conf_mount_ro(); - stop_service("bandwidthd"); - if ($bandwidthd_enable) { - start_service("bandwidthd"); + + /* Restart the service if enabled; otherwise stop it */ + if ($bandwidthd_config['enable']) { + restart_service("bandwidthd"); + } else { + stop_service("bandwidthd"); + } +} + +function bandwidthd_upgrade_config() { + global $config, $changes; + // The 'Advanced Filter' options and 'Database Password' are now base64-encoded + // in order to now break config.xml if they contain some special chars. + // If this is an old config, pick up the original unencoded values and upgrade + $changes = 0; + $bandwidthd_config = $config['installedpackages']['bandwidthd']['config'][0]; + if (isset($bandwidthd_config['filter'])) { + $config['installedpackages']['bandwidthd']['config'][0]['advfilter'] = base64_encode($bandwidthd_config['filter']); + unset($config['installedpackages']['bandwidthd']['config'][0]['filter']); + $changes++; + } + if (isset($bandwidthd_config['postgresqlpassword'])) { + $config['installedpackages']['bandwidthd']['config'][0]['postgresqlpasswordenc'] = base64_encode($bandwidthd_config['postgresqlpassword']); + unset($config['installedpackages']['bandwidthd']['config'][0]['postgresqlpassword']); + $changes++; + } + // Subnet(s) for Statistics Collection - convert old subnets_custom to interface_array here as well. + if (isset($bandwidthd_config['subnets_custom'])) { + $i = 0; + $subnets_custom = explode(';', $bandwidthd_config['subnets_custom']); + // For each configured interface on this box... + $iflist = get_configured_interface_list_by_realif(); + foreach ($iflist as $if) { + // first, gets its subnet... + $sn = gen_subnet(get_interface_ip($if), get_interface_subnet($if)); + foreach ($subnets_custom as $subnet_custom) { + // next, strip the subnet mask from the old settings value... + $subnet_custom = substr($subnet_custom, 0, strpos($subnet_custom, '/')); + // next, try to match the originally configured subnet against configured interfaces... + if ($subnet_custom != "" && $subnet_custom == $sn) { + // skip PPPoE interfaces if any... + $realif = get_real_interface($if); + if (!preg_match("/pppoe[0-9]+/i", $realif)) { + // and finally, set the new config value for config.xml + echo $if; + $config['installedpackages']['bandwidthd']['config'][0]['interface_array'][$i] = $if; + $i++; + } + } + } + } + // Remove the old settings value now that we are done with config upgrade + unset($config['installedpackages']['bandwidthd']['config'][0]['subnets_custom']); + } + // Write the upgraded config.xml if something changed + write_config("[bandwidthd] Upgraded old package configuration."); +} + +function bandwidthd_validate_input($post, &$input_errors) { + if ($post['active_interface']) { + $realif = get_real_interface($post['active_interface']); + $ip = find_interface_ip($realif); + if (!is_ipaddrv4($ip)) { + // IPv6-only interfaces are not supported + $errif = convert_friendly_interface_to_friendly_descr($post['active_interface']); + $input_errors[] = gettext("The \"{$errif}\" interface selected under 'BandwidthD Web Interface' has no IPv4 configured. Configured IPv4 is required."); + } + } else { + $input_errors[] = "You must select the 'BandwidthD Web Interface'."; + } + // bandwidthd does not work with PPPoE; IPv6 is also not supported + if ($post['interface_array']) { + foreach ($post['interface_array'] as $ifname) { + $realif = get_real_interface($ifname); + $ip = find_interface_ip($realif); + $errif = convert_friendly_interface_to_friendly_descr($ifname); + if (preg_match("/pppoe[0-9]+/i", $realif)) { + $input_errors[] = gettext("Sorry, BandwidthD does not support PPPoE interfaces. Remove \"{$errif}\" from 'Subnet(s) for Statistics Collection'."); + } + if (!is_ipaddrv4($ip)) { + $input_errors[] = gettext("The \"{$errif}\" interface selected under 'Subnet(s) for Statistics Collection' has no IPv4 configured. Configured IPv4 is required."); + } + } + } else { + $input_errors[] = "You must select at least on interface under 'Subnet(s) for Statistics Collection'."; + } + // Only support sane characters in Sensor ID + if ($post['sensorid']) { + if ((!is_hostname($post['sensorid'])) && !preg_match("/^[a-zA-Z0-9\-\=\(\):. ]*$/", $post['sensorid'])) { + $input_errors[] .= gettext('Sensor ID must be either a hostname or a string which may only contain characters matching ^[a-zA-Z0-9\-\(\):. ]*$ regexp.'); + } + } + if (($post['meta_refresh']) && (!is_numericint($post['meta_refresh']))) { + $input_errors[] = gettext("The value for 'Meta Refresh' must be a positive integer."); + } + if (($post['skipintervals']) && (!is_numericint($post['skipintervals']))) { + $input_errors[] = gettext("The value for 'Skip Intervals' must be a positive integer."); + } + if (($post['graphcutoff']) && (!is_numericint($post['graphcutoff']))) { + $input_errors[] = gettext("The value for 'Graph Cutoff' must be a positive integer."); + } + if ($post['outputpostgresql']) { + if (!$post['postgresqlhost']) { + $input_errors[] = gettext("The value for 'Database Host' must not be empty when 'Output to PostgreSQL' is enabled."); + } + if (!$post['postgresqldatabase']) { + $input_errors[] = gettext("The value for 'Database Name' must not be empty when 'Output to PostgreSQL' is enabled."); + } + if (!$post['postgresqlusername']) { + $input_errors[] = gettext("The value for 'Database User' must not be empty when 'Output to PostgreSQL' is enabled."); + } + if (!$post['postgresqlpasswordenc']) { + $input_errors[] = gettext("The value for 'Database Password' must not be empty when 'Output to PostgreSQL' is enabled."); + } + } + if (($post['postgresqlhost']) && (!is_hostname($post['postgresqlhost']))) { + $input_errors[] = gettext("The value for 'Database Host' must be a valid hostname or IPv4."); + } + if (($post['advfilter']) && !preg_match("/^[a-zA-Z0-9\+\-\=\(\):. ]*$/", $post['advfilter'])) { + $input_errors[] = gettext('Advanced traffic filtering options may only contain characters matching ^[a-zA-Z0-9\+\-\=\(\):. ]*$ regexp.'); } } -- cgit v1.2.3 From b27cc72bf237732bfc6222682499f3c5f39480fe Mon Sep 17 00:00:00 2001 From: doktornotor Date: Sat, 15 Aug 2015 16:26:31 +0200 Subject: bandwidthd - code style and major improvements Only write upgraded config if there was something actually upgraded. --- config/bandwidthd/bandwidthd.inc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'config') diff --git a/config/bandwidthd/bandwidthd.inc b/config/bandwidthd/bandwidthd.inc index 6edd255b..9364cbbc 100644 --- a/config/bandwidthd/bandwidthd.inc +++ b/config/bandwidthd/bandwidthd.inc @@ -335,9 +335,9 @@ function bandwidthd_upgrade_config() { $realif = get_real_interface($if); if (!preg_match("/pppoe[0-9]+/i", $realif)) { // and finally, set the new config value for config.xml - echo $if; $config['installedpackages']['bandwidthd']['config'][0]['interface_array'][$i] = $if; $i++; + $changes++; } } } @@ -346,7 +346,9 @@ function bandwidthd_upgrade_config() { unset($config['installedpackages']['bandwidthd']['config'][0]['subnets_custom']); } // Write the upgraded config.xml if something changed - write_config("[bandwidthd] Upgraded old package configuration."); + if ($changes > 0 ) { + write_config("[bandwidthd] Upgraded old package configuration."); + } } function bandwidthd_validate_input($post, &$input_errors) { -- cgit v1.2.3 From 3fcba04bc283a6667b75535be02befcd5fcd2141 Mon Sep 17 00:00:00 2001 From: doktornotor Date: Sat, 15 Aug 2015 23:32:33 +0200 Subject: bandwidthd - code style and major improvements Fix indentation. --- config/bandwidthd/bandwidthd.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'config') diff --git a/config/bandwidthd/bandwidthd.xml b/config/bandwidthd/bandwidthd.xml index 59a08e94..1668f748 100644 --- a/config/bandwidthd/bandwidthd.xml +++ b/config/bandwidthd/bandwidthd.xml @@ -61,10 +61,10 @@ /pkg_edit.php?xml=bandwidthd.xml&id=0 - bandwidthd - bandwidthd.sh - bandwidthd - BandwidthD bandwidth monitoring daemon + bandwidthd + bandwidthd.sh + bandwidthd + BandwidthD bandwidth monitoring daemon -- cgit v1.2.3