/**
   * Sets the up.
   *
   * @throws Exception the exception
   */
  @Before
  public void setUp() throws Exception {
    FileUtils.deleteDirectory(new File(TEST_SNMP_DIRECTORY));
    MockLogAppender.setupLogging();

    System.setProperty("org.opennms.rrd.usetcp", "false");
    System.setProperty("org.opennms.rrd.usequeue", "false");
    RrdUtils.setStrategy(new JRobinRrdStrategy());

    m_collectionAgent = EasyMock.createMock(CollectionAgent.class);
    EasyMock.expect(m_collectionAgent.getNodeId()).andReturn(1).anyTimes();
    EasyMock.expect(m_collectionAgent.getHostAddress()).andReturn("127.0.0.1").anyTimes();
    EasyMock.expect(m_collectionAgent.getStorageDir()).andReturn(new File("1")).anyTimes();
    m_eventProxy = EasyMock.createMock(EventProxy.class);

    m_xmlCollectionDao = new XmlDataCollectionConfigDaoJaxb();
    Resource resource = new FileSystemResource(getXmlConfigFileName());
    m_xmlCollectionDao.setConfigResource(resource);
    m_xmlCollectionDao.afterPropertiesSet();
    MockDocumentBuilder.setXmlFileName(getXmlSampleFileName());

    EasyMock.replay(m_collectionAgent, m_eventProxy);
  }
Esempio n. 2
0
 /* (non-Javadoc)
  * @see org.opennms.netmgt.collectd.ServiceCollector#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.netmgt.model.events.EventProxy, java.util.Map)
  */
 @Override
 public CollectionSet collect(
     CollectionAgent agent, EventProxy eproxy, Map<String, Object> parameters)
     throws CollectionException {
   try {
     String collectionName = ParameterMap.getKeyedString(parameters, "collection", null);
     if (collectionName == null) {
       collectionName = ParameterMap.getKeyedString(parameters, "tca-collection", null);
     }
     if (collectionName == null) {
       throw new CollectionException("Parameter collection is required for the TCA Collector!");
     }
     TcaCollectionSet collectionSet =
         new TcaCollectionSet(agent, getRrdRepository(collectionName));
     collectionSet.setCollectionTimestamp(new Date());
     collectionSet.collect();
     return collectionSet;
   } catch (Throwable t) {
     throw new CollectionException(
         "Unexpected error during node TCA collection for: " + agent.getHostAddress() + ": " + t,
         t);
   }
 }
  /* (non-Javadoc)
   * @see org.opennms.protocols.xml.collector.XmlCollectionHandler#collect(org.opennms.netmgt.collectd.CollectionAgent, org.opennms.protocols.xml.config.XmlDataCollection, java.util.Map)
   */
  @Override
  public XmlCollectionSet collect(
      CollectionAgent agent, XmlDataCollection collection, Map<String, Object> parameters)
      throws CollectionException {
    // Create a new collection set.
    XmlCollectionSet collectionSet = new XmlCollectionSet(agent);
    collectionSet.setCollectionTimestamp(new Date());
    collectionSet.setStatus(ServiceCollector.COLLECTION_UNKNOWN);

    // TODO We could be careful when handling exceptions because parsing exceptions will be treated
    // different from connection or retrieval exceptions
    try {
      File resourceDir =
          new File(getRrdRepository().getRrdBaseDir(), Integer.toString(agent.getNodeId()));
      for (XmlSource source : collection.getXmlSources()) {
        if (!source.getUrl().startsWith(Sftp3gppUrlHandler.PROTOCOL)) {
          throw new CollectionException(
              "The 3GPP SFTP Collection Handler can only use the protocol "
                  + Sftp3gppUrlHandler.PROTOCOL);
        }
        String urlStr = parseUrl(source.getUrl(), agent, collection.getXmlRrd().getStep());
        URL url = UrlFactory.getUrl(urlStr);
        String lastFile = getLastFilename(resourceDir, url.getPath());
        Sftp3gppUrlConnection connection = (Sftp3gppUrlConnection) url.openConnection();
        if (lastFile == null) {
          lastFile = connection.get3gppFileName();
          log()
              .debug(
                  "collect(single): retrieving file from "
                      + url.getPath()
                      + File.separatorChar
                      + lastFile
                      + " from "
                      + agent.getHostAddress());
          Document doc = getXmlDocument(urlStr);
          fillCollectionSet(agent, collectionSet, source, doc);
          setLastFilename(resourceDir, url.getPath(), lastFile);
          deleteFile(connection, lastFile);
        } else {
          connection.connect();
          List<String> files = connection.getFileList();
          long lastTs = connection.getTimeStampFromFile(lastFile);
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          factory.setIgnoringComments(true);
          boolean collected = false;
          for (String fileName : files) {
            if (connection.getTimeStampFromFile(fileName) > lastTs) {
              log()
                  .debug(
                      "collect(multiple): retrieving file "
                          + fileName
                          + " from "
                          + agent.getHostAddress());
              InputStream is = connection.getFile(fileName);
              Document doc = builder.parse(is);
              IOUtils.closeQuietly(is);
              fillCollectionSet(agent, collectionSet, source, doc);
              setLastFilename(resourceDir, url.getPath(), fileName);
              deleteFile(connection, fileName);
              collected = true;
            }
          }
          if (!collected) {
            log().warn("collect: could not find any file after " + lastFile + " on " + agent);
          }
          connection.disconnect();
        }
      }
      collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
      return collectionSet;
    } catch (Exception e) {
      collectionSet.setStatus(ServiceCollector.COLLECTION_FAILED);
      throw new CollectionException(e.getMessage(), e);
    }
  }