Using ScriptPolicies to set assets

Abstract

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">
   <begin>172.21.78.1</begin>
   <end>172.21.78.253</end>
 </include-range>
 <include-range location="FUL" retries="1" timeout="1000" foreign-source="idrac">
    <begin>10.10.9.1</begin>
    <end>10.10.9.254</end>
</include-range>
<include-range location="WDC" retries="1" timeout="1000" foreign-source="idrac">
    <begin>172.22.78.1</begin>
    <end>172.22.78.253</end>
</include-range>

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>

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

import java.util.stream.Collectors

// 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="idrac-wdc.properties"
String fra="idrac-fra.properties"
String ful="idrac-ful.properties"

def setAssets(fileName){
    Properties props = new Properties()
    File propsFile = new File(fileName)
    props.load(propsFile.newDataInputStream())
    node.assetRecord.city = props.getProperty('city')
    node.assetRecord.address1 = props.getProperty('address1')
    node.assetRecord.zip = props.getProperty('zip')
    node.assetRecord.country = props.getProperty('country')
    node.assetRecord.floor = props.getProperty('floor')
    node.assetRecord.room = 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);
     break;
     case "Default":
     setAssets(path + fra);
     break;
     case "FUL":
     setAssets(path + ful);
     break;
    }
}

Property files

An example of a property file

city=City
address1=Street
zip=12345
country=Country
floot=Basement
room=5
rack=3
circuitId=1
vendorPhone=AccountManagerNumber
supportPhone=HotlineNumberDell
1 Like