Using ScriptPolicies to set assets


This example demonstrates how to set asset fields for different locations using the ScriptPolicy in Provisiond.
In this scenario we have multiple discovery processes for different and dedicated networks for Dell iDRACs.

Use Case

Filling the asset fields in general to have more information in OpenNMS about each node and using them to achieve well formated outage notifications.
If iDRACs report hardware issues you need all information about the device in one notification. You need to know the serialNumber (which can be automatically obtained using the provisioning asset adapter, the nodeLocation and also it might be helpful to know, who you gonna call to get you broken hardward replaced.
If you are using monitors like these to monitor the hardware status of you Dell servers, you can easily enrich nodeLostService based notifications with assets to achieve something like this

Discoveryd Examples

Multiple discovery configurations to find iDRAC devices and set the location.

<include-range location="Default" retries="1" timeout="1000" foreign-source="idrac">
 <include-range location="FUL" retries="1" timeout="1000" foreign-source="idrac">
<include-range location="WDC" retries="1" timeout="1000" foreign-source="idrac">

Requisition Script Policy

The requisition definition idrac needs a script policy configuration that points to the policy script.

<policy name="assets" class="org.opennms.netmgt.provision.persist.policies.ScriptPolicy">
  <parameter key="matchBehavior" value="ALL_PARAMETERS"/>
  <parameter key="script" value="set_assets.groovy"/>

Policy Script

The script is located in /opt/opennms/etc/script-policies.groovy. When a provisiond synchronisation is triggered it will fetch the nodeLocation (that is set by the discovery process) of a node and will choose the corresponding variables in a property file that contains the asset information that should be used for each node.

import org.opennms.core.utils.ByteArrayComparator
import org.opennms.core.utils.InetAddressUtils
import org.opennms.netmgt.model.OnmsAssetRecord
import org.opennms.netmgt.model.OnmsIpInterface
import org.opennms.netmgt.model.PrimaryType
import org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation


// transaction is a boolean to indicate whether the scripts run in transaction or not.
// Not in transaction indicates that the node is in initial stage of scanning phase and  may not be persisted yet.
// Only use this to add assets/metadata or return invalid node (returning null) to abort the scan.

String path="/opt/opennms/etc/script-policies/"
String wdc=""
String fra=""
String ful=""

def setAssets(fileName){
    Properties props = new Properties()
    File propsFile = new File(fileName)
    props.load(propsFile.newDataInputStream()) = props.getProperty('city')
    node.assetRecord.address1 = props.getProperty('address1') = props.getProperty('zip') = props.getProperty('country')
    node.assetRecord.floor = props.getProperty('floor') = props.getProperty('room')
    node.assetRecord.rack = props.getProperty('rack')
    node.assetRecord.circuitId = props.getProperty('circuitId')
    node.assetRecord.vendorPhone = props.getProperty('vendorPhone')
    node.assetRecord.supportPhone = props.getProperty('supportPhone')
    return node

 if (! this.binding.variables.containsKey("transaction") || ! transaction) {
    LOG.debug("customscript: not in transaction");
    switch(node.location.locationName) {
     case "WDC":
     setAssets(path + wdc);
     case "Default":
     setAssets(path + fra);
     case "FUL":
     setAssets(path + ful);

Property files

An example of a property file

1 Like