6.1. Summary

NetSpyGlass comes with embedded Python interpreter that is used to process collected monitoring data. At the end of each polling cycle, the server scans Python scripts found in the directory scripts/aggregation, looking for those that declare a class based on nw2rules.Nw2Rules with a method execute(). The server loads all scripts like that and then calls method execute().

Scripts are executed in parallel, sometimes by different servers, and do not share memory. Scripts can not share variables directly with each other.

These scripts operate on monitoring variables and can modify them, create new variables, create log records, add or modify tags on monitoring variables and trigger alerts.

Here is an example of a simple aggregation script that uses NsgQL query to find and count active agents and then exports the value it calculates as new monitoring variable with name numAgents. The script can be checked in to git as file aggregation/count_agents.py:

import nw2rules
import nw2functions


class Aggregation(nw2rules.Nw2Rules):
    def __init__(self, log):
        super(Aggregation, self).__init__(log)

    def execute(self):
        """
        count customer's agents (those that do not belong to region "netspyglass")

        the agent is assumed to be "down" if we dd not receive its status for the last 5
        minutes. Export result as new variable numAgents
        """
        agents_query = \
            "FROM processUptime WHERE (NsgRole = 'agent' and NsgRegion NOT NULL and NsgRegion != 'netspyglass')"

        agents_count = sum(1 for mv in nw2functions.aggregation_query(agents_query, 300)
                           if mv.timeseries and mv.timeseries.getLastValue() > 0)

        aggr = nw2functions.new_var('nsg-agents', 'count')
        aggr.timeseries.put(nw2functions.current_timestamp(), agents_count)
        aggr.addTag('VariableTags.Aggregation')
        nw2functions.export_var('numAgents', [aggr])

The script defines class Aggregation (the name does not really matter) with method execute(). Servers fund this script and call execute() on every cycle.