From eeb3d58f5b526dcaad4c8d8017c799d2ef474615 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Fri, 13 Jul 2018 17:08:00 -0500 Subject: [PATCH] Improved Logging and Debugging (#8870) Use Log facility when Laravel is booted. Update init.php so we can easily boot Laravel for CLI scripts. (and just Eloquent, but that may go away) Move all debug setup into set_debug() function and use that across all scripts. Log Laravel database queries. Send debug output to librenms log file when enabling debug in the webui. Allow for colorized Log CLI output. (currently will leave % tags in log file output) ** Needs testing and perhaps tweaking still. DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply `, i.e `./scripts/github-apply 5926` --- LibreNMS/Component.php | 2 +- LibreNMS/Device/Sensor.php | 2 +- LibreNMS/Util/CliColorFormatter.php | 58 +++++ LibreNMS/Util/FileLock.php | 2 +- alerts.php | 13 +- app/Providers/AppServiceProvider.php | 3 +- check-services.php | 13 +- discovery.php | 13 +- html/csv.php | 16 +- html/graph-realtime.php | 2 +- html/graph.php | 16 +- html/includes/api_functions.inc.php | 2 +- .../graphs/accesspoints/nummonbssid.inc.php | 2 +- .../application/mysql_query_cache.inc.php | 2 +- html/includes/graphs/device/storage.inc.php | 16 +- html/includes/graphs/mempool/usage.inc.php | 48 ++-- html/includes/print-accesspoint.inc.php | 6 +- html/includes/print-debug.php | 86 ------- html/includes/print-interface.inc.php | 2 +- html/includes/table/address-search.inc.php | 2 +- html/legacy_index.php | 33 +-- html/netcmd.php | 10 +- html/network-map.php | 11 +- html/pages/bill.inc.php | 8 +- html/pages/bill/transfer.inc.php | 2 +- html/pages/device/latency.inc.php | 4 +- html/pages/device/performance.inc.php | 2 +- html/pages/device/port.inc.php | 4 +- html/pages/device/port/macaccounting.inc.php | 20 +- html/pages/device/port/vlans.inc.php | 4 +- html/pages/ports/graph.inc.php | 2 +- html/pdf.php | 16 +- includes/billing.php | 32 +-- includes/common.php | 30 +-- includes/dbFacile.php | 44 ++-- includes/defaults.inc.php | 1 - includes/device-groups.inc.php | 9 +- includes/discovery/cisco-otv.inc.php | 8 +- includes/discovery/cisco-pw.inc.php | 17 +- includes/discovery/cisco-vrf-lite.inc.php | 2 +- includes/discovery/fdb-table/ios.inc.php | 2 +- includes/discovery/functions.inc.php | 2 +- .../discovery/loadbalancers/f5-gtm.inc.php | 2 +- .../discovery/loadbalancers/f5-ltm.inc.php | 110 ++++----- includes/discovery/mempools/pbn.inc.php | 2 +- includes/discovery/ntp/cisco.inc.php | 6 +- includes/discovery/ports-stack.inc.php | 1 + includes/discovery/ports.inc.php | 14 -- .../discovery/sensors/current/ciscosb.inc.php | 2 +- .../discovery/sensors/dbm/ciscosb.inc.php | 4 +- .../discovery/sensors/entity-sensor.inc.php | 13 +- .../discovery/sensors/fanspeed/linux.inc.php | 2 +- .../discovery/sensors/frequency/linux.inc.php | 32 +-- .../discovery/sensors/power/ciscosb.inc.php | 4 +- includes/discovery/sensors/state/boss.inc.php | 6 +- .../discovery/sensors/state/cisco.inc.php | 2 +- .../discovery/sensors/state/ciscosb.inc.php | 4 +- .../discovery/sensors/state/linux.inc.php | 90 ++++---- .../sensors/temperature/ciscosb.inc.php | 2 +- .../discovery/sensors/voltage/ciscosb.inc.php | 2 +- .../discovery/sensors/voltage/linux.inc.php | 44 ++-- includes/discovery/stp.inc.php | 18 +- includes/discovery/vlans.inc.php | 5 +- includes/discovery/vmware-vminfo.inc.php | 2 +- includes/functions.php | 218 +++++++++++++----- includes/init.php | 13 +- .../polling/applications/fail2ban.inc.php | 2 +- includes/polling/applications/zfs.inc.php | 160 ++++++------- includes/polling/bgp-peers.inc.php | 2 - includes/polling/cisco-cbqos.inc.php | 2 +- includes/polling/cisco-cef.inc.php | 2 +- includes/polling/cisco-otv.inc.php | 2 +- includes/polling/cisco-vpdn.inc.php | 2 +- .../polling/entity-physical/state.inc.php | 2 +- includes/polling/functions.inc.php | 3 +- .../polling/mempools/fiberhome-switch.inc.php | 2 +- includes/polling/mempools/hpGlobal.inc.php | 2 +- includes/polling/mempools/hpLocal.inc.php | 2 +- includes/polling/mempools/hrstorage.inc.php | 2 +- .../polling/mempools/ironware-dyn.inc.php | 2 +- includes/polling/mempools/junos.inc.php | 2 +- includes/polling/mempools/pbn-mem.inc.php | 2 +- includes/polling/mempools/vrp.inc.php | 2 +- includes/polling/ntp/cisco.inc.php | 2 +- includes/polling/ports.inc.php | 6 +- includes/polling/storage/eql-storage.inc.php | 4 +- includes/polling/storage/hpe-ilo.inc.php | 2 +- includes/polling/storage/hrstorage.inc.php | 2 +- .../polling/storage/intelliflash-pl.inc.php | 2 +- .../polling/storage/intelliflash-pr.inc.php | 2 +- .../polling/storage/netapp-storage.inc.php | 2 +- includes/polling/storage/nimbleos.inc.php | 2 +- includes/polling/storage/ucd-dsktable.inc.php | 2 +- includes/polling/stp.inc.php | 22 +- poll-billing.php | 20 +- poller.php | 13 +- scripts/new-os.php | 9 +- scripts/test-alert.php | 9 +- scripts/test-template.php | 9 +- snmptrap.php | 11 +- 100 files changed, 710 insertions(+), 769 deletions(-) create mode 100644 LibreNMS/Util/CliColorFormatter.php delete mode 100644 html/includes/print-debug.php diff --git a/LibreNMS/Component.php b/LibreNMS/Component.php index f90710cfe9..d8d3b9cd65 100644 --- a/LibreNMS/Component.php +++ b/LibreNMS/Component.php @@ -294,7 +294,7 @@ class Component $OLD = $this->getComponents($device_id); // Loop over each component. - foreach ($ARRAY as $COMPONENT => $AVP) { + foreach ((array)$ARRAY as $COMPONENT => $AVP) { // Make sure the component already exists. if (!isset($OLD[$device_id][$COMPONENT])) { // Error. Component doesn't exist in the database. diff --git a/LibreNMS/Device/Sensor.php b/LibreNMS/Device/Sensor.php index 7b12e3e252..20b42b5f8a 100644 --- a/LibreNMS/Device/Sensor.php +++ b/LibreNMS/Device/Sensor.php @@ -304,7 +304,7 @@ class Sensor implements DiscoveryModule, PollerModule } // pre-fetch all standard sensors - $standard_sensors = call_user_func_array('array_merge', $sensors); + $standard_sensors = collect($sensors)->flatten(1)->all(); $pre_fetch = self::fetchSnmpData($os->getDevice(), $standard_sensors); // poll standard sensors diff --git a/LibreNMS/Util/CliColorFormatter.php b/LibreNMS/Util/CliColorFormatter.php new file mode 100644 index 0000000000..35be8f553d --- /dev/null +++ b/LibreNMS/Util/CliColorFormatter.php @@ -0,0 +1,58 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2018 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Util; + +class CliColorFormatter extends \Monolog\Formatter\LineFormatter +{ + private $console_color; + private $console; + + public function __construct() + { + $this->console_color = new \Console_Color2(); + $this->console = \App::runningInConsole(); + + parent::__construct( + "%message%\n", + null, + true + ); + } + + public function format(array $record) + { + // only format messages where color is enabled + if (isset($record['context']['color']) && $record['context']['color']) { + if ($this->console) { + $record['message'] = $this->console_color->convert($record['message']); + } else { + $record['message'] = $this->console_color->strip($record['message']); + } + } + + return parent::format($record); + } +} diff --git a/LibreNMS/Util/FileLock.php b/LibreNMS/Util/FileLock.php index 3ab083d2ca..e27ca7c553 100644 --- a/LibreNMS/Util/FileLock.php +++ b/LibreNMS/Util/FileLock.php @@ -63,7 +63,7 @@ class FileLock implements Lock return; } - if ($this->handle !== false) { + if (is_resource($this->handle)) { flock($this->handle, LOCK_UN); fclose($this->handle); } diff --git a/alerts.php b/alerts.php index c3a8038e88..09b519a46a 100755 --- a/alerts.php +++ b/alerts.php @@ -34,19 +34,8 @@ $options = getopt('d::'); $alerts_lock = FileLock::lockOrDie('alerts'); -if (isset($options['d'])) { +if (set_debug(isset($options['d']))) { echo "DEBUG!\n"; - $debug = true; - ini_set('display_errors', 1); - ini_set('display_startup_errors', 1); - ini_set('log_errors', 1); - ini_set('error_reporting', 1); -} else { - $debug = false; - // ini_set('display_errors', 0); - ini_set('display_startup_errors', 0); - ini_set('log_errors', 0); - // ini_set('error_reporting', 0); } if (!defined('TEST') && $config['alert']['disable'] != 'true') { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 4f7215c84d..87d54a9019 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -38,7 +38,8 @@ class AppServiceProvider extends ServiceProvider Config::load(); // direct log output to librenms.log - Log::useFiles(Config::get('log_file', base_path('logs/librenms.log'))); + Log::getMonolog()->popHandler(); // remove existing errorlog logger + Log::useFiles(Config::get('log_file', base_path('logs/librenms.log')), 'error'); // Blade directives (Yucky because of < L5.5) diff --git a/check-services.php b/check-services.php index 0270b6c6aa..9d6d92b3f7 100755 --- a/check-services.php +++ b/check-services.php @@ -17,19 +17,8 @@ $init_modules = array(); require __DIR__ . '/includes/init.php'; $options = getopt('d::h:f:;'); -if (isset($options['d'])) { +if (set_debug(isset($options['d']))) { echo "DEBUG!\n"; - $debug = true; - ini_set('display_errors', 1); - ini_set('display_startup_errors', 1); - ini_set('log_errors', 1); - ini_set('error_reporting', 1); -} else { - $debug = false; - // ini_set('display_errors', 0); - ini_set('display_startup_errors', 0); - ini_set('log_errors', 0); - // ini_set('error_reporting', 0); } if (isset($options['f'])) { diff --git a/discovery.php b/discovery.php index c418df14d9..3dc497e7b2 100755 --- a/discovery.php +++ b/discovery.php @@ -64,7 +64,7 @@ if (isset($options['i']) && $options['i'] && isset($options['n'])) { $doing = $options['n'].'/'.$options['i']; } -if (isset($options['d']) || isset($options['v'])) { +if (set_debug(isset($options['d'])) || isset($options['v'])) { $versions = version_info(); echo << '500') { - $rrd_options .= " LINE1.25:$mempool[mempool_id]perc#".$background['left'].":'$descr'"; - $rrd_options .= " GPRINT:$mempool[mempool_id]size:LAST:%6.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]used:LAST:%6.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]free:LAST:%6.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]free:MIN:%5.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]free:MAX:%5.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]free:AVERAGE:%5.2lf%sB\\n"; + $rrd_options .= " LINE1.25:{$mempool['mempool_id']}perc#".$background['left'].":'$descr'"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}size:LAST:%6.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}used:LAST:%6.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}free:LAST:%6.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}free:MIN:%5.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}free:MAX:%5.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}free:AVERAGE:%5.2lf%sB\\n"; $rrd_options .= " COMMENT:'".substr(str_pad('', ($descr_len + 12)), 0, ($descr_len + 12))." '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]perc:LAST:'%5.2lf%% '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]percx:LAST:'%5.2lf%% '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]perc:MIN:'%5.2lf%% '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]perc:MAX:'%5.2lf%% '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]perc:AVERAGE:%5.2lf%%\\n"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}perc:LAST:'%5.2lf%% '"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}percx:LAST:'%5.2lf%% '"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}perc:MIN:'%5.2lf%% '"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}perc:MAX:'%5.2lf%% '"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}perc:AVERAGE:%5.2lf%%\\n"; } else { - $rrd_options .= " LINE1.25:$mempool[mempool_id]perc#".$background['left'].":'$descr'"; - $rrd_options .= " GPRINT:$mempool[mempool_id]size:LAST:%6.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]used:LAST:%6.2lf%sB"; - $rrd_options .= " GPRINT:$mempool[mempool_id]free:LAST:%6.2lf%sB"; + $rrd_options .= " LINE1.25:{$mempool['mempool_id']}perc#".$background['left'].":'$descr'"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}size:LAST:%6.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}used:LAST:%6.2lf%sB"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}free:LAST:%6.2lf%sB"; $rrd_options .= " COMMENT:'\l'"; $rrd_options .= " COMMENT:'".substr(str_pad('', ($descr_len + 12)), 0, ($descr_len + 12))." '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]perc:LAST:'%5.2lf%% '"; - $rrd_options .= " GPRINT:$mempool[mempool_id]percx:LAST:'%5.2lf%% '"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}perc:LAST:'%5.2lf%% '"; + $rrd_options .= " GPRINT:{$mempool['mempool_id']}percx:LAST:'%5.2lf%% '"; $rrd_options .= " COMMENT:'\l'"; }//end if diff --git a/html/includes/print-accesspoint.inc.php b/html/includes/print-accesspoint.inc.php index d1149113d4..b3e3e884a2 100644 --- a/html/includes/print-accesspoint.inc.php +++ b/html/includes/print-accesspoint.inc.php @@ -37,9 +37,9 @@ echo "
\n"; echo ''; -echo " ".format_bi($ap[numasoclients]).' Clients
'; -echo " ".format_bi($ap[radioutil]).' % busy
'; -echo " ".format_bi($ap[interference]).' interference index
'; +echo " ".format_bi($ap['numasoclients']).' Clients
'; +echo " ".format_bi($ap['radioutil']).' % busy
'; +echo " ".format_bi($ap['interference']).' interference index
'; echo ''; diff --git a/html/includes/print-debug.php b/html/includes/print-debug.php deleted file mode 100644 index 7fdbbcc6d9..0000000000 --- a/html/includes/print-debug.php +++ /dev/null @@ -1,86 +0,0 @@ - - - - - diff --git a/html/includes/print-interface.inc.php b/html/includes/print-interface.inc.php index 7060ffcb92..3bc43c3e52 100644 --- a/html/includes/print-interface.inc.php +++ b/html/includes/print-interface.inc.php @@ -103,7 +103,7 @@ if ($port['ifSpeed']) { echo '
'; -if ($port[ifDuplex] != 'unknown') { +if ($port['ifDuplex'] != 'unknown') { echo ''.$port['ifDuplex'].''; } else { echo '-'; diff --git a/html/includes/table/address-search.inc.php b/html/includes/table/address-search.inc.php index c77be68467..1697468eee 100644 --- a/html/includes/table/address-search.inc.php +++ b/html/includes/table/address-search.inc.php @@ -93,7 +93,7 @@ foreach (dbFetchRows($sql, $param) as $interface) { } if ($interface['in_errors'] > 0 || $interface['out_errors'] > 0) { - $error_img = generate_port_link($interface, "", errors); + $error_img = generate_port_link($interface, "", 'errors'); } else { $error_img = ''; } diff --git a/html/legacy_index.php b/html/legacy_index.php index 8956dee99f..d373a6fafa 100644 --- a/html/legacy_index.php +++ b/html/legacy_index.php @@ -22,32 +22,6 @@ if (empty($_SERVER['PATH_INFO'])) { } } -if (strpos($_SERVER['REQUEST_URI'], "debug")) { - $debug = true; - ini_set('display_errors', 0); - ini_set('display_startup_errors', 1); - ini_set('log_errors', 1); - ini_set('error_reporting', E_ALL); - set_error_handler(function ($errno, $errstr, $errfile, $errline) { - global $php_debug; - $php_debug[] = array('errno' => $errno, 'errstr' => $errstr, 'errfile' => $errfile, 'errline' => $errline); - }); - register_shutdown_function(function () { - $last_error = error_get_last(); - if ($last_error['type'] == 1) { - $log_error = array($last_error['type'], $last_error['message'], $last_error['file'], $last_error['line']); - print_r($log_error); - } - }); - $sql_debug = array(); - $php_debug = array(); -} else { - $debug = false; - ini_set('display_errors', 0); - ini_set('display_startup_errors', 0); - ini_set('log_errors', 0); - ini_set('error_reporting', 0); -} // Set variables $msg_box = array(); @@ -61,6 +35,8 @@ if (!file_exists('../config.php') && $_SERVER['PATH_INFO'] != '/install.php') { $init_modules = array('web', 'auth'); require realpath(__DIR__ . '/..') . '/includes/init.php'; +set_debug(str_contains($_SERVER['REQUEST_URI'], 'debug')); + LibreNMS\Plugins::start(); $runtime_start = microtime(true); @@ -68,7 +44,6 @@ $runtime_start = microtime(true); ob_start(); ini_set('allow_url_fopen', 0); -ini_set('display_errors', 0); if (strstr($_SERVER['REQUEST_URI'], 'widescreen=yes')) { $_SESSION['widescreen'] = 1; @@ -341,10 +316,6 @@ if (is_array($msg_box)) { echo(""); } -if (is_array($sql_debug) && is_array($php_debug) && Auth::check()) { - require_once "includes/print-debug.php"; -} - if ($no_refresh !== true && $config['page_refresh'] != 0) { $refresh = $config['page_refresh'] * 1000; echo('