Skip to content

How to create a new plugin ?

Nicolas Hennion edited this page Oct 20, 2024 · 5 revisions

First of all, have a look on existing plugins and ask you a simple question: is it possible to improve an existing plugin instead of create a new one ?

Second question, is it possible to use an AMP configuration ? Glances proposes an Application Monitoring Process (AMP) where you can monitor and run action (script).

If you answer "no" to the previous questions, this page will drive you in the process of creating a new Glances plugin.

Create an issue on the official Github to explain your need

Open an issue (feature request) in order to explain your need and how you plan to the plugin.

Configure your development environment

Read the following Wiki page: https://github.com/nicolargo/glances/wiki/How-to-contribute-to-Glances-%3F

Start coding the new foo plugin

foo is an example... to be replace by your plugin name.

Create the plugin script

In the glances/plugins folder, create a foo sub-folder and a foo/init.py file.

You can use the following skeleton for the init.py file: https://gist.github.com/nicolargo/505e5603217c8c4f8adf779dda558841

  • fields_description is a global variable with fields description for API
  • items_history_list (optional) is a global variable list of field to be saved in the history
  • class PluginModel is the main class (do not change the name), all the followings methods should be defined inside this class, it's a child of GlancesPluginModel.

init method

self.display_curse = True should be True if you want to display it in UI Variables from the configuration file (where a [foo] section should be created) can be reached with:

config.get_bool_value(self.plugin_name, 'variable_name', default=<default value if not defined>)

get_key method (optional)

In case of foo return a list of dict, define the key in the dict.

For example, for the DiskIO plugin, the key is th disk_name.

update method

Call at every refresh, it should set the self.stats variable with the stats used by the plugin.

self.stats should be init with get_init_value method.

update_views method (optional)

Call at every refresh, it should set the self.views variable with the information needed to display the stats in the UI.

msg_curse method

Call by the TUI in order to define how to display the stats.

Init your plugin in the main script

Edit the glances/main.py script and add the following line in the init_args method of the GlancesMain classe:

parser.add_argument('--disable-foo', action='store_true', default=False, dest='disable_foo', help='disable Foo module')

Add your plugin in the curses interface

Edit the glances/outputs/glances_curses.py script and add your plugin to one of the following variable in the _GlancesCurses class:

  • _top
  • _left_sidebar
  • _right_sidebar

Also add shortcut to the _hotkeys variable in the same _GlancesCurses class.

Add your plugin in the Web interface

In the glances/outputs/static/js/App.vue file, add:

import GlancesPluginFoo from './components/plugin-foo.vue';

also in App.vue, add shortcut in setupHotKeys method.

If your plugin is in the left menu, add it in the glances/outputs/static/js/uiconfig.json file

In glances/outputs/static/js/components create a new plugin-foo.vue file (you can use another plugin-*.vue file to create the new one).

The rebuild the WebUI with:

make webui

And test it with:

make run-webserver

Test

If relevant, create a new unit test in the unitest.py file.

Pull request

When everything is ok then create a pull request on the DEVELOP branch !