Moogsoft Docs

MooBot Modules


There are two places within Moog AIOps where 'Bots' (simple computer programs for performing automated tasks) are used; in a moog_farmd Moolet, where they are called MooBots and in LAMs, where they are called LAMBots (For more information on LAMs and LAMBots, see Data ingestion ). A MooBot is a JavaScript file that is loaded at start up by the Moolet. The MooBot exposes logic and data flow, which you can control in JavaScript, relevant to the necessary function.

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 automations.

Threads and global scope

Moogsoft AIOps 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

Module Description
Constants Build a key value dictionary shared across MooBots
Logger Write log messages to the common moog_farmd log file
Events Set the types of Event that interest a MooBot
Process Run and control the execution of other processes
ExternalDb Access external relational databases
MoogDb.V2 Query and manipulate a variety of entities in the Moogsoft AIOps database, including Alerts and Situations
REST.V2 Access an external RESTful API via HTTP to post, read, or delete data


To use these modules, at the top of the MooBot js file, define their variables using the loadModule method.

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

Worked 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 AIOps needs to know which functionality inside of the MooBot to run.

Using external modules in MooBots

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 in MooBots

MooBots can include an onLoad function to allow commands to be run once on startup per MooBot instance. This can be used to initialize internal variables, such as dbTypes , as shown in the code example below:

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