Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Plugins in the Port page #8665

Merged
merged 6 commits into from May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions LibreNMS/Plugins.php
Expand Up @@ -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)
{
Expand Down
38 changes: 29 additions & 9 deletions doc/Extensions/Plugin-System.md
Expand Up @@ -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

Expand Down Expand Up @@ -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('<li><a href="plugin/p='.get_class().'">'.get_class().'</a></li>');
}
public static function menu() {
echo('<li><a href="plugin/p='.get_class().'">'.get_class().'</a></li>');
}
```

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('<div class="container-fluid"><div class="row"> <div class="col-md-12"> <div class="panel panel-default panel-condensed"> <div class="panel-heading"><strong>'.get_class().' Plugin </strong> </div>');
echo(' Example plugin in "Device - Overview" tab <br>');
echo('</div></div></div></div>');
}
```

* 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('<div class="container-fluid"><div class="row"> <div class="col-md-12"> <div class="panel panel-default panel-condensed"> <div class="panel-heading"><strong>'.get_class().' plugin in "Port" tab</strong> </div>');
echo ('Example display in Port tab</br>');
echo('</div></div></div></div>');
}
```

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).
5 changes: 5 additions & 0 deletions html/pages/device/port.inc.php
Expand Up @@ -104,6 +104,11 @@
$menu_options['cbqos'] = 'CBQoS';
}

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';
}

$sep = '';
foreach ($menu_options as $option => $text) {
echo $sep;
Expand Down
16 changes: 16 additions & 0 deletions html/pages/device/port/plugins.inc.php
@@ -0,0 +1,16 @@
<?php
/*
* LibreNMS
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
$pagetitle[] = 'Plugins';
?>
<h3>Plugins</h3>
<hr>
<?php
LibreNMS\Plugins::call('port_container', $device, $port);
13 changes: 8 additions & 5 deletions html/plugins/Test/Test.php
Expand Up @@ -4,18 +4,21 @@

class Test
{

public static function menu()
{
echo '<li><a href="plugin/p=Test">Test</a></li>';
}//end menu()


/*
public function device_overview_container($device) {
public function device_overview_container($device) {
echo('<div class="container-fluid"><div class="row"> <div class="col-md-12"> <div class="panel panel-default panel-condensed"> <div class="panel-heading"><strong>'.get_class().' Plugin </strong> </div>');
echo(' Example plugin in "Device - Overview" tab <br>');
echo('</div></div></div></div>');
}
*/
}

public function port_container($device, $port) {
echo('<div class="container-fluid"><div class="row"> <div class="col-md-12"> <div class="panel panel-default panel-condensed"> <div class="panel-heading"><strong>'.get_class().' plugin in "Port" tab</strong> </div>');
echo ('Example display in Port tab</br>');
echo('</div></div></div></div>');
}
}