Skip to content

Commit

Permalink
NMS-7367: Add support for a new 'hexstring' type that converts an oct…
Browse files Browse the repository at this point in the history
…et string

retrieved via SNMP to a hex string when persisted to string.properties.
  • Loading branch information
Jesse White committed Jan 26, 2015
1 parent ca60d79 commit 95eee04
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 3 deletions.
Expand Up @@ -512,7 +512,7 @@
<simpleType>
<restriction base="string">
<pattern
value="([Cc](ounter|OUNTER)(32|64)?|[Gg](auge|AUGE)(32|64)?|[Tt](ime|IME)[Tt](icks|ICKS)|[Ii](nteger|NTEGER)(32|64)?|[Oo](ctet|CTET)[Ss](tring|TRING))|[Ss](tring|TRING)" />
value="([Cc](ounter|OUNTER)(32|64)?|[Gg](auge|AUGE)(32|64)?|[Tt](ime|IME)[Tt](icks|ICKS)|[Ii](nteger|NTEGER)(32|64)?|[Oo](ctet|CTET)[Ss](tring|TRING))|[Ss](tring|TRING)|[Hh](ex|EX)[Ss](tring|TRING))" />
</restriction>
</simpleType>
</attribute>
Expand Down
@@ -0,0 +1,91 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2006-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.netmgt.collectd;

import org.opennms.netmgt.collection.api.AttributeGroupType;
import org.opennms.netmgt.collection.api.CollectionAttribute;
import org.opennms.netmgt.collection.api.Persister;
import org.opennms.netmgt.config.MibObject;

/**
* <p>HexStringAttributeType class.</p>
*
* @author jwhite
*/
public class HexStringAttributeType extends SnmpAttributeType {
/**
* <p>supportsType</p>
*
* @param rawType a {@link java.lang.String} object.
* @return a boolean.
*/
public static boolean supportsType(String rawType) {
return rawType.toLowerCase().startsWith("hexstring");
}

/**
* <p>Constructor for HexStringAttributeType.</p>
*
* @param resourceType a {@link org.opennms.netmgt.collectd.ResourceType} object.
* @param collectionName a {@link java.lang.String} object.
* @param mibObj a {@link org.opennms.netmgt.config.MibObject} object.
* @param groupType a {@link org.opennms.netmgt.collection.api.AttributeGroupType} object.
*/
public HexStringAttributeType(ResourceType resourceType, String collectionName, MibObject mibObj, AttributeGroupType groupType) {
super(resourceType, collectionName, mibObj, groupType);
}

@Override
public void storeAttribute(CollectionAttribute attribute, Persister persister) {
CollectionAttribute attributeToPersist = attribute;
if (attribute instanceof SnmpAttribute) {
// When storing SNMP attributes alter the getStringValue() value method
// so that the hex string is returned instead of the display string
attributeToPersist = new SnmpAttributeWrapper((SnmpAttribute)attribute);
}
persister.persistStringAttribute(attributeToPersist);
}

/**
* Used to alter the behavior the getStringValue() value method.
*/
private static class SnmpAttributeWrapper extends SnmpAttribute {
private final SnmpAttribute m_attribute;

public SnmpAttributeWrapper(SnmpAttribute attribute) {
super(attribute.getResource(), (SnmpAttributeType)attribute.getAttributeType(), attribute.getValue());
m_attribute = attribute;
}

@Override
public String getStringValue() {
return m_attribute.getValue().toHexString();
}
}
}
Expand Up @@ -28,6 +28,7 @@

package org.opennms.netmgt.collectd;

import org.opennms.netmgt.collection.api.CollectionAttributeType;
import org.opennms.netmgt.collection.api.CollectionResource;
import org.opennms.netmgt.collection.api.Persister;
import org.opennms.netmgt.collection.support.AbstractCollectionAttribute;
Expand Down
Expand Up @@ -131,7 +131,10 @@ public static SnmpAttributeType create(ResourceType resourceType, String collect
if (StringAttributeType.supportsType(mibObj.getType())) {
return new StringAttributeType(resourceType, collectionName, mibObj, groupType);
}

if (HexStringAttributeType.supportsType(mibObj.getType())) {
return new HexStringAttributeType(resourceType, collectionName, mibObj, groupType);
}

throw new IllegalArgumentException("No support exists for AttributeType '" + mibObj.getType() + "' for MIB object: "+ mibObj);
}

Expand Down
Expand Up @@ -29,6 +29,7 @@
package org.opennms.netmgt.collectd;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

import java.io.File;
Expand All @@ -37,6 +38,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.junit.After;
Expand All @@ -59,6 +61,7 @@
import org.opennms.netmgt.model.NetworkBuilder;
import org.opennms.netmgt.model.OnmsIpInterface;
import org.opennms.netmgt.model.OnmsNode;
import org.opennms.netmgt.model.ResourceTypeUtils;
import org.opennms.netmgt.rrd.RrdDataSource;
import org.opennms.netmgt.rrd.RrdStrategy;
import org.opennms.netmgt.rrd.RrdUtils;
Expand Down Expand Up @@ -468,6 +471,78 @@ public void testBug2447_GenericIndexedOnlyCollect() throws Exception {
m_collectionSpecification.release(m_collectionAgent);
}

