Search in sources :

Example 26 with ServiceParameters

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

the class SnmpCollector method collect.

/**
 * {@inheritDoc}
 *
 * Perform data collection.
 */
@Override
public CollectionSet collect(CollectionAgent agent, Map<String, Object> parameters) throws CollectionException {
    try {
        final ServiceParameters params = new ServiceParameters(parameters);
        params.logIfAliasConfig();
        if (m_client == null) {
            m_client = BeanUtils.getBean("daoContext", "locationAwareSnmpClient", LocationAwareSnmpClient.class);
        }
        OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection((SnmpCollectionAgent) agent, params, m_client);
        final EventProxy eventProxy = EventIpcManagerFactory.getIpcManager();
        final ForceRescanState forceRescanState = new ForceRescanState(agent, eventProxy);
        SnmpCollectionSet collectionSet = snmpCollection.createCollectionSet((SnmpCollectionAgent) agent);
        collectionSet.setCollectionTimestamp(new Date());
        if (!collectionSet.hasDataToCollect()) {
            LOG.info("agent {} defines no data to collect.  Skipping.", agent);
        // should we return here?
        }
        collectionSet.collect();
        /*
             * FIXME: Should we even be doing this? I say we get rid of this force rescan thingie
             * {@see http://issues.opennms.org/browse/NMS-1057}
             */
        if (System.getProperty("org.opennms.netmgt.collectd.SnmpCollector.forceRescan", "false").equalsIgnoreCase("true") && collectionSet.rescanNeeded()) {
            /*
                 * TODO: the behavior of this object may have been re-factored away.
                 * Verify that this is correct and remove this unused object if it
                 * is no longer needed.  My gut thinks this should be investigated.
                 */
            forceRescanState.rescanIndicated();
        } else {
            collectionSet.checkForSystemRestart();
        }
        return collectionSet;
    } catch (CollectionException e) {
        throw e;
    } catch (Throwable t) {
        throw new CollectionException("Unexpected error during node SNMP collection for: " + agent.getHostAddress(), t);
    }
}
Also used : LocationAwareSnmpClient(org.opennms.netmgt.snmp.proxy.LocationAwareSnmpClient) CollectionException(org.opennms.netmgt.collection.api.CollectionException) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) EventProxy(org.opennms.netmgt.events.api.EventProxy) Date(java.util.Date)

Example 27 with ServiceParameters

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

the class ThresholdingVisitorIT method testReloadThreshdConfig.

/*
     * Use case A:
     * 
     * I have 5 nodes. The current threshd-config matches 2 of them. The new threshd-config will match the other 2, by
     * adding a new threshold package. For example: n1 y n2 belongs to category CAT1, n2, n3 y n4 belongs to category CAT2.
     * The initial configuration is related with CAT1 and the new package is related with CAT2. In both cases, n5 should
     * never match any threshold package.
     * 
     * Use case B:
     * 
     * I have a package with SNMP thresholds. Then update the package by adding HTTP thresholds. The test node should
     * support both services.
     * 
     * IMPORTANT:
     *     The reload should be do it first, then notify all visitors (I think this is the current behavior)
     *     The reload should not be executed inside the visitor because every collector thread has their own visitor.
     */
