diff options
Diffstat (limited to 'config/bandwidthd/bandwidthd.inc')
-rw-r--r-- | config/bandwidthd/bandwidthd.inc | 190 |
1 files changed, 162 insertions, 28 deletions
diff --git a/config/bandwidthd/bandwidthd.inc b/config/bandwidthd/bandwidthd.inc index 34532c18..00e3cd28 100644 --- a/config/bandwidthd/bandwidthd.inc +++ b/config/bandwidthd/bandwidthd.inc @@ -46,6 +46,8 @@ function bandwidthd_install_deinstall() { exec("rm -f /usr/local/etc/rc.d/bandwidthd*"); exec("rm -rf " . PKG_BANDWIDTHD_BASE . "/htdocs"); exec("rm -f /usr/local/www/bandwidthd"); + // Remove the cron job, if it is there + install_cron_job("/bin/kill -HUP `cat /var/run/bandwidthd.pid`", false); conf_mount_ro(); config_unlock(); } @@ -54,50 +56,76 @@ function bandwidthd_install_config() { global $config, $g; /* bandwidthd doesn't have a way to pass a custom config path, unfortunately */ + /* the conf file must be ./etc/bandwidthd.conf relative to the current dir */ + $bandwidthd_base_dir = PKG_BANDWIDTHD_BASE; $bandwidthd_config_dir = PKG_BANDWIDTHD_BASE . "/etc"; conf_mount_rw(); config_lock(); /* user defined values */ - $meta_refresh = $config['installedpackages']['bandwidthd']['config'][0]['meta_refresh']; + $bandwidthd_config = $config['installedpackages']['bandwidthd']['config'][0]; + $meta_refresh = $bandwidthd_config['meta_refresh']; if($meta_refresh) $meta_refresh = "meta_refresh $meta_refresh\n"; - $graph = $config['installedpackages']['bandwidthd']['config'][0]['drawgraphs']; + $graph = $bandwidthd_config['drawgraphs']; if($graph) $graph = "graph true\n"; else $graph = "graph false\n"; - $filter_text = $config['installedpackages']['bandwidthd']['config'][0]['filter']; + $filter_text = $bandwidthd_config['filter']; if($filter_text) $filter_text = "filter $filter_text\n"; - $recover_cdf = $config['installedpackages']['bandwidthd']['config'][0]['recovercdf']; + $recover_cdf = $bandwidthd_config['recovercdf']; if($recover_cdf) $recover_cdf = "recover_cdf true\n"; - $output_cdf = $config['installedpackages']['bandwidthd']['config'][0]['outputcdf']; + $output_cdf = $bandwidthd_config['outputcdf']; if($output_cdf) - $output_cdf = "output_cdf true\n"; - $promiscuous = $config['installedpackages']['bandwidthd']['config'][0]['promiscuous']; + $output_cdf_string = "output_cdf true\n"; + else + $output_cdf_string = ""; + + $output_postgresql = $bandwidthd_config['outputpostgresql']; + $postgresql_host = $bandwidthd_config['postgresqlhost']; + $postgresql_database = $bandwidthd_config['postgresqldatabase']; + $postgresql_username = $bandwidthd_config['postgresqlusername']; + $postgresql_password = $bandwidthd_config['postgresqlpassword']; + $postgresql_string = ""; + if($output_postgresql) { + if ($postgresql_host && $postgresql_username && $postgresql_database && $postgresql_password) + $postgresql_string = "pgsql_connect_string \"user = $postgresql_username dbname = $postgresql_database password = $postgresql_password host = $postgresql_host\"\n"; + else + log_error("You have to specify the postgreSQL Host, Database, Username and Password. Exiting."); + } + + $sensor_id = $bandwidthd_config['sensorid']; + + if($sensor_id) + $sensor_id_string = "sensor_id \"$sensor_id\""; + else + $sensor_id_string = ""; + + $promiscuous = $bandwidthd_config['promiscuous']; if($promiscuous) $promiscuous = "promiscuous true\n"; else $promiscuous = "promiscuous false\n"; - $graph_cutoff = $config['installedpackages']['bandwidthd']['config'][0]['graphcutoff']; + $graph_cutoff = $bandwidthd_config['graphcutoff']; if($graph_cutoff) $graph_cutoff = "graph_cutoff $graph_cutoff\n"; - $skip_intervals = $config['installedpackages']['bandwidthd']['config'][0]['skipintervals']; + $skip_intervals = $bandwidthd_config['skipintervals']; if($skip_intervals) $skip_intervals = "skip_intervals $skip_intervals\n"; - if($config['installedpackages']['bandwidthd']['config'][0]['active_interface']){ - $ifdescrs = array($config['installedpackages']['bandwidthd']['config'][0]['active_interface']); + if($bandwidthd_config['active_interface']){ + $ifdescrs = array($bandwidthd_config['active_interface']); } else { log_error("You should specify an interface for bandwidthd to listen on. Exiting."); } - $subnets_custom = explode(';',str_replace(' ','',$config['installedpackages']['bandwidthd']['config'][0]['subnets_custom'])); + $subnets_custom = explode(';',str_replace(' ','',$bandwidthd_config['subnets_custom'])); /* initialize to "" */ $subnets = ""; @@ -143,7 +171,7 @@ function bandwidthd_install_config() { # This file was automatically generated by the pfSense # package management system. Changing this file # will lead to it being overwritten again when -# the package manage resyncs. +# the package manager resyncs. # #################################################### # Bandwidthd.conf @@ -178,11 +206,19 @@ $graph_cutoff $promiscuous #Log data to cdf file htdocs/log.cdf -$output_cdf +$output_cdf_string #Read back the cdf file on startup $recover_cdf +# Standard postgres connect string, just like php, see postgres docs for +# details +$postgresql_string + +# Arbitrary sensor name, I recommend the sensors fully qualified domain +# name +$sensor_id_string + #Libpcap format filter string used to control what bandwidthd sees #Please always include "ip" in the string to avoid strange problems $filter_text @@ -206,25 +242,123 @@ EOF; fwrite($fd, $config_file); fclose($fd); - /* write out rc.d start/stop file */ - write_rcfile(array( - "file" => "bandwidthd.sh", - "start" => "/usr/local/bandwidthd/bandwidthd {$bandwidthd_config_dir}/bandwidthd.conf", - "stop" => "/usr/bin/killall bandwidthd" - ) - ); + if ($g['platform'] == 'nanobsd') { + $bandwidthd_nano_dir = "/var/bandwidthd"; + $bandwidthd_htdocs_dir = $bandwidthd_nano_dir . "/htdocs"; + if (!is_dir($bandwidthd_nano_dir)) { + if (file_exists($bandwidthd_nano_dir)) { + unlink($bandwidthd_nano_dir); + } + mkdir($bandwidthd_nano_dir); + } + } else { + $bandwidthd_htdocs_dir = $bandwidthd_base_dir . "/htdocs"; + } - exec("rm /usr/local/www/bandwidthd"); - exec("/bin/ln -s " . PKG_BANDWIDTHD_BASE . "/htdocs /usr/local/www/bandwidthd"); + $rc = array(); + $rc['file'] = 'bandwidthd.sh'; + $rc['stop'] = <<<EOD +/usr/bin/killall bandwidthd +EOD; + + // If this is an old config before the enable checkbox was added, then enable by default + $bandwidthd_enable = (!isset($bandwidthd_config['enable']) || ($bandwidthd_config['enable'])); + if ($bandwidthd_enable) { + if ($g['platform'] == 'nanobsd') { + // On nanobsd, /var/bandwidthd is created. + // In that is a real /var/bandwidth/htdocs, where the graph data is written + // A soft link to the real bandwidth program is made - /var/bandwidthd/bandwidthd + // A soft link to the etc folder with the conf file is made - /var/bandwidthd/etc + // bandwidthd is started from /var/bandwidthd with the current dir /var/bandwidth + // This way, it: + // looks in ./etc for the conf file + // writes graph files in ./htdocs + // writes cdf log files (if selected in the config) to ./ + // All of this is on the /var filesystem, which is a read-write memory disk on nanobsd + $rc['start'] = <<<EOD +if [ ! -d "{$bandwidthd_nano_dir}" ] ; then + if [ -e "{$bandwidthd_nano_dir}" ] ; then + /bin/rm -f {$bandwidthd_nano_dir} + fi + /bin/mkdir -p {$bandwidthd_nano_dir} +fi +if [ ! -d "{$bandwidthd_htdocs_dir}" ] ; then + if [ -e "{$bandwidthd_htdocs_dir}" ] ; then + /bin/rm -f {$bandwidthd_htdocs_dir} + fi + /bin/mkdir -p {$bandwidthd_htdocs_dir} +fi +if [ ! -L "{$bandwidthd_nano_dir}/bandwidthd" ] ; then + if [ -e "{$bandwidthd_nano_dir}/bandwidthd" ] ; then + /bin/rm -Rf {$bandwidthd_nano_dir}/bandwidthd + fi + /bin/ln -s {$bandwidthd_base_dir}/bandwidthd {$bandwidthd_nano_dir}/bandwidthd +fi +if [ ! -L "{$bandwidthd_nano_dir}/etc" ] ; then + if [ -e "{$bandwidthd_nano_dir}/etc" ] ; then + /bin/rm -Rf {$bandwidthd_nano_dir}/etc + fi + /bin/ln -s {$bandwidthd_config_dir} {$bandwidthd_nano_dir}/etc +fi +if [ ! -f "{$bandwidthd_htdocs_dir}/legend.gif" ] ; then + /bin/cp {$bandwidthd_base_dir}/htdocs/legend.gif {$bandwidthd_htdocs_dir} +fi +if [ ! -f "{$bandwidthd_htdocs_dir}/logo.gif" ] ; then + /bin/cp {$bandwidthd_base_dir}/htdocs/logo.gif {$bandwidthd_htdocs_dir} +fi +cd {$bandwidthd_nano_dir} +{$bandwidthd_nano_dir}/bandwidthd +cd - +EOD; + } else { + $rc['start'] = <<<EOD +/usr/local/bandwidthd/bandwidthd +EOD; + } + } else { + // bandwidthd is disabled, so do not put any real start commands in the script. + // This effectively disables it but keeps all the files in place (e.g. saved logs) ready to reload when it is enabled. + $rc['start'] = "return"; + } + + /* write out rc.d start/stop file */ + write_rcfile($rc); - exec("echo \"Please start bandwidthd to populate this directory.\" > " . PKG_BANDWIDTHD_BASE . "/htdocs/index.html"); + if (!is_dir($bandwidthd_htdocs_dir)) { + if (file_exists($bandwidthd_htdocs_dir)) { + unlink($bandwidthd_htdocs_dir); + } + mkdir($bandwidthd_htdocs_dir); + } + $bandwidthd_www_link = $g["www_path"] . "/bandwidthd"; + if (!is_link($bandwidthd_www_link)) { + if (file_exists($bandwidthd_www_link)) { + // It is a file and not a link - clean it up. + unlink($bandwidthd_www_link); + } + symlink($bandwidthd_htdocs_dir, $bandwidthd_www_link); + } + $bandwidthd_index_file = $bandwidthd_htdocs_dir . "/index.html"; + if (!file_exists($bandwidthd_index_file)) { + exec("echo \"Please start bandwidthd to populate this directory.\" > " . $bandwidthd_index_file); + } + + if (($bandwidthd_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 + install_cron_job("/bin/kill -HUP `cat /var/run/bandwidthd.pid`", false); + } conf_mount_ro(); config_unlock(); - stop_service("bandwidthd"); - start_service("bandwidthd"); - + if ($bandwidthd_enable) { + start_service("bandwidthd"); + } } -?>
\ No newline at end of file +?> |