skip to main content
Widgets : Writing a simple widget
   
Writing a simple widget
 
Creating the directory structure
Writing the widget file
Writing the view file
Multiple instances
Adding the widget to the widget table
Removing a widget
Viewing the widget
In this example we will create a small hello_world widget. We will assume you have:
ssh access to the server
required knowledge about PHP
knowledge about how to use an editor in a Linux environment.
We does also assume that you, before you start, log in to the op5 Monitor server.
Creating the directory structure
To create the directory structure
1 Go to the application folder:
cd /opt/monitor/op5/ninja/application
2 Create the following folders:
mkdir -p custom_widgets/hello_world
mkdir custom_widgets/hello_world/css
mkdir custom_widgets/hello_world/images
mkdir custom_widgets/hello_world/js
Writing the widget file
To write the widget file
1 Go to the widget folder:
# cd /opt/monitor/op5/ninja/application/custom_widgets/hello_world
2 Create a file, with your favorite editor, called:
hello_world.php
3 Type in the following content in the file:
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Hello_world_Widget extends widget_Base {
public function options()
{
// Load default options, like refresh frequency
$options = parent::options();
// Add option for specifying a custom greeting
// (widget_name, option_name, label,
// option_type, special_options, default)
$options[] = new option('hello_world', 'greeting', 'Greeting',
'input', array(), 'World');
return $options;
}
public function index()
{
// get the widget arguments, based on the options above
$arguments = $this->get_arguments();
require($this->view_path('view'));
}
}
4 Save and exit from your editor.
Writing the view file
 
To write the view file
1 Go to the widget folder:
# cd /opt/monitor/op5/ninja/application/custom_widgets/hello_world
2 Create a file, with your favorite editor, called:
view.php
3 Type in the following content in the file:
<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>
Hello <?php echo $arguments['greeting']; ?>!
4 Save and exit from your editor.
Multiple instances
It is possible to spawn multiple instances of a custom widget, like the ones we are shipping in op5 Monitor.
You could use this to create a gazillion of “Hello World”-widgets, or you could create a widget that has multiple datasources like the built-in widget “Unacknowledged service problems” that is described in op5 Monitor User Manual.
To add this functionality, insert the following in the top of your widget class:
protected $duplicatable = true;
Adding the widget to the widget table
Before we can see the widget on the tactical overview we need to add it to the widgets table in mysql.
To add the widget to the database
Run the following command from your shell:
# php /opt/monitor/op5/ninja/index.php cli/save_widget \
--name=hello_world --friendly_name='Hello World' --page='tac/index'
 
Removing a widget
If you for some reason should want to delete a added widget completely you can remove it by deleting it from the mysql database:
# mysql -uroot merlin
> DELETE FROM ninja_widgets WHERE NAME=’hello_world’
> quit
Viewing the widget
If everything is done correctly we will now be able to view the simple op5 Monitor widget.
To view the widget
Open up the Tactical overview in the op5 Monitor user interface and it will look like this: