Search in sources :

Example 16 with CollectionSetBuilder

use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.

the class CollectableServiceTest method canWrapResourcesWithTimekeeper.

/**
 * Validates that we can successfully wrap collection sets with a custom time-keeper,
 * allowing us to override the timestamp of the attributes within the collection set.
 */
@Test
public void canWrapResourcesWithTimekeeper() throws CollectionInitializationException, CollectionException, IOException, RrdException {
    System.setProperty(CollectableService.USE_COLLECTION_START_TIME_SYS_PROP, Boolean.TRUE.toString());
    createCollectableService();
    long collectionDelayInSecs = 2;
    when(spec.collect(any())).then(new Answer<CollectionSet>() {

        @Override
        public CollectionSet answer(InvocationOnMock invocation) throws InterruptedException {
            Thread.sleep(collectionDelayInSecs * 1000);
            CollectionAgent agent = (CollectionAgent) invocation.getArguments()[0];
            NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
            return new CollectionSetBuilder(agent).withNumericAttribute(nodeResource, "mibGroup", "myCounter", 1000, AttributeType.COUNTER).build();
        }
    });
    File nodeDir = fileAnticipator.expecting(getSnmpRrdDirectory(), "1");
    File jrbFile = fileAnticipator.expecting(nodeDir, "myCounter" + rrdStrategy.getDefaultFileExtension());
    fileAnticipator.expecting(nodeDir, "myCounter" + ".meta");
    long beforeInMs = System.currentTimeMillis();
    service.run();
    long afterInMs = System.currentTimeMillis();
    // Quick sanity check
    assertTrue(String.format("Delay was not succesfully applied (delay was %d).", beforeInMs - afterInMs), afterInMs - beforeInMs >= collectionDelayInSecs * 1000);
    // Verify the last update time match the start of the collection time
    RrdDb rrdDb = new RrdDb(jrbFile);
    long lastUpdateTimeInSecs = rrdDb.getLastUpdateTime();
    long beforeInSecs = Math.floorDiv(beforeInMs, 1000);
    long afterInSecs = Math.floorDiv(afterInMs, 1000) + 1;
    assertTrue("Last update was before the collector was invoked!", lastUpdateTimeInSecs >= beforeInSecs);
    assertTrue("Last update was too long after the collector was invoked!", lastUpdateTimeInSecs < (afterInSecs - (collectionDelayInSecs / 2d)));
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RrdDb(org.jrobin.core.RrdDb) CollectionAgent(org.opennms.netmgt.collection.api.CollectionAgent) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) File(java.io.File) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) Test(org.junit.Test)

Example 17 with CollectionSetBuilder

use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.

the class XmpCollector method collect.

/**
 * {@inheritDoc}
 *
 * Collect data, via XMP, from a particular agent EventProxy is
 *       used to send opennms events into the system in case a
 *       collection fails or if a system is back working again after a
 *       failure (suceed event).  But otherwise, no events sent if
 *       collection succeeds.  Collect is called once per agent per
 *       collection cycle.  Parameters are a map of String Key/String
 *       Value passed in.  Keys come from collectd config
 * @throws CollectionException
 */
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    CollectionSetBuilder collectionSetBuilder;
    XmpSession session;
    long oldUptime;
    int i;
    XmpCollection collection;
    LOG.debug("collect agent {}", agent);
    oldUptime = 0;
    // First go to the peer factory
    XmpAgentConfig peerConfig = XmpPeerFactory.getInstance().getAgentConfig(agent.getAddress());
    authenUser = peerConfig.getAuthenUser();
    timeout = (int) peerConfig.getTimeout();
    retries = peerConfig.getRetry();
    xmpPort = peerConfig.getPort();
    if (parameters.get("authenUser") != null)
        authenUser = ParameterMap.getKeyedString(parameters, "authenUser", null);
    if (parameters.get("timeout") != null) {
        timeout = ParameterMap.getKeyedInteger(parameters, "timeout", 3000);
    }
    if (parameters.get("retry") != null) {
        retries = ParameterMap.getKeyedInteger(parameters, "retries", 0);
    }
    parameters.get("collection");
    if (parameters.get("port") != null) {
        xmpPort = Integer.valueOf((String) parameters.get("port"));
    }
    // log().debug("collect got parameters for "+agent);
    String collectionName = ParameterMap.getKeyedString(parameters, "collection", null);
    // this would/will come from xmp-datacollection.xml
    if (collectionName == null) {
        // log this!
        LOG.warn("collect found no collectionName for {}", agent);
        return null;
    }
    // log().debug("collect got collectionName for "+agent);
    LOG.debug("XmpCollector: collect {} from {}", collectionName, agent);
    // get/create our collections set
    collectionSetBuilder = new CollectionSetBuilder(agent).withStatus(// default to failed
    CollectionStatus.FAILED).disableCounterPersistence(// don't persist counters by default
    true);
    // default collection resource for putting scalars in
    final NodeLevelResource nodeLevelResource = new NodeLevelResource(agent.getNodeId());
    // get the collection, again, from the data config file factory
    // because it could have changed; its not necessarily re-parsed,
    // but we are getting another copy of it for each agent
    // that we are queried each time we are invoked
    collection = XmpCollectionFactory.getInstance().getXmpCollection(collectionName);
    if (collection == null) {
        LOG.warn("collect found no matching collection for {}", agent);
        return collectionSetBuilder.build();
    }
    if (collection.getGroups().getGroup().length < 1) {
        LOG.info("No groups to collect.");
        return collectionSetBuilder.withStatus(CollectionStatus.SUCCEEDED).build();
    }
    oldUptime = agent.getSavedSysUpTime();
    // open/get a session with the target agent
    LOG.debug("collect: attempting to open XMP session with {}:{},{}", agent.getAddress(), xmpPort, authenUser);
    // Set the SO_TIMEOUT, why don't we...
    sockopts.setConnectTimeout(timeout);
    session = new XmpSession(sockopts, agent.getAddress(), xmpPort, authenUser);
    if (session.isClosed()) {
        LOG.warn("collect unable to open XMP session with {}", agent);
        return collectionSetBuilder.build();
    }
    LOG.debug("collect: successfully opened XMP session with{}", agent);
    for (Group group : collection.getGroups().getGroup()) {
        // get name of group and MIB objects in group
        String groupName = group.getName();
        MibObj[] mibObjects = group.getMibObj();
        XmpVar[] vars = new XmpVar[mibObjects.length];
        LOG.debug("collecting XMP group {} with {} mib objects", groupName, mibObjects.length);
        // prepare the query vars
        for (i = 0; i < mibObjects.length; i++) {
            vars[i] = new XmpVar(mibObjects[i].getMib(), mibObjects[i].getVar(), mibObjects[i].getInstance(), "", Xmp.SYNTAX_NULLSYNTAX);
        }
        if ((mibObjects[0].getTable() != null) && (mibObjects[0].getTable().length() != 0)) {
            String[] tableInfo = new String[3];
            tableInfo[0] = mibObjects[0].getMib();
            tableInfo[1] = mibObjects[0].getTable();
            tableInfo[2] = mibObjects[0].getInstance();
            // tabular query
            if (handleTableQuery(group.getName(), group.getResourceType(), agent, collectionSetBuilder, tableInfo, session, nodeLevelResource, vars) == false) {
                session.closeSession();
                return collectionSetBuilder.build();
            }
        } else {
            // scalar query
            if (handleScalarQuery(group.getName(), agent, collectionSetBuilder, oldUptime, session, nodeLevelResource, vars) == false) {
                session.closeSession();
                return collectionSetBuilder.build();
            }
        }
    }
    /* for each Group in collection Group list */
    // done talking to this agent; close session
    session.closeSession();
    // Did agent restart since last query?  If so, set
    // ignorePersist to true; our scalar
    // query will have handled this by searching returned
    // MIB objects for sysUpTime
    // WARNING, EACH COLLECTION SHOULD HAVE A SCALAR QUERY THAT
    // INCLUDES Core.sysUpTime
    collectionSetBuilder.withStatus(CollectionStatus.SUCCEEDED);
    LOG.debug("XMP collect finished for {}, uptime for {} is {}", collectionName, agent, agent.getSavedSysUpTime());
    return collectionSetBuilder.build();
}
Also used : Group(org.opennms.netmgt.config.xmpDataCollection.Group) CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) MibObj(org.opennms.netmgt.config.xmpDataCollection.MibObj) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) XmpVar(org.krupczak.xmp.XmpVar) XmpSession(org.krupczak.xmp.XmpSession) XmpAgentConfig(org.opennms.netmgt.protocols.xmp.config.XmpAgentConfig) XmpCollection(org.opennms.netmgt.config.xmpDataCollection.XmpCollection)

