Search in sources :

Example 1 with XmpAgentConfig

use of org.opennms.netmgt.protocols.xmp.config.XmpAgentConfig 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)

Aggregations

XmpSession (org.krupczak.xmp.XmpSession)1 XmpVar (org.krupczak.xmp.XmpVar)1 CollectionSetBuilder (org.opennms.netmgt.collection.support.builder.CollectionSetBuilder)1 NodeLevelResource (org.opennms.netmgt.collection.support.builder.NodeLevelResource)1 Group (org.opennms.netmgt.config.xmpDataCollection.Group)1 MibObj (org.opennms.netmgt.config.xmpDataCollection.MibObj)1 XmpCollection (org.opennms.netmgt.config.xmpDataCollection.XmpCollection)1 XmpAgentConfig (org.opennms.netmgt.protocols.xmp.config.XmpAgentConfig)1