use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class PersistRegexSelectorStrategyTest method setUp.
@Before
public void setUp() throws Exception {
ipInterfaceDao = EasyMock.createMock(IpInterfaceDao.class);
String localhost = InetAddress.getLocalHost().getHostAddress();
NetworkBuilder builder = new NetworkBuilder();
builder.addNode("myNode");
builder.addInterface(localhost).setIsManaged("M").setIsSnmpPrimary("P");
OnmsNode node = builder.getCurrentNode();
node.setId(1);
OnmsIpInterface ipInterface = node.getIpInterfaces().iterator().next();
EasyMock.expect(ipInterfaceDao.load(1)).andReturn(ipInterface).anyTimes();
EasyMock.replay(ipInterfaceDao);
Package pkg = new Package();
pkg.setName("junitTestPackage");
Filter filter = new Filter();
filter.setContent("IPADDR != '0.0.0.0'");
pkg.setFilter(filter);
Service service = new Service();
service.setName("SNMP");
pkg.addService(service);
Map<String, Object> map = new TreeMap<String, Object>();
List<org.opennms.netmgt.config.collectd.Parameter> params = pkg.getService("SNMP").getParameters();
for (org.opennms.netmgt.config.collectd.Parameter p : params) {
map.put(p.getKey(), p.getValue());
}
map.put("collection", "default");
serviceParams = new ServiceParameters(map);
LocationAwareSnmpClient locationAwareSnmpClient = new LocationAwareSnmpClientRpcImpl(new MockRpcClientFactory());
PlatformTransactionManager ptm = new MockPlatformTransactionManager();
SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(1, ipInterfaceDao, ptm);
OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, serviceParams, new MockDataCollectionConfigDao(), locationAwareSnmpClient);
org.opennms.netmgt.config.datacollection.ResourceType rt = new org.opennms.netmgt.config.datacollection.ResourceType();
rt.setName("myResourceType");
StorageStrategy storageStrategy = new StorageStrategy();
storageStrategy.setClazz("org.opennms.netmgt.collection.support.IndexStorageStrategy");
rt.setStorageStrategy(storageStrategy);
PersistenceSelectorStrategy persistenceSelectorStrategy = new PersistenceSelectorStrategy();
persistenceSelectorStrategy.setClazz("org.opennms.netmgt.collectd.PersistRegexSelectorStrategy");
Parameter param = new Parameter();
param.setKey(PersistRegexSelectorStrategy.MATCH_EXPRESSION);
param.setValue("#name matches '^agalue.*$'");
persistenceSelectorStrategy.addParameter(param);
rt.setPersistenceSelectorStrategy(persistenceSelectorStrategy);
GenericIndexResourceType resourceType = new GenericIndexResourceType(agent, snmpCollection, rt);
resourceA = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.1"));
AttributeGroupType groupType = new AttributeGroupType("mib2-interfaces", AttributeGroupType.IF_TYPE_ALL);
MibObject mibObject = new MibObject();
mibObject.setOid(".1.2.3.4.5.6.7.8.9.2.1");
mibObject.setInstance("1");
mibObject.setAlias("name");
mibObject.setType("string");
StringAttributeType attributeType = new StringAttributeType(resourceType, snmpCollection.getName(), mibObject, groupType);
SnmpValue snmpValue = new Snmp4JValueFactory().getOctetString("agalue rules!".getBytes());
resourceA.setAttributeValue(attributeType, snmpValue);
resourceB = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.2"));
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class SnmpIfAdmin method setIfAdmin.
/**
* <p>
* Set admin interface status to value.
* </p>
*
* @param ifindex
* interface index to set
* @param value
* desired interface status value
* @return The status of interface after operation
* @throws SnmpBadConversionException
* Throw if returned code is not an integer
* @throws java.sql.SQLException if any.
*/
public boolean setIfAdmin(int ifindex, int value) throws SQLException {
if (value != UP && value != DOWN)
throw new IllegalArgumentException("Value not valid");
SnmpObjId oid = SnmpObjId.get(snmpObjectId + "." + ifindex);
SnmpValue val = SnmpUtils.getValueFactory().getInt32(value);
SnmpValue result = SnmpUtils.set(m_agent, oid, val);
if (result != null && result.isNumeric()) {
int retvalue = result.toInt();
setIfAdminStatusInDB(ifindex, retvalue);
if (retvalue == value)
return true;
}
return false;
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class CiscoIpSlaDetector method isServiceDetected.
/**
* {@inheritDoc}
*
* Returns true if the protocol defined by this plugin is supported. If
* the protocol is not supported then a false value is returned to the
* caller. The qualifier map passed to the method is used by the plugin to
* return additional information by key-name. These key-value pairs can be
* added to service events if needed.
*/
@Override
public boolean isServiceDetected(final InetAddress address, final SnmpAgentConfig agentConfig) {
try {
configureAgentPTR(agentConfig);
configureAgentVersion(agentConfig);
Map<SnmpInstId, SnmpValue> tagResults = SnmpUtils.getOidValues(agentConfig, "CiscoIpSlaDetector", SnmpObjId.get(RTT_ADMIN_TAG_OID));
if (tagResults == null) {
LOG.warn("isServiceDetected: No admin tags received!");
return false;
}
Map<SnmpInstId, SnmpValue> operStateResults = SnmpUtils.getOidValues(agentConfig, "CiscoIpSlaDetector", SnmpObjId.get(RTT_OPER_STATE_OID));
if (operStateResults == null) {
LOG.warn("isServiceDetected: No operational states received!");
return false;
}
// Iterate over the list of configured IP SLAs
for (Entry<SnmpInstId, SnmpValue> ipslaEntry : tagResults.entrySet()) {
SnmpValue status = operStateResults.get(ipslaEntry.getKey());
LOG.debug("isServiceDetected: admin-tag={} value={} oper-state={}", m_adminTag, formatValue(ipslaEntry.getValue()), status.toInt());
// Check if a configured IP SLA with specific tag exist and is the operational state active
if (m_adminTag.equals(formatValue(ipslaEntry.getValue())) && status.toInt() == RTT_MON_OPER_STATE_ACTIVE) {
LOG.debug("isServiceDetected: admin tag found");
return true;
}
}
} catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
return false;
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class DiskUsageDetector method isServiceDetected.
/**
* {@inheritDoc}
*
* Returns true if the protocol defined by this plugin is supported. If the
* protocol is not supported then a false value is returned to the caller.
* The qualifier map passed to the method is used by the plugin to return
* additional information by key-name. These key-value pairs can be added to
* service events if needed.
*/
@Override
public boolean isServiceDetected(final InetAddress address, final SnmpAgentConfig agentConfig) {
int matchType = MATCH_TYPE_EXACT;
try {
if (getPort() > 0) {
agentConfig.setPort(getPort());
}
if (getTimeout() > 0) {
agentConfig.setTimeout(getTimeout());
}
if (getRetries() > -1) {
agentConfig.setRetries(getRetries());
}
if (getForceVersion() != null) {
String version = getForceVersion();
// @see http://issues.opennms.org/browse/NMS-7518
if ("v1".equalsIgnoreCase(version) || "snmpv1".equalsIgnoreCase(version)) {
agentConfig.setVersion(SnmpAgentConfig.VERSION1);
} else if ("v2".equalsIgnoreCase(version) || "v2c".equalsIgnoreCase(version) || "snmpv2".equalsIgnoreCase(version) || "snmpv2c".equalsIgnoreCase(version)) {
agentConfig.setVersion(SnmpAgentConfig.VERSION2C);
} else if ("v3".equalsIgnoreCase(version) || "snmpv3".equalsIgnoreCase(version)) {
agentConfig.setVersion(SnmpAgentConfig.VERSION3);
}
}
//
if (!"".equals(getMatchType())) {
String matchTypeStr = getMatchType();
if (matchTypeStr.equalsIgnoreCase("exact")) {
matchType = MATCH_TYPE_EXACT;
} else if (matchTypeStr.equalsIgnoreCase("startswith")) {
matchType = MATCH_TYPE_STARTSWITH;
} else if (matchTypeStr.equalsIgnoreCase("endswith")) {
matchType = MATCH_TYPE_ENDSWITH;
} else if (matchTypeStr.equalsIgnoreCase("regex")) {
matchType = MATCH_TYPE_REGEX;
} else {
throw new RuntimeException("Unknown value '" + matchTypeStr + "' for parameter 'match-type'");
}
}
SnmpObjId hrStorageDescrSnmpObject = SnmpObjId.get(getHrStorageDescr());
Map<SnmpInstId, SnmpValue> descrResults = SnmpUtils.getOidValues(agentConfig, "DiskUsagePoller", hrStorageDescrSnmpObject);
if (descrResults.size() == 0) {
return false;
}
for (Map.Entry<SnmpInstId, SnmpValue> e : descrResults.entrySet()) {
LOG.debug("capsd: SNMPwalk succeeded, addr={} oid={} instance={} value={}", InetAddressUtils.str(agentConfig.getAddress()), hrStorageDescrSnmpObject, e.getKey(), e.getValue());
if (isMatch(e.getValue().toString(), getDisk(), matchType)) {
LOG.debug("Found disk '{}' (matching hrStorageDescr was '{}')", getDisk(), e.getValue());
return true;
}
}
return false;
} catch (Throwable t) {
throw new UndeclaredThrowableException(t);
}
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class HostResourceSWRunDetector method isServiceDetected.
/**
* {@inheritDoc}
*
* Returns true if the protocol defined by this plugin is supported. If the
* protocol is not supported then a false value is returned to the caller.
* The qualifier map passed to the method is used by the plugin to return
* additional information by key-name. These key-value pairs can be added to
* service events if needed.
*/
@Override
public boolean isServiceDetected(final InetAddress address, final SnmpAgentConfig agentConfig) {
boolean status = false;
// Get configuration parameters
//
// This is the string that represents the service name to be monitored.
String serviceName = getServiceToDetect();
// set timeout and retries on SNMP peer object
//
configureAgentPTR(agentConfig);
LOG.debug("capsd: service= SNMP address={}", agentConfig);
// Establish SNMP session with interface
//
final String hostAddress = InetAddressUtils.str(agentConfig.getAddress());
try {
LOG.debug("HostResourceSwRunMonitor.poll: SnmpAgentConfig address: {}", agentConfig);
if (serviceName == null) {
LOG.warn("HostResourceSwRunMonitor.poll: No Service Name Defined! ");
return status;
}
// This returns two maps: one of instance and service name, and one of instance and status.
Map<SnmpInstId, SnmpValue> nameResults = SnmpUtils.getOidValues(agentConfig, "HostResourceSwRunMonitor", SnmpObjId.get(getServiceNameOid()));
// Iterate over the list of running services
for (Entry<SnmpInstId, SnmpValue> entry : nameResults.entrySet()) {
SnmpValue value = entry.getValue();
// See if the service name is in the list of running services
if (match(serviceName, StringUtils.stripExtraQuotes(value.toString())) && !status) {
LOG.debug("poll: HostResourceSwRunMonitor poll succeeded, addr={} service name={} value={}", hostAddress, serviceName, value);
status = true;
break;
}
}
} catch (NumberFormatException e) {
LOG.warn("Number operator used on a non-number {}", e.getMessage());
} catch (IllegalArgumentException e) {
LOG.warn("Invalid SNMP Criteria: {}", e.getMessage());
} catch (Throwable t) {
LOG.warn("Unexpected exception during SNMP poll of interface {}", hostAddress, t);
}
return status;
}
Aggregations