Example 18 with CollectionSetBuilder

use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.

the class WmiCollector method collect.

/**
 * {@inheritDoc}
 */
@Override
public CollectionSet collect(final CollectionAgent agent, final Map<String, Object> parameters) {
    // Find attributes to collect - check groups in configuration. For each,
    // check scheduled nodes to see if that group should be collected
    final WmiCollection collection = (WmiCollection) parameters.get(WMI_COLLECTION_KEY);
    final WmiAgentConfig agentConfig = (WmiAgentConfig) parameters.get(WMI_AGENT_CONFIG_KEY);
    final WmiAgentState agentState = new WmiAgentState(agent.getAddress(), agentConfig, parameters);
    // Create a new collection set.
    CollectionSetBuilder builder = new CollectionSetBuilder(agent).withStatus(CollectionStatus.FAILED);
    if (collection.getWpms().size() < 1) {
        LOG.info("No groups to collect.");
        return builder.withStatus(CollectionStatus.SUCCEEDED).build();
    }
    final NodeLevelResource nodeResource = new NodeLevelResource(agent.getNodeId());
    // Iterate through the WMI collection groups.
    for (final Wpm wpm : collection.getWpms()) {
        // A wpm consists of a list of attributes, identified by name
        if (agentState.shouldCheckAvailability(wpm.getName(), wpm.getRecheckInterval())) {
            if (!isGroupAvailable(agentState, wpm)) {
                continue;
            }
        }
        if (agentState.groupIsAvailable(wpm.getName())) {
            WmiClient client = null;
            // Collect the data
            try {
                // Tell the agent to connect
                agentState.connect(wpm.getWmiNamespace());
                // And retrieve the client object for working.
                client = (WmiClient) agentState.getWmiClient();
                // Retrieve the WbemObjectSet from the class defined on the group.
                final OnmsWbemObjectSet wOS = client.performInstanceOf(wpm.getWmiClass());
                // If we received a WbemObjectSet result, lets go through it and collect it.
                if (wOS != null) {
                    // Go through each object (class instance) in the object set.
                    for (int i = 0; i < wOS.count(); i++) {
                        // Create a new collection resource.
                        Resource resource = null;
                        // Fetch our WBEM Object
                        final OnmsWbemObject obj = wOS.get(i);
                        // If this is multi-instance, fetch the instance name and store it.
                        if (wOS.count() > 1) {
                            // Fetch the value of the key value. e.g. Name.
                            final OnmsWbemProperty prop = obj.getWmiProperties().getByName(wpm.getKeyvalue());
                            final Object propVal = prop.getWmiValue();
                            String instance = null;
                            if (propVal instanceof String) {
                                instance = (String) propVal;
                            } else {
                                instance = propVal.toString();
                            }
                            resource = getWmiResource(agent, wpm.getResourceType(), nodeResource, instance);
                        } else {
                            resource = nodeResource;
                        }
                        for (final Attrib attrib : wpm.getAttribs()) {
                            final OnmsWbemProperty prop = obj.getWmiProperties().getByName(attrib.getWmiObject());
                            final AttributeType type = attrib.getType();
                            final String stringValue = prop.getWmiValue().toString();
                            if (type.isNumeric()) {
                                Double numericValue = Double.NaN;
                                try {
                                    numericValue = Double.parseDouble(stringValue);
                                } catch (NumberFormatException e) {
                                    LOG.warn("Value '{}' for attribute named '{}' cannot be converted to a number. Skipping.", prop.getWmiValue(), attrib.getName());
                                    continue;
                                }
                                builder.withNumericAttribute(resource, wpm.getName(), attrib.getAlias(), numericValue, type);
                            } else {
                                builder.withStringAttribute(resource, wpm.getName(), attrib.getAlias(), stringValue);
                            }
                        }
                    }
                }
                builder.withStatus(CollectionStatus.SUCCEEDED);
            } catch (final WmiException e) {
                LOG.info("unable to collect params for wpm '{}'", wpm.getName(), e);
            } finally {
                if (client != null) {
                    try {
                        client.disconnect();
                    } catch (final WmiException e) {
                        LOG.warn("An error occurred disconnecting while collecting from WMI.", e);
                    }
                }
            }
        }
    }
    return builder.build();
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) WmiAgentConfig(org.opennms.netmgt.config.wmi.WmiAgentConfig) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) Resource(org.opennms.netmgt.collection.support.builder.Resource) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) OnmsWbemProperty(org.opennms.protocols.wmi.wbem.OnmsWbemProperty) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) WmiCollection(org.opennms.netmgt.config.wmi.WmiCollection) Attrib(org.opennms.netmgt.config.wmi.Attrib) Wpm(org.opennms.netmgt.config.wmi.Wpm) WmiClient(org.opennms.protocols.wmi.WmiClient) AttributeType(org.opennms.netmgt.collection.api.AttributeType) OnmsWbemObject(org.opennms.protocols.wmi.wbem.OnmsWbemObject) WmiAgentState(org.opennms.netmgt.collectd.wmi.WmiAgentState) OnmsWbemObjectSet(org.opennms.protocols.wmi.wbem.OnmsWbemObjectSet) WmiException(org.opennms.protocols.wmi.WmiException) OnmsWbemObject(org.opennms.protocols.wmi.wbem.OnmsWbemObject)

