From 4eeaed06b47316cd1a64649218f9de3bcdbaf446 Mon Sep 17 00:00:00 2001 From: PipoCanaja Date: Mon, 7 May 2018 00:42:33 +0200 Subject: [PATCH 1/6] Added new plugin menu_option in the "port" page, which contain hook calling public function port_container($device, $port) in plugins --- html/pages/device/port.inc.php | 5 +++++ html/pages/device/port/plugins.inc.php | 20 ++++++++++++++++++++ html/plugins/Test/Test.php | 13 ++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 html/pages/device/port/plugins.inc.php diff --git a/html/pages/device/port.inc.php b/html/pages/device/port.inc.php index 424cc1324d64..8119e7c690e4 100644 --- a/html/pages/device/port.inc.php +++ b/html/pages/device/port.inc.php @@ -104,6 +104,11 @@ $menu_options['cbqos'] = 'CBQoS'; } +// If enabled via config, display the Plugin Link +if($config['enable_ports_plugins'] > 0) { + $menu_options['plugins'] = 'Plugins'; +} + $sep = ''; foreach ($menu_options as $option => $text) { echo $sep; diff --git a/html/pages/device/port/plugins.inc.php b/html/pages/device/port/plugins.inc.php new file mode 100644 index 000000000000..2b68ea3544c2 --- /dev/null +++ b/html/pages/device/port/plugins.inc.php @@ -0,0 +1,20 @@ + +

Plugins