@Test
@Transactional
@JUnitCollector(
datacollectionConfig = "/org/opennms/netmgt/config/datacollection-brocade-no-ifaces-config.xml",
datacollectionType = "snmp",
anticipateRrds={
"1/brocadeFCPortIndex/1/swFCPortTxWords",
"1/brocadeFCPortIndex/1/swFCPortRxWords",
"1/brocadeFCPortIndex/2/swFCPortTxWords",
"1/brocadeFCPortIndex/2/swFCPortRxWords",
"1/brocadeFCPortIndex/3/swFCPortTxWords",
"1/brocadeFCPortIndex/3/swFCPortRxWords",
"1/brocadeFCPortIndex/4/swFCPortTxWords",
"1/brocadeFCPortIndex/4/swFCPortRxWords",
"1/brocadeFCPortIndex/5/swFCPortTxWords",
"1/brocadeFCPortIndex/5/swFCPortRxWords",
"1/brocadeFCPortIndex/6/swFCPortTxWords",
"1/brocadeFCPortIndex/6/swFCPortRxWords",
"1/brocadeFCPortIndex/7/swFCPortTxWords",
"1/brocadeFCPortIndex/7/swFCPortRxWords",
"1/brocadeFCPortIndex/8/swFCPortTxWords",
"1/brocadeFCPortIndex/8/swFCPortRxWords"
},
anticipateFiles={
"1",
"1/brocadeFCPortIndex",
"1/brocadeFCPortIndex/1/strings.properties",
"1/brocadeFCPortIndex/1",
"1/brocadeFCPortIndex/2/strings.properties",
"1/brocadeFCPortIndex/2",
"1/brocadeFCPortIndex/3/strings.properties",
"1/brocadeFCPortIndex/3",
"1/brocadeFCPortIndex/4/strings.properties",
"1/brocadeFCPortIndex/4",
"1/brocadeFCPortIndex/5/strings.properties",
"1/brocadeFCPortIndex/5",
"1/brocadeFCPortIndex/6/strings.properties",
"1/brocadeFCPortIndex/6",
"1/brocadeFCPortIndex/7/strings.properties",
"1/brocadeFCPortIndex/7",
"1/brocadeFCPortIndex/8/strings.properties",
"1/brocadeFCPortIndex/8"
}
)
@JUnitSnmpAgent(resource = "/org/opennms/netmgt/snmp/brocadeTestData1.properties")
public void verifyPersistedStringProperties() throws Exception {
// Initialize the agent
m_collectionSpecification.initialize(m_collectionAgent);

// Perform the collection
CollectionSet collectionSet = m_collectionSpecification.collect(m_collectionAgent);
assertEquals("collection status",
ServiceCollector.COLLECTION_SUCCEEDED,
collectionSet.getStatus());

// Persist
CollectorTestUtils.persistCollectionSet(m_collectionSpecification, collectionSet);

// Verify results on disk
File snmpDir = (File)m_context.getAttribute("rrdDirectory");
Properties properties = ResourceTypeUtils.getStringProperties(snmpDir, "1/brocadeFCPortIndex/1");

// "string" attributes should convert the octets directly to a string
String value = properties.getProperty("swFCPortName");
assertTrue(value.startsWith("..3DUfw"));

// "hexstring" attributes should convert the octets to a hex string
// see http://issues.opennms.org/browse/NMS-7367
value = properties.getProperty("swFCPortWwn");
assertEquals("1100334455667788", value);
}

private static String rrd(String file) {
return file + RrdUtils.getExtension();
}
Expand Down
Expand Up @@ -16,7 +16,7 @@
<group name="brocade-switch-fctable" ifType="all">
<mibObj oid=".1.3.6.1.4.1.1588.2.1.1.1.6.2.1.11" instance="brocadeFCPortIndex" alias="swFCPortTxWords" type="counter" />
<mibObj oid=".1.3.6.1.4.1.1588.2.1.1.1.6.2.1.12" instance="brocadeFCPortIndex" alias="swFCPortRxWords" type="counter" />
<mibObj oid=".1.3.6.1.4.1.1588.2.1.1.1.6.2.1.34" instance="brocadeFCPortIndex" alias="swFCPortWwn" type="string" />
<mibObj oid=".1.3.6.1.4.1.1588.2.1.1.1.6.2.1.34" instance="brocadeFCPortIndex" alias="swFCPortWwn" type="hexstring" />
<mibObj oid=".1.3.6.1.4.1.1588.2.1.1.1.6.2.1.36" instance="brocadeFCPortIndex" alias="swFCPortName" type="string" />
</group>
</groups>
Expand Down
Expand Up @@ -333,3 +333,4 @@
.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.34.6 = Hex-STRING: 11:05:33:44:55:66:77:88
.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.34.7 = Hex-STRING: 11:06:33:44:55:66:77:88
.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.34.8 = Hex-STRING: 11:07:33:44:55:66:77:88
.1.3.6.1.4.1.1588.2.1.1.1.6.2.1.36.1 = Hex-STRING: 11:00:33:44:55:66:77:88

0 comments on commit 95eee04

Please sign in to comment.