@Test
public void testReloadThreshdConfig() throws Exception {
    String baseIpAddress = "10.0.0.";
    // Initialize Mock Network
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    for (int i = 1; i <= 5; i++) {
        String ipAddress = baseIpAddress + i;
        network.addNode(i, "testNode-" + ipAddress);
        network.addInterface(ipAddress);
        network.setIfAlias("eth0");
        network.addService("ICMP");
        network.addService("SNMP");
        if (i == 5) {
            // Adding HTTP on node 5
            network.addService("HTTP");
        }
    }
    network.addPathOutage(1, InetAddressUtils.addr("192.168.1.1"), "ICMP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 10, "CAT1");
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 11, "CAT2");
    for (int i = 1; i <= 5; i++) {
        db.update("update snmpinterface set snmpifname=?, snmpifdescr=? where id=?", "eth0", "eth0", i);
        db.update("update node set nodesysoid=? where nodeid=?", ".1.3.6.1.4.1.9.1.222", i);
    }
    for (int i = 1; i <= 2; i++) {
        db.update("insert into category_node values (?, ?)", 10, i);
    }
    for (int i = 3; i <= 5; i++) {
        db.update("insert into category_node values (?, ?)", 11, i);
    }
    DataSourceFactory.setInstance(db);
    // Initialize Filter DAO
    System.setProperty("opennms.home", "src/test/resources");
    DatabaseSchemaConfigFactory.init();
    JdbcFilterDao jdbcFilterDao = new JdbcFilterDao();
    jdbcFilterDao.setDataSource(db);
    jdbcFilterDao.setDatabaseSchemaConfigFactory(DatabaseSchemaConfigFactory.getInstance());
    jdbcFilterDao.afterPropertiesSet();
    FilterDaoFactory.setInstance(jdbcFilterDao);
    // Initialize Factories
    initFactories("/threshd-configuration-reload-use-case-a.xml", "/test-thresholds-reload-use-cases.xml");
    // Initialize Thresholding Visitors
    System.err.println("-----------------------------------------------------------------------------------");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("thresholding-enabled", "true");
    ServiceParameters svcParams = new ServiceParameters(params);
    List<ThresholdingVisitor> visitors = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        String ipAddress = baseIpAddress + i;
        ThresholdingVisitor visitor = ThresholdingVisitor.create(i, ipAddress, "SNMP", getRepository(), svcParams, m_resourceStorageDao);
        assertNotNull(visitor);
        visitors.add(visitor);
        if (i == 5) {
            ThresholdingVisitor httpVisitor = ThresholdingVisitor.create(i, ipAddress, "HTTP", getRepository(), svcParams, m_resourceStorageDao);
            assertNotNull(httpVisitor);
            visitors.add(httpVisitor);
        }
    }
    System.err.println("-----------------------------------------------------------------------------------");
    // Check Visitors
    for (int i = 0; i < 2; i++) {
        // Nodes n1 and n2 has thresholds defined on one threshold group.
        assertTrue(visitors.get(i).hasThresholds());
        assertEquals(1, visitors.get(i).getThresholdGroups().size());
    }
    for (int i = 2; i < 6; i++) {
        // Nodes n3, n4 and n5 should not have thresholds defined.
        assertFalse(visitors.get(i).hasThresholds());
        assertEquals(0, visitors.get(i).getThresholdGroups().size());
    }
    // Re-Initialize Factories
    initFactories("/threshd-configuration-reload-use-case-b.xml", "/test-thresholds-reload-use-cases.xml");
    // Reload state on each visitor
    System.err.println("-----------------------------------------------------------------------------------");
    for (ThresholdingVisitor visitor : visitors) {
        visitor.reload();
    }
    System.err.println("-----------------------------------------------------------------------------------");
    // Check Visitors
    for (int i = 0; i < 6; i++) {
        assertTrue(visitors.get(i).hasThresholds());
        assertEquals(1, visitors.get(i).getThresholdGroups().size());
        if (i == 5) {
            assertEquals("web-services", visitors.get(i).getThresholdGroups().get(0).getName());
        }
    }
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockDatabase(org.opennms.core.test.db.MockDatabase) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) JdbcFilterDao(org.opennms.netmgt.filter.JdbcFilterDao) Test(org.junit.Test)

Example 28 with ServiceParameters

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

the class ThresholdingVisitorIT method runTestForBug3554.

private void runTestForBug3554() throws Exception {
    MockLogAppender.resetState();
    System.err.println("----------------------------------------------------------------------------------- begin test");
    String baseIpAddress = "10.0.0.";
    int numOfNodes = 5;
    // Initialize Mock Network
    MockNetwork network = new MockNetwork();
    network.setCriticalService("ICMP");
    for (int i = 1; i <= numOfNodes; i++) {
        String ipAddress = baseIpAddress + i;
        network.addNode(i, "testNode-" + ipAddress);
        network.addInterface(ipAddress);
        network.setIfAlias("eth0");
        network.addService("ICMP");
        network.addService("SNMP");
    }
    network.addPathOutage(1, InetAddressUtils.addr("192.168.1.1"), "ICMP");
    MockDatabase db = new MockDatabase();
    db.populate(network);
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 10, "IPRA");
    db.update("insert into categories (categoryid, categoryname) values (?, ?)", 11, "NAS");
    for (int i = 1; i <= numOfNodes; i++) {
        db.update("update snmpinterface set snmpifname=?, snmpifdescr=? where id=?", "eth0", "eth0", i);
        db.update("update node set nodesysoid=? where nodeid=?", ".1.3.6.1.4.1.9.1.222", i);
        db.update("insert into category_node values (?, ?)", 10, i);
        db.update("insert into category_node values (?, ?)", 11, i);
    }
    DataSourceFactory.setInstance(db);
    // Initialize Filter DAO
    System.setProperty("opennms.home", "src/test/resources");
    DatabaseSchemaConfigFactory.init();
    JdbcFilterDao jdbcFilterDao = new JdbcFilterDao();
    jdbcFilterDao.setDataSource(db);
    jdbcFilterDao.setDatabaseSchemaConfigFactory(DatabaseSchemaConfigFactory.getInstance());
    jdbcFilterDao.afterPropertiesSet();
    FilterDaoFactory.setInstance(jdbcFilterDao);
    // Initialize Factories
    initFactories("/threshd-configuration-bug3554.xml", "/test-thresholds-bug3554.xml");
    // Initialize Thresholding Visitors
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("thresholding-enabled", "true");
    ServiceParameters svcParams = new ServiceParameters(params);
    for (int i = 1; i <= numOfNodes; i++) {
        System.err.println("----------------------------------------------------------------------------------- visitor #" + i);
        String ipAddress = baseIpAddress + i;
        ThresholdingVisitor visitor = ThresholdingVisitor.create(1, ipAddress, "SNMP", getRepository(), svcParams, m_resourceStorageDao);
        assertNotNull(visitor);
        // mib2, cisco, ciscoIPRA, ciscoNAS
        assertEquals(4, visitor.getThresholdGroups().size());
    }
    System.err.println("----------------------------------------------------------------------------------- end");
}
Also used : MockNetwork(org.opennms.netmgt.mock.MockNetwork) HashMap(java.util.HashMap) MockDatabase(org.opennms.core.test.db.MockDatabase) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) JdbcFilterDao(org.opennms.netmgt.filter.JdbcFilterDao)

