Ejemplo n.º 1
0
 /** {@inheritDoc} */
 @Override
 public void release(final CollectionAgent agent) {
   final WmiAgentState nodeState = m_scheduledNodes.get((Integer) agent.getNodeId());
   if (nodeState != null) {
     m_scheduledNodes.remove((Integer) agent.getNodeId());
   }
 }
Ejemplo n.º 2
0
  /**
   * Sets the up.
   *
   * @throws Exception the exception
   */
  @Before
  public void setUp() throws Exception {
    MockLogAppender.setupLogging();

    initializeRrdStrategy();
    initializeDocumentBuilder();

    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(getConfigFileName());
    m_xmlCollectionDao.setConfigResource(resource);
    m_xmlCollectionDao.afterPropertiesSet();

    EasyMock.replay(m_collectionAgent, m_eventProxy);
  }
Ejemplo n.º 3
0
  /** {@inheritDoc} */
  @Override
  public void initialize(final CollectionAgent agent, final Map<String, Object> parameters) {
    LOG.debug("initialize: Initializing WMI collection for agent: {}", agent);
    final Integer scheduledNodeKey = Integer.valueOf(agent.getNodeId());
    WmiAgentState nodeState = m_scheduledNodes.get(scheduledNodeKey);

    if (nodeState != null) {
      LOG.info(
          "initialize: Not scheduling interface for WMI collection: {}", nodeState.getAddress());
      final StringBuffer sb = new StringBuffer();
      sb.append("initialize service: ");
      sb.append(" for address: ");
      sb.append(nodeState.getAddress());
      sb.append(" already scheduled for collection on node: ");
      sb.append(agent);
      LOG.debug(sb.toString());
      throw new IllegalStateException(sb.toString());
    } else {
      nodeState = new WmiAgentState(agent.getAddress(), parameters);
      LOG.info("initialize: Scheduling interface for collection: {}", nodeState.getAddress());
      m_scheduledNodes.put(scheduledNodeKey, nodeState);
    }
  }
Ejemplo n.º 4
0
  /** {@inheritDoc} */
  @Override
  public CollectionSet collect(
      final CollectionAgent agent, final EventProxy eproxy, final Map<String, Object> parameters) {

    String collectionName =
        ParameterMap.getKeyedString(
            parameters,
            "collection",
            ParameterMap.getKeyedString(parameters, "wmi-collection", null));
    // Find attributes to collect - check groups in configuration. For each,
    // check scheduled nodes to see if that group should be collected
    final WmiCollection collection =
        WmiDataCollectionConfigFactory.getInstance().getWmiCollection(collectionName);
    final WmiAgentState agentState = m_scheduledNodes.get(agent.getNodeId());

    // Load the attribute group types.
    loadAttributeGroupList(collection);

    // Load the attribute types.
    loadAttributeTypeList(collection);

    // Create a new collection set.
    final WmiCollectionSet collectionSet = new WmiCollectionSet();
    collectionSet.setCollectionTimestamp(new Date());
    final WmiSingleInstanceCollectionResource nodeResource =
        new WmiSingleInstanceCollectionResource(agent);

    // Iterate through the WMI collection groups.
    for (final Wpm wpm : collection.getWpms().getWpm()) {
      // 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.
              WmiCollectionResource 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 =
                    new WmiMultiInstanceCollectionResource(agent, instance, wpm.getResourceType());
              } else {
                resource = nodeResource;
              }

              for (final Attrib attrib : wpm.getAttrib()) {
                final OnmsWbemProperty prop =
                    obj.getWmiProperties().getByName(attrib.getWmiObject());
                final WmiCollectionAttributeType attribType =
                    m_attribTypeList.get(attrib.getName());
                resource.setAttributeValue(attribType, prop.getWmiValue().toString());
              }
              collectionSet.getCollectionResources().add(resource);
            }
          }
        } 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);
            }
          }
        }
      }
    }
    collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
    return collectionSet;
  }
  /* (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();
    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());
        Request request = parseRequest(source.getRequest(), agent);
        URL url = UrlFactory.getUrl(urlStr, request);
        String lastFile = getLastFilename(resourceDir, url.getPath());
        Sftp3gppUrlConnection connection = (Sftp3gppUrlConnection) url.openConnection();
        if (lastFile == null) {
          lastFile = connection.get3gppFileName();
          LOG.debug(
              "collect(single): retrieving file from {}{}{} from {}",
              url.getPath(),
              File.separatorChar,
              lastFile,
              agent.getHostAddress());
          Document doc = getXmlDocument(urlStr, source.getRequest());
          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 {} from {}",
                  fileName,
                  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 {} on {}", lastFile, agent);
          }
          connection.disconnect();
        }
      }
      collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
      return collectionSet;
    } catch (Exception e) {
      collectionSet.setStatus(ServiceCollector.COLLECTION_FAILED);
      throw new CollectionException(e.getMessage(), e);
    }
  }