Script Policy

This policy (ScriptPolicy) lets you use Groovy scripts to modify provisioned node data when the built-in policies cannot express what you need. The script runs against each matching node during a node scan.

Parameter Description Required

script

File name of the Groovy script, relative to ${OPENNMS_HOME}/etc/script-policies.

yes

This policy also accepts the common matchBehavior parameter and matches against the same node properties as the Node Categorization policy (for example, label, sysObjectId, and sysName). See Policies for matchBehavior and the ~ regular-expression prefix.

The script receives two bindings: node (the current node, mutable) and LOG (a logger). Return the node to apply your changes. Returning null aborts the scan for that node — its scan results are not applied — but it does not delete the node, which remains as defined in the requisition.

The following example sets the 192.168.100.0/24 interface to PRIMARY, while all remaining interfaces are set to SECONDARY. Furthermore, the node’s location is set to Minneapolis.

import org.opennms.netmgt.model.OnmsIpInterface;
import org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation;
import org.opennms.netmgt.model.PrimaryType;

for(OnmsIpInterface iface : node.getIpInterfaces()) {
    if (iface.getIpAddressAsString().matches("^192\\.168\\.100\\..*")) {
        LOG.warn(iface.getIpAddressAsString() + " set to PRIMARY")
        iface.setIsSnmpPrimary(PrimaryType.PRIMARY)
    } else {
        LOG.warn(iface.getIpAddressAsString() + " set to SECONDARY")
        iface.setIsSnmpPrimary(PrimaryType.SECONDARY)
    }
}

node.setLocation(new OnmsMonitoringLocation("Minneapolis", ""));

return node;

For a further example, see Custom logic with the Script Policy.