Example 29 with ServiceParameters

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

the class HttpDataCollectionIT method testJsonHttpCollection.

/**
 * Test HTTP Data Collection with JSON
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testJsonHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/solaris-zones-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("Solaris");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "Solaris");
    DefaultJsonCollectionHandler collector = new DefaultJsonCollectionHandler();
    collector.setRrdRepository(repository);
    collector.setServiceName("HTTP");
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_nodeDao, collector, m_collectionAgent, collection, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    System.err.println(CollectionSetUtils.flatten(collectionSet));
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, repository, false, false);
    collectionSet.visit(persister);
    RrdDb jrb = new RrdDb(new File(getSnmpRoot(), "1/solarisZoneStats/global/solaris-zone-stats.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(6, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("nproc");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(245.0), Double.valueOf(ds.getLastValue()));
}
Also used : Datasource(org.jrobin.core.Datasource) DefaultJsonCollectionHandler(org.opennms.protocols.json.collector.DefaultJsonCollectionHandler) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) File(java.io.File) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Example 30 with ServiceParameters

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

the class HttpDataCollectionIT method testHttpCollection.

/**
 * Test HTTP Data Collection with XPath
 *
 * @throws Exception the exception
 */
@Test
@JUnitHttpServer(port = 10342, https = false, webapps = { @Webapp(context = "/junit", path = "src/test/resources/test-webapp") })
public void testHttpCollection() throws Exception {
    File configFile = new File("src/test/resources/http-datacollection-config.xml");
    XmlDataCollectionConfig config = JaxbUtils.unmarshal(XmlDataCollectionConfig.class, configFile);
    XmlDataCollection collection = config.getDataCollectionByName("Http-Count");
    RrdRepository repository = createRrdRepository(collection.getXmlRrd());
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "Http-Count");
    DefaultXmlCollectionHandler collector = new DefaultXmlCollectionHandler();
    collector.setRrdRepository(repository);
    collector.setServiceName("HTTP");
    CollectionSet collectionSet = XmlCollectorTestUtils.doCollect(m_nodeDao, collector, m_collectionAgent, collection, parameters);
    Assert.assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
    ServiceParameters serviceParams = new ServiceParameters(new HashMap<String, Object>());
    CollectionSetVisitor persister = m_persisterFactory.createGroupPersister(serviceParams, repository, false, false);
    collectionSet.visit(persister);
    RrdDb jrb = new RrdDb(new File(getSnmpRoot(), "1/count-stats.jrb"));
    Assert.assertNotNull(jrb);
    Assert.assertEquals(1, jrb.getDsCount());
    Datasource ds = jrb.getDatasource("count");
    Assert.assertNotNull(ds);
    Assert.assertEquals(new Double(5), Double.valueOf(ds.getLastValue()));
}
Also used : Datasource(org.jrobin.core.Datasource) XmlDataCollectionConfig(org.opennms.protocols.xml.config.XmlDataCollectionConfig) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) XmlDataCollection(org.opennms.protocols.xml.config.XmlDataCollection) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) File(java.io.File) Test(org.junit.Test) JUnitHttpServer(org.opennms.core.test.http.annotations.JUnitHttpServer)

Aggregations

ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)44 HashMap (java.util.HashMap)27 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)19 Test (org.junit.Test)18 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)15 MockDataCollectionConfig (org.opennms.netmgt.mock.MockDataCollectionConfig)14 File (java.io.File)10 CollectionSet (org.opennms.netmgt.collection.api.CollectionSet)10 OnmsSnmpCollection (org.opennms.netmgt.collectd.OnmsSnmpCollection)9 MibObject (org.opennms.netmgt.config.datacollection.MibObject)9 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)7 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)7 NodeResourceType (org.opennms.netmgt.collectd.NodeResourceType)6 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)6 Datasource (org.jrobin.core.Datasource)5 RrdDb (org.jrobin.core.RrdDb)5 SnmpCollectionAgent (org.opennms.netmgt.collectd.SnmpCollectionAgent)5 SnmpCollectionResource (org.opennms.netmgt.collectd.SnmpCollectionResource)5 CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)5 JUnitHttpServer (org.opennms.core.test.http.annotations.JUnitHttpServer)4