Action Menu

new in release v1.86

The Action Menu is a way to create custom icons and menus for every host or service. You may add multiple action icons or even multiple menus for each host or service. You can then open links or trigger server actions from that menu or icons.

Action Menu

Adding an Action Menu

There are 3 ways to add an action menu to a host or service.

  • set the menu directly by the _THRUK_ACTION_MENU custom variable

  • reference an existing menu from the action_menu_items by a _THRUK_ACTION_MENU custom variable

  • use apply rules from action_menu_apply

The are some examples in the configuration documentation.

Server Actions

Server Action can be triggered from icons or menu items by using a pseudo url like this in your action urls. See the configuration section on how and where to set this link.

    server://*scriptname*/argument1/argument2/...

To make this work, you need to define that scriptname in your thruk_local.conf:

  <action_menu_actions>
      scriptname   = /usr/local/bin/sample.sh $HOSTNAME$ $SERVICEDESC$
  </action_menu_actions>

When somebody clicks on that link, Thruk will start the sample.sh script like:

    /usr/local/bin/sample.sh <hostname> <servicename> argument1 argument2

First it uses the complete commandline from the thruk_local.conf and replaces all available macros. Then it adds the arguments from the link configuration.

See Macros for a list of all available macros.

Scripts

The scripts can do whatever they want. However you should consider a few things:

  • Make the script exit quickly. Thruk waits for the script to return. Long running tasks should be started in the background.

  • Give the user useful feedback on STDOUT

  • Exit codes should be zero if OK and greater than zero on errors.

script output

Script output will be displayed as user response which allows the script to give feedback to the user. HTML is allowed and will be used in the output.

Authorization / Security

The Script will be run whenever the user has access to the given host and service. Its general good advice to check the $REMOTE_USER and/or $REMOTE_USER_GROUPS environment variables before running critical scripts. Macros used in the command line defined in your config and the REMOTE_USER variables can be trusted. Variables supplied by the url can easily be faked and are considered unsafe.

You can use the action_wrapper script from the examples folder to check for valid users and groups.

Available standard environment variables are listed on the CLI Environment page.

Dynamic Javascript Menus

new in release v2.24

Since version 2.24 you can use javascript to dynamically create menus. Basically you create a file ending on .js in your action_menus folder. The first function will be called and expected to return a json structure with the actual menu.

Althought the first function will be called to generate the menu, the file may contain multiple functions, for example to generate submenus.

The first function in the file must return an array of (action_menu_items). The menu itself can be a function again to create more dynamic menus.

All defined functions are running in the same namespace, so make sure your functions use uniq names over all your menus. Best practice is to use the filename as prefix.

A very simple example:

action_menus/js_basic_menu.js
function js_basic_menu(data) {
    return({
      "icon":"/thruk/themes/{{theme}}/images/dropdown.png",
      "title": "javascript menu",
      "menu":[{
          "icon":"/thruk/themes/{{theme}}/images/link.png",
          "label":"test menu item",
          "action":"http://..."
        }]
    });
}

A more advanced example with dynamic submenus. Menu callbacks may return thenables (in jQuery context). This makes it possible to do ajax requests and use the result to create submenus. This example creates a submenu listing all hosts.

action_menus/js_adv_menu.js
function js_adv_menu(cfg) {
    return({
      "icon":"/thruk/themes/{{theme}}/images/dropdown.png",
      "title": "javascript advanced menu",
      "menu":[
        {
          "icon":"/thruk/themes/{{theme}}/images/link.png",
          "label":"test menu item",
          "action":"http://..."
        },
        {
          "icon":"/thruk/themes/{{theme}}/images/folder.png",
          "label":"list all hosts",
          "menu": js_adv_menu_submenu
        }
      ]
    });
}

function js_adv_menu_submenu(cfg) {
  return(jQuery.get("../r/hosts?columns=name").then(function(data, textStatus, jqXHR) {
    var result = [];
    jQuery(data).each(function(i, r) {
      result.push({ label: r.name });
    });
    return(result);
  }));
}

Examples

Panorama Dashboard

This is a small example script which uses perl to change the color of a shaped icon.

icon settings

Dashboard Icon

