Search in sources :

Example 21 with AttributeGroupType

use of org.opennms.netmgt.collection.api.AttributeGroupType in project opennms by OpenNMS.

the class RegExPropertyExtender method getTargetAttribute.

/* (non-Javadoc)
     * @see org.opennms.netmgt.collectd.SnmpPropertyExtender#getTargetAttribute(java.util.List, org.opennms.netmgt.collectd.SnmpCollectionResource, org.opennms.netmgt.config.datacollection.MibObjProperty)
     */
@Override
public SnmpAttribute getTargetAttribute(List<CollectionAttribute> sourceAttributes, SnmpCollectionResource targetResource, MibObjProperty property) {
    final String sourceType = property.getParameterValue(SOURCE_TYPE);
    if (StringUtils.isBlank(sourceType)) {
        LOG.warn("Cannot execute the RegEx property extender because: missing parameter {}", SOURCE_TYPE);
        return null;
    }
    final String sourceAlias = property.getParameterValue(SOURCE_ALIAS);
    if (StringUtils.isBlank(sourceAlias)) {
        LOG.warn("Cannot execute the RegEx property extender because: missing parameter {}", SOURCE_ALIAS);
        return null;
    }
    final String indexPattern = property.getParameterValue(INDEX_PATTERN);
    if (StringUtils.isBlank(indexPattern)) {
        LOG.warn("Cannot execute the RegEx property extender because: missing parameter {}", INDEX_PATTERN);
        return null;
    }
    Pattern p = Pattern.compile(indexPattern);
    Matcher m = p.matcher(targetResource.getInstance());
    Optional<CollectionAttribute> target = null;
    if (m.find()) {
        final String index = m.group(1);
        target = sourceAttributes.stream().filter(a -> matches(sourceType, sourceAlias, index, a)).findFirst();
    }
    if (target != null && target.isPresent()) {
        AttributeGroupType groupType = targetResource.getGroupType(property.getGroupName());
        if (groupType != null) {
            MibPropertyAttributeType type = new MibPropertyAttributeType(targetResource.getResourceType(), property, groupType);
            SnmpValue value = SnmpUtils.getValueFactory().getOctetString(target.get().getStringValue().getBytes());
            return new SnmpAttribute(targetResource, type, value);
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) Matcher(java.util.regex.Matcher) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType)

Aggregations

AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)21 MibObject (org.opennms.netmgt.config.datacollection.MibObject)16 SnmpAttributeType (org.opennms.netmgt.collectd.SnmpAttributeType)9 NumericAttributeType (org.opennms.netmgt.collectd.NumericAttributeType)8 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)8 Test (org.junit.Test)7 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)7 HashMap (java.util.HashMap)6 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)6 SnmpCollectionResource (org.opennms.netmgt.collectd.SnmpCollectionResource)6 NodeInfo (org.opennms.netmgt.collectd.NodeInfo)5 NodeResourceType (org.opennms.netmgt.collectd.NodeResourceType)5 MockDataCollectionConfig (org.opennms.netmgt.mock.MockDataCollectionConfig)5 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)5 Date (java.util.Date)4 CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)4 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)4 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)4 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)4 SnmpAttribute (org.opennms.netmgt.collectd.SnmpAttribute)3