+
+ diff --git a/html/plugins/Test/Test.php b/html/plugins/Test/Test.php index 273fe4320dc2..08f72980a7de 100644 --- a/html/plugins/Test/Test.php +++ b/html/plugins/Test/Test.php @@ -4,18 +4,21 @@ class Test { - public static function menu() { echo '
  • Test
  • '; }//end menu() - /* - public function device_overview_container($device) { + public function device_overview_container($device) { echo('
    '.get_class().' Plugin
    '); echo(' Example plugin in "Device - Overview" tab
    '); echo('
    '); - } - */ + } + + public function port_container($device, $port) { + echo('
    '.get_class().' plugin in "Port" tab
    '); + echo ('Example display in Port tab
    '); + echo('
    '); + } } From e77226459be55658fd03722e091512fcb18fdfb1 Mon Sep 17 00:00:00 2001 From: PipoCanaja Date: Mon, 7 May 2018 00:53:07 +0200 Subject: [PATCH 2/6] Cleaning after pre-commit error --- html/pages/device/port.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/pages/device/port.inc.php b/html/pages/device/port.inc.php index 8119e7c690e4..3583fd3051dd 100644 --- a/html/pages/device/port.inc.php +++ b/html/pages/device/port.inc.php @@ -105,7 +105,7 @@ } // If enabled via config, display the Plugin Link -if($config['enable_ports_plugins'] > 0) { +if ($config['enable_ports_plugins'] > 0) { $menu_options['plugins'] = 'Plugins'; } From 48dd7128f06dc138c7feff5c0fa160b6f35551c6 Mon Sep 17 00:00:00 2001 From: PipoCanaja Date: Fri, 11 May 2018 23:18:56 +0200 Subject: [PATCH 3/6] New method in Plugins.php to allow counting all plugins implementing a specific hook. This allow conditionnal display of the plugin menu_option in the port view. --- LibreNMS/Plugins.php | 10 ++++++++++ html/pages/device/port.inc.php | 4 ++-- html/pages/device/port/plugins.inc.php | 3 --- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/LibreNMS/Plugins.php b/LibreNMS/Plugins.php index 0f14921eed1e..36ead2b84820 100644 --- a/LibreNMS/Plugins.php +++ b/LibreNMS/Plugins.php @@ -82,6 +82,16 @@ public static function load($file, $pluginName) return $plugin; }//end load() + public static function countHooks($hook) + { + // count all plugins implementing a specific hook + self::start(); + if (!empty(self::$plugins[$hook])) { + return count(self::$plugins[$hook]); + } else { + return false; + } + } public static function call($hook, $params = false) { diff --git a/html/pages/device/port.inc.php b/html/pages/device/port.inc.php index 3583fd3051dd..8b628da74bd5 100644 --- a/html/pages/device/port.inc.php +++ b/html/pages/device/port.inc.php @@ -104,8 +104,8 @@ $menu_options['cbqos'] = 'CBQoS'; } -// If enabled via config, display the Plugin Link -if ($config['enable_ports_plugins'] > 0) { +if (LibreNMS\Plugins::count_hook('port_container')) { + // Checking if any plugin implements the port_container. If yes, allow to display the menu_option $menu_options['plugins'] = 'Plugins'; } diff --git a/html/pages/device/port/plugins.inc.php b/html/pages/device/port/plugins.inc.php index 2b68ea3544c2..b670a603535f 100644 --- a/html/pages/device/port/plugins.inc.php +++ b/html/pages/device/port/plugins.inc.php @@ -9,9 +9,6 @@ * the source code distribution for details. */ $pagetitle[] = 'Plugins'; -$port_id_notes = 'port_id_notes:' . $port['port_id']; -$device_id = $device['device_id']; -$data = get_dev_attrib($device, $port_id_notes); ?>

    Plugins


    From c63ba9e68c72b07b655356cb5eded064b6bd8259 Mon Sep 17 00:00:00 2001 From: PipoCanaja Date: Sat, 12 May 2018 01:06:23 +0200 Subject: [PATCH 4/6] Typo after rebase --- html/pages/device/port.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/html/pages/device/port.inc.php b/html/pages/device/port.inc.php index 8b628da74bd5..1ad449c97d2e 100644 --- a/html/pages/device/port.inc.php +++ b/html/pages/device/port.inc.php @@ -104,7 +104,7 @@ $menu_options['cbqos'] = 'CBQoS'; } -if (LibreNMS\Plugins::count_hook('port_container')) { +if (LibreNMS\Plugins::countHooks('port_container')) { // Checking if any plugin implements the port_container. If yes, allow to display the menu_option $menu_options['plugins'] = 'Plugins'; } From 4e262130a066753c9fa20f6dfb95bd1a5cfea5c6 Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Sat, 19 May 2018 10:30:50 +0100 Subject: [PATCH 5/6] Update plugins.inc.php --- html/pages/device/port/plugins.inc.php | 1 - 1 file changed, 1 deletion(-) diff --git a/html/pages/device/port/plugins.inc.php b/html/pages/device/port/plugins.inc.php index b670a603535f..503fe6649fbd 100644 --- a/html/pages/device/port/plugins.inc.php +++ b/html/pages/device/port/plugins.inc.php @@ -14,4 +14,3 @@
    From df5b761d54838e27997337e759d09533bd33250f Mon Sep 17 00:00:00 2001 From: PipoCanaja Date: Sat, 19 May 2018 19:08:56 +0200 Subject: [PATCH 6/6] Updating the documentation with device_overview_container and port_container hooks. --- doc/Extensions/Plugin-System.md | 38 +++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/doc/Extensions/Plugin-System.md b/doc/Extensions/Plugin-System.md index b905f8c32d33..7fe8cd9ea3ce 100644 --- a/doc/Extensions/Plugin-System.md +++ b/doc/Extensions/Plugin-System.md @@ -4,8 +4,9 @@ source: Extensions/Plugin-System.md > This will most likely be deprecated in favour of adding the possible extensions to the core code base. This documentation will hopefully give you a basis for how to write a plugin for LibreNMS. +A test plugin is included in LibreNMS distribution. -A test plugin is available on GitHub: https://github.com/laf/Test +### Generic structure ### Plugins need to be installed into html/plugins @@ -48,17 +49,36 @@ PluginName.inc.php - This file is the main included file when browsing to the pl ### System Hooks ### -System hooks are called as functions within your plugin class, so for example to create a menu entry within the Plugin dropdown you would do: +System hooks are called as functions within your plugin class. +The following system hooks are currently available: +* menu() + * This is called to build the plugin menu system and you can use this to link to your plugin (you don't have to). ``` - public function menu() { - echo('
  • '.get_class().'
  • '); - } + public static function menu() { + echo('
  • '.get_class().'
  • '); + } ``` -This would then add the name and a link to your plugin. +* device_overview_container($device) + * This is called in the Device Overview page. You receive the $device as a parameter, can do your work here and display your results in a frame. -The following system hooks are currently available: +``` + public function device_overview_container($device) { + echo('
    '.get_class().' Plugin
    '); + echo(' Example plugin in "Device - Overview" tab
    '); + echo('
    '); + } +``` + +* port_container($device, $port) + * This is called in the Port page, in the "Plugins" menu_option that will appear when your plugin gets enabled. In this function, you can do your work and display your results in a frame. + +``` + public function port_container($device, $port) { + echo('
    '.get_class().' plugin in "Port" tab
    '); + echo ('Example display in Port tab
    '); + echo('
    '); + } +``` -menu() -* This is called to build the plugin menu system and you can use this to link to your plugin (you don't have to).