thruk_local.conf
  <action_menu_actions>
    switch_color    = .../switch_color.pl $DASHBOARD_ID$ $DASHBOARD_ICON$
  </action_menu_actions>
switch_color.pl

just changes the color of the icon.

#!/usr/bin/perl

use warnings;
use strict;
use Thruk::Utils;

my $file  = 'var/panorama/'.$ARGV[0].'.tab';
my $data  = Thruk::Utils::read_data_file($file);
my $color = $data->{$ARGV[1]}->{'xdata'}->{'appearance'}->{'shapecolor_ok'};
if($color eq '#CA1414') {
    $color = '#199C0F';
} else {
    $color = '#CA1414';
}
$data->{$ARGV[1]}->{'xdata'}->{'appearance'}->{'shapecolor_ok'} = $color;
Thruk::Utils::write_data_file($file, $data);

print "switched color to $color\n";

Confirmation Dialog

new in release v2.14-2

Using arbitrary attributes, you can easily add a confirmation dialog to your server actions.

action_menus/confirm_restart.json
{
  "icon":"../themes/{{theme}}/images/arrow_refresh.png",
  "label":"service restart",
  "action":"server://restart_service",
  "onclick": "return(confirm('Really restart service?'));"
}

Custom Form Elements

Combining the raw html menu item with javascript menus can create any arbitrary menu.

action_menus/js_form_menu.js
function js_form_menu(cfg) {
    return({
      "icon":"/thruk/themes/{{theme}}/images/dropdown.png",
      "title": "javascript advanced menu",
      "menu":[{
        "icon":"/thruk/themes/{{theme}}/images/page_white_key.png",
        "html":"<b>test</b><input type='text' id='menu_input'><input type='button' value='ok' onclick='test_form_handler()'>"
      }]
    });
}

function test_form_handler() {
  thruk_message(0, "user input: "+jQuery('#menu_input').val());
}

Sending Commands

This example fetches some host details from the rest api and shows a dynamic host menu based on some attributes. It also sends some commands.

action_menus/js_cmd_menu.js
function js_menu(d) {
  return({
    "icon":"/thruk/themes/{{theme}}/images/dropdown.png",
    "title": "confirm menu",
    "menu": js_menu_items
  });
}

function js_menu_items(d) {
  // fetch host attributes
  return(jQuery.get("../r/sites/"+d.backend+"/hosts/"+d.host).then(function(data, textStatus, jqXHR) {
    var result = [];
    host_data = data[0];
    if(!host_data) {
      return({ 'label': 'host not found?'});
    }

    result.push({ label: d.host+":", disabled: true });

    // add submenu with all parents
    if(host_data.parents.length > 0) {
      parents_menu = [];
      jQuery(host_data.parents).each(function(i, r) {
        parents_menu.push({ label: r, action: "extinfo.cgi?type=1&host="+r });
      });
      var parents = {
        'label': 'parents',
        'menu': parents_menu
      };
      result.push(parents);
    }

    // add submenu with all child hosts
    if(host_data.childs.length > 0) {
      childs_menu = [];
      jQuery(host_data.childs).each(function(i, r) {
        childs_menu.push({ label: r, action: "extinfo.cgi?type=1&host="+r });
      });
      var childs = {
        'label': 'childs',
        'menu': childs_menu
      };
      result.push(childs);
    }

    // add active checks switch
    result.push({
      'label': "active checks",
      'menu': [{
        'label': 'disable active checks',
        'disabled': !host_data.active_checks_enabled,
        'action': function() {
          jQuery.post("../r/sites/"+d.backend+"/hosts/"+d.host+'/cmd/disable_host_check').then(function(data, textStatus, jqXHR) {
            thruk_message(0, "active checks disabled");
          });
        }
      }, {
        'label':   'enable active checks',
        'disabled': host_data.active_checks_enabled,
        'action': function() {
          jQuery.post("../r/sites/"+d.backend+"/hosts/"+d.host+'/cmd/enable_host_check').then(function(data, textStatus, jqXHR) {
            thruk_message(0, "active checks enabled");
          });
        }
      }]
    });

    return(result);
  }));
}
Edit page on GitHub