Example 19 with CollectionSetBuilder

use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.

the class EvaluateStatsIT method testPersister.

/**
 * Test persister.
 *
 * @throws Exception the exception
 */
@Test
public void testPersister() throws Exception {
    RrdRepository repo = new RrdRepository();
    repo.setRrdBaseDir(new File("/tmp"));
    EvaluateGroupPersister persister = new EvaluateGroupPersister(stats, new ServiceParameters(new HashMap<String, Object>()), repo);
    MockCollectionAgent agent = new MockCollectionAgent(1, "node.local", "Test", "001", InetAddressUtils.addr("127.0.0.1"));
    CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    NodeLevelResource node = new NodeLevelResource(agent.getNodeId());
    InterfaceLevelResource eth0 = new InterfaceLevelResource(node, "eth0");
    builder.withNumericAttribute(eth0, "mib2-interfaces", "ifInErrors", 0.0, AttributeType.COUNTER);
    builder.withNumericAttribute(eth0, "mib2-interfaces", "ifOutErrors", 0.0, AttributeType.COUNTER);
    builder.withNumericAttribute(eth0, "mib2-X-interfaces", "ifHCInOctets", 100.0, AttributeType.COUNTER);
    builder.withNumericAttribute(eth0, "mib2-X-interfaces", "ifHCOutOctets", 100.0, AttributeType.COUNTER);
    builder.withStringAttribute(eth0, "mib2-X-interfaces", "ifHighSpeed", "1000");
    InterfaceLevelResource eth1 = new InterfaceLevelResource(node, "eth1");
    builder.withNumericAttribute(eth1, "mib2-interfaces", "ifInErrors", 0.0, AttributeType.COUNTER);
    builder.withNumericAttribute(eth1, "mib2-interfaces", "ifOutErrors", 0.0, AttributeType.COUNTER);
    builder.withNumericAttribute(eth1, "mib2-X-interfaces", "ifHCInOctets", 100.0, AttributeType.COUNTER);
    builder.withNumericAttribute(eth1, "mib2-X-interfaces", "ifHCOutOctets", 100.0, AttributeType.COUNTER);
    builder.withStringAttribute(eth1, "mib2-X-interfaces", "ifHighSpeed", "1000");
    builder.build().visit(persister);
    stats.dumpCache();
    Assert.assertEquals(1, registry.getGauges().get("evaluate.nodes").getValue());
    Assert.assertEquals(2, registry.getGauges().get("evaluate.resources").getValue());
    Assert.assertEquals(4, registry.getGauges().get("evaluate.groups").getValue());
    Assert.assertEquals(8, registry.getGauges().get("evaluate.numeric-attributes").getValue());
    Assert.assertEquals(2, registry.getGauges().get("evaluate.string-attributes").getValue());
    Assert.assertEquals(8, registry.getMeters().get("evaluate.samples").getCount());
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) HashMap(java.util.HashMap) MockCollectionAgent(org.opennms.core.collection.test.MockCollectionAgent) InterfaceLevelResource(org.opennms.netmgt.collection.support.builder.InterfaceLevelResource) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) File(java.io.File) Test(org.junit.Test)

Example 20 with CollectionSetBuilder

use of org.opennms.netmgt.collection.support.builder.CollectionSetBuilder in project opennms by OpenNMS.

the class ScriptedCollectionSetBuilder method build.

/**
 * Builds a collection set from the given message.
 *
 * WARNING: This method is not necessarily thread safe. This depends on the
 * script, and the script engine that is being used.
 *
 * @param agent
 *            the agent associated with the collection set
 * @param message
 *            the messaged passed to script containing the metrics
 * @return a collection set
 * @throws ScriptException
 */
public CollectionSet build(CollectionAgent agent, Object message) throws ScriptException {
    final CollectionSetBuilder builder = new CollectionSetBuilder(agent);
    final SimpleBindings globals = new SimpleBindings();
    globals.put("agent", agent);
    globals.put("builder", builder);
    globals.put("msg", message);
    compiledScript.eval(globals);
    return builder.build();
}
Also used : CollectionSetBuilder(org.opennms.netmgt.collection.support.builder.CollectionSetBuilder) SimpleBindings(javax.script.SimpleBindings)

Aggregations

CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)31 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)21 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)13 Test (org.junit.Test)10 CollectionAgent (org.opennms.netmgt.collection.api.CollectionAgent)10 CollectionException (org.opennms.netmgt.collection.api.CollectionException)10 GenericTypeResource (org.opennms.netmgt.collection.support.builder.GenericTypeResource)9 Resource (org.opennms.netmgt.collection.support.builder.Resource)9 ResourcePath (org.opennms.netmgt.model.ResourcePath)9 AttributeType (org.opennms.netmgt.collection.api.AttributeType)8 List (java.util.List)6 Date (java.util.Date)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 InetAddressUtils (org.opennms.core.utils.InetAddressUtils)5 DeferredGenericTypeResource (org.opennms.netmgt.collection.support.builder.DeferredGenericTypeResource)5 Group (org.opennms.netmgt.config.wsman.Group)5 Maps (com.google.common.collect.Maps)4 Sets (com.google.common.collect.Sets)4 XMLDoc (com.mycila.xmltool.XMLDoc)4