Skip to main content

Moobot Modules

Within Moogsoft Enterprise data processing, Moogfarmd Moolets, LAMs and integrations use simple computer programs called "bots" to perform automated tasks. A Moobot is a JavaScript file that is loaded at startup by a Moolet. The Moobot exposes logic and data flow, which you can control in JavaScript, relevant to the necessary function. LAMbots perform a similar function for LAMs and integrations.

Moobots expose the function of the Moolets allowing for extensive customization, for example in the Alert Rules Engine where the Moobot is used to perform automation.

Threads and global scope

Moogsoft Enterprise is built to handle high-scale environments, so individual JavaScript MooBots are run in a multi-threaded fashion. For example, if a Moolet has ten threads, there will be ten instances of the MooBot running. This supports high throughput of Events through the Moobot, particularly, when they are doing complex processing. However, it does have important implications for the JavaScript concerning where the global scope (or context) for the JavaScript program for the MooBot resides. In principle, each Moobot has its own independent global scope. So it is impossible for one Moobot's logic to interact and affect another instance of the Moobot logic. To allow necessary communication between individual Moobot instances there are utility modules such as the Constants module.

Moobot modules

You can use the available Moobot modules to perform these functions:

Module

Description

Config

Read configuration files within LAMbots and Moobots.

Constants

Build a key value dictionary shared across Moobots.

Events

Set the types of Event that interest a Moobot.

ExternalDb

Access external relational databases.

Graph Topology

Access topology methods.

Kafka

Allows you to broadcast information on a Kafka bus.

Logger

Write log messages to the common Moogfarmd log file. See Configure Logging.

Mailer

Send an email in response to events occurring in Moogsoft Enterprise.

MoogDb V2

Query and manipulate a variety of entities in the Moogsoft Enterprise database, including alerts and Situations.

Moolet Informs

Can send update messages from one Moolet to other Moolets.

Process

Run and control the execution of other processes.

RabbitMQ

Allows you to broadcast information on a RabbitMQ bus.

REST.V2

Access an external RESTful API via HTTP to post, read, or delete data.

Utilities

Escape and unescape XML strings, convert an XML string to a JSON object and vice versa.

To use these modules, define a global variable at the top of the Moobot js file using the loadModule method.

You can also load load external JavaScript modules using the loadModule method. See below for more details.

Examples

Throughout this section, all examples will use AlertBuilder.js to explain how Moobots function.

Step 1

When the Alert Builder starts and creates an instance of the Moolet, it creates a Moobot for every threaded instance of the Moolet. The first action undertaken by a Moobot is to load a system wide default file called MooB.js. This file pushes into the Global Scope using a closure, some shared functionality, which you can take advantage of in the Moobot. You should never edit MooB.js as the file is linked to the internal implementation of the Moobots.

Step 2

The preload statements in the MooB.js closure instruct a Moobot to load into its Global Scope the available modules. For example, they can be used to:

  • Change and create structure in the MoogDb database.

  • Listen for specific events in the system.

  • Push events out.

  • Log to the common log file output.

  • Communicate using communication methodologies such as tweets, email etc.

Before you can use any of the built in modules that correspond to the functionality Moogsoft provides, you need the preload() method in the global object (MooB.js) to load the required modules.

The object exposes an API that you can use to add functionality into the system. In the example above, “Process” has a number of functions that you can call which allow the Moolet to run processes in the system.

After loading and running the MooB.js closure in the Moobot, the full Moobot user definable JavaScript file is loaded and run. It is important to understand from a JavaScript concept that it is executed at start-up. The reason for executing the script at start-up is to load any Event driven callbacks, and initialization code inside of the Moobot. For example in the Alert Builder, for a new Event arriving in the Moolet, Moogsoft Enterprise needs to know which functionality inside of the Moobot to run.

Using external modules

Moobots can load external JavaScript modules. This means that modules can be reused as generic functions in multiple Moobots.

To do this:

  • Add the external JavaScript module file (BotExampleModule.js) in the $MOOGSOFT_HOME/bots/moobots or the $MOOGSOFT_HOME/contrib directory.

  • Load the external JavaScript module in the Moobot by adding a line at the beginning (relative paths are supported), for example:

MooBot.loadModule('BotExampleModule.js'); 

The example below shows the external JavaScript module (BotExampleModule.js). It defines a class which takes an alert and prints out a message:

function CPrinter() 
{
    var mLogger=MooBot.loadModule('Logger');
    var self=
    {
        prettyPrint: function(alert)
        {
            mLogger.info("This is a print of " + alert.value("alert_id") + " other info");
        }
    };
    var F=function() {};
    F.prototype=self;
    return( new F() );
}

The AlertMgr.js Moobot loads the external JavaScript module BotExampleModule.js and uses the function CPrinter (from the external JavaScript module) to send alert details to a remote service:

MooBot.loadModule('BotExampleModule.js');
var printer = new CPrinter();
function newAlert(alert)
{
    printer.prettyPrint(alert);
}

onLoad function

Moobots can include an onLoad function to allow commands to be run once on startup per Moobot instance. You can use it to initialize internal variables, such as dbTypes, for example:

var dbTypes = null;
function onLoad()
{
    dbTypes = {
        employees: {
            type: 'mySql',
            host: '192.168.1.141',
            port: '3306',
            database: 'emp_db'
        },
        customers: {
            type: 'sqlServer',
            host: '213.32.112.17',
            database: 'customers',
            user: 'sa',
            encrypted_password: '0rJGl5oCWpmE9Hbk32sxFgxlQV3O5cx2bx1vKNOM7YA='
        }
    };
}