Example #1
0
  void wakeUpNodeCollection(int nodeid) {

    LinkableNode node = getNode(nodeid);

    if (node == null) {
      LogUtils.warnf(
          this, "wakeUpNodeCollection: node not found during scheduling with ID %d", nodeid);
      scheduleNodeCollection(nodeid);
    } else {
      // get collections
      // get readyRunnuble
      // wakeup RR
      Collection<SnmpCollection> collections =
          getSnmpCollections(nodeid, node.getSnmpPrimaryIpAddr(), node.getSysoid());
      LogUtils.debugf(
          this,
          "wakeUpNodeCollection: fetched SnmpCollections from scratch, iterating over %d objects to wake them up",
          collections.size());
      for (SnmpCollection collection : collections) {
        ReadyRunnable rr = getReadyRunnable(collection);
        if (rr == null) {
          LogUtils.warnf(this, "wakeUpNodeCollection: found null ReadyRunnable");
          return;
        } else {
          rr.wakeUp();
        }
      }
    }
  }
 /**
  * Parse the {@link DataAccessException} to see if special problems were encountered while
  * performing the query. See issue NMS-5029 for examples of stack traces that can be thrown from
  * these calls. {@see http://issues.opennms.org/browse/NMS-5029}
  */
 private void logExtraSaveOrUpdateExceptionInformation(
     final T entity, final DataAccessException e) {
   Throwable cause = e;
   while (cause.getCause() != null) {
     // if (cause.getCause().getClass().getName().equals(PSQLException.class.getName())) {
     if (cause.getMessage().contains("duplicate key value violates unique constraint")) {
       final ClassMetadata meta = getSessionFactory().getClassMetadata(m_entityClass);
       LogUtils.warnf(
           this,
           "Duplicate key constraint violation, class: %s, key value: %s",
           m_entityClass.getName(),
           meta.getPropertyValue(entity, meta.getIdentifierPropertyName(), EntityMode.POJO));
       break;
     } else if (cause.getMessage().contains("given object has a null identifier")) {
       LogUtils.warnf(
           this,
           "Null identifier on object, class: %s: %s",
           m_entityClass.getName(),
           entity.toString());
       break;
     }
     // }
     cause = cause.getCause();
   }
 }
  /**
   * getRanges
   *
   * @return a {@link java.util.List} object.
   */
  public List<IPPollRange> getRanges() {
    final List<IPPollRange> includes = new LinkedList<IPPollRange>();

    getReadLock().lock();

    try {
      Long defaultTimeout = null;
      Integer defaultRetries = null;
      if (getConfiguration().hasTimeout()) defaultTimeout = getConfiguration().getTimeout();
      if (getConfiguration().hasRetries()) defaultRetries = getConfiguration().getRetries();

      for (final IncludeRange ir : getConfiguration().getIncludeRangeCollection()) {

        // Validate IP range; if invalid, then log and discard this range
        try {
          InetAddressUtils.toIpAddrBytes(ir.getBegin());
        } catch (Throwable e) {
          LogUtils.warnf(
              this, "Begin address of discovery range is invalid, discarding: %s", ir.getBegin());
          continue;
        }

        try {
          InetAddressUtils.toIpAddrBytes(ir.getEnd());
        } catch (Throwable e) {
          LogUtils.warnf(
              this, "End address of discovery range is invalid, discarding: %s", ir.getEnd());
          continue;
        }

        long timeout = 800L;
        if (ir.hasTimeout()) {
          timeout = ir.getTimeout();
        } else if (defaultTimeout != null) {
          timeout = defaultTimeout;
        }

        int retries = 3;
        if (ir.hasRetries()) {
          retries = ir.getRetries();
        } else if (defaultRetries != null) {
          retries = defaultRetries;
        }

        try {
          includes.add(new IPPollRange(ir.getBegin(), ir.getEnd(), timeout, retries));
        } catch (final UnknownHostException uhE) {
          LogUtils.warnf(
              this, uhE, "Failed to convert address range (%s, %s)", ir.getBegin(), ir.getEnd());
        }
      }

      return includes;
    } finally {
      getReadLock().unlock();
    }
  }
Example #4
0
 private void addTrap(TrapNotification o) {
   try {
     m_backlogQ.add(o);
   } catch (InterruptedException e) {
     LogUtils.warnf(this, e, "addTrap: Error adding trap to queue, it was interrupted");
     Thread.currentThread().interrupt();
   } catch (final FifoQueueException e) {
     LogUtils.warnf(this, e, "addTrap: Error adding trap to queue");
   }
 }
  /**
   * Check whether the data sources in opennms-datasources.xml are valid.
   *
   * @throws MissingDataSourceException A required data source was not found in
   *     opennms-datasources.xml.
   * @throws InvalidDataSourceException A required data source could not be connected to.
   */
  public void check() throws MissingDataSourceException, InvalidDataSourceException {

    // First, check to make sure the required datasources are there.
    boolean dataSourcesFound = true;
    for (final String dataSource : m_required) {
      if (!m_dataSources.containsKey(dataSource)) {
        LogUtils.errorf(
            this, "Required data source '%s' is missing from opennms-datasources.xml", dataSource);
        dataSourcesFound = false;
      }
    }
    if (!dataSourcesFound) {
      throw new MissingDataSourceException(
          "OpenNMS is missing one or more data sources required for startup.");
    }

    // Then, check for the optional ones so we can warn about them going missing.
    for (final String dataSource : m_optional) {
      if (!m_dataSources.containsKey(dataSource)) {
        LogUtils.infof(
            this, "Data source '%s' is missing from opennms-datasources.xml", dataSource);
      }
    }

    // Finally, try connecting to all data sources, and warn or error as appropriate.
    for (final JdbcDataSource dataSource : m_dataSources.values()) {
      final String name = dataSource.getName();
      if (!m_required.contains(name) && !m_optional.contains(name)) {
        LogUtils.warnf(this, "Unknown datasource '%s' was found.", name);
      }
      try {
        Class.forName(dataSource.getClassName());
        final Connection connection =
            DriverManager.getConnection(
                dataSource.getUrl(), dataSource.getUserName(), dataSource.getPassword());
        connection.close();
      } catch (final Throwable t) {
        final String errorMessage =
            "Unable to connect to data source '%s' at URL '%s' with username '%s', check opennms-datasources.xml and your database permissions.";
        if (m_required.contains(name)) {
          LogUtils.errorf(this, errorMessage, name, dataSource.getUrl(), dataSource.getUserName());
          throw new InvalidDataSourceException("Data source '" + name + "' failed.", t);
        } else {
          LogUtils.warnf(this, errorMessage, name, dataSource.getUrl(), dataSource.getUserName());
        }
      }
    }
  }
 /**
  *
  *
  * <pre>
  * The file URL is read and a 'specific IP' is added for each entry
  *  in this file. Each line in the URL file can be one of -
  *  &lt;IP&gt;&lt;space&gt;#&lt;comments&gt;
  *  or
  *  &lt;IP&gt;
  *  or
  *  #&lt;comments&gt;
  *
  *  Lines starting with a '#' are ignored and so are characters after
  *  a '&lt;space&gt;#' in a line.
  * </pre>
  *
  * @param specifics the list to add to
  * @param url the URL file
  * @param timeout the timeout for all entries in this URL
  * @param retries the retries for all entries in this URL
  * @return a boolean.
  */
 public static boolean addToSpecificsFromURL(
     final List<IPPollAddress> specifics,
     final String url,
     final long timeout,
     final int retries) {
   // open the file indicated by the URL
   InputStream is = null;
   try {
     final URL fileURL = new URL(url);
     is = fileURL.openStream();
     // check to see if the file exists
     if (is == null) {
       // log something
       LogUtils.warnf(DiscoveryConfigFactory.class, "URL does not exist: %s", url);
       return true;
     } else {
       return addToSpecificsFromURL(specifics, fileURL.openStream(), timeout, retries);
     }
   } catch (final IOException e) {
     LogUtils.errorf(DiscoveryConfigFactory.class, "Error reading URL: %s", url);
     return false;
   } finally {
     IOUtils.closeQuietly(is);
   }
 }
  private NodeScanSchedule createScheduleForNode(final OnmsNode node, final boolean force) {
    Assert.notNull(node, "Node may not be null");
    final String actualForeignSource = node.getForeignSource();
    if (actualForeignSource == null && !isDiscoveryEnabled()) {
      infof(
          this,
          "Not scheduling node %s to be scanned since it has a null foreignSource and handling of discovered nodes is disabled in provisiond",
          node);
      return null;
    }

    final String effectiveForeignSource =
        actualForeignSource == null ? "default" : actualForeignSource;
    try {
      final ForeignSource fs = m_foreignSourceRepository.getForeignSource(effectiveForeignSource);

      final Duration scanInterval = fs.getScanInterval();
      Duration initialDelay = Duration.ZERO;
      if (node.getLastCapsdPoll() != null && !force) {
        final DateTime nextPoll =
            new DateTime(node.getLastCapsdPoll().getTime()).plus(scanInterval);
        final DateTime now = new DateTime();
        if (nextPoll.isAfter(now)) {
          initialDelay = new Duration(now, nextPoll);
        }
      }

      return new NodeScanSchedule(
          node.getId(), actualForeignSource, node.getForeignId(), initialDelay, scanInterval);
    } catch (final ForeignSourceRepositoryException e) {
      warnf(this, e, "unable to get foreign source '%s' from repository", effectiveForeignSource);
      return null;
    }
  }
 /**
  * Adds the dependency to the queue.
  *
  * @param queue the queue
  * @param mibDirectoryFiles
  * @return true, if successful
  */
 private boolean addDependencyToQueue(
     final List<URL> queue, final Map<String, File> mibDirectoryFiles) {
   final List<String> dependencies = new ArrayList<String>(missingDependencies);
   boolean ok = true;
   for (String dependency : dependencies) {
     boolean found = false;
     for (String suffix : MIB_SUFFIXES) {
       final String fileName = (dependency + suffix).toLowerCase();
       if (mibDirectoryFiles.containsKey(fileName)) {
         File f = mibDirectoryFiles.get(fileName);
         LogUtils.debugf(this, "Checking dependency file %s", f.getAbsolutePath());
         if (f.exists()) {
           LogUtils.infof(this, "Adding dependency file %s", f.getAbsolutePath());
           addFileToQueue(queue, f);
           missingDependencies.remove(dependency);
           found = true;
           break;
         }
       }
       LogUtils.debugf(this, "Dependency file %s doesn't exist", fileName);
     }
     if (!found) {
       LogUtils.warnf(this, "Couldn't find dependency %s on %s", dependency, mibDirectory);
       ok = false;
     }
   }
   return ok;
 }
Example #9
0
  public boolean scheduleNodeCollection(int nodeid) {

    LinkableNode node = null;
    // database changed need reload packageiplist
    m_linkdConfig.updatePackageIpListMap();

    // First of all get Linkable Node
    LogUtils.debugf(this, "scheduleNodeCollection: Loading node %d from database", nodeid);
    try {
      node = m_queryMgr.getSnmpNode(nodeid);
      if (node == null) {
        LogUtils.warnf(
            this,
            "scheduleNodeCollection: Failed to get linkable node from database with ID %d. Exiting",
            nodeid);
        return false;
      }
    } catch (final SQLException sqlE) {
      LogUtils.errorf(
          this,
          sqlE,
          "scheduleNodeCollection: SQL Exception while syncing node object with ID %d with database information.",
          nodeid);
      return false;
    }
    synchronized (m_nodes) {
      LogUtils.debugf(this, "adding node %s to the collection", node);
      m_nodes.add(node);
    }

    scheduleCollectionForNode(node);
    return true;
  }
Example #10
0
  /** {@inheritDoc} */
  public GWTLatLng geocode(final String geolocation) throws GeocoderException {
    final HttpUriRequest method = new HttpGet(getUrl(geolocation));
    method.addHeader("User-Agent", "OpenNMS-MapquestGeocoder/1.0");
    if (m_referer != null) {
      method.addHeader("Referer", m_referer);
    }

    try {
      InputStream responseStream = m_httpClient.execute(method).getEntity().getContent();
      final ElementTree tree = ElementTree.fromStream(responseStream);
      if (tree == null) {
        throw new GeocoderException(
            "an error occurred connecting to the Nominatim geocoding service (no XML tree was found)");
      }

      final List<ElementTree> places = tree.findAll("//place");
      if (places.size() > 1) {
        LogUtils.warnf(this, "more than one location returned for query: %s", geolocation);
      } else if (places.size() == 0) {
        throw new GeocoderException("Nominatim returned an OK status code, but no places");
      }
      final ElementTree place = places.get(0);

      Double latitude = Double.valueOf(place.getAttribute("lat"));
      Double longitude = Double.valueOf(place.getAttribute("lon"));
      return new GWTLatLng(latitude, longitude);
    } catch (GeocoderException e) {
      throw e;
    } catch (Throwable e) {
      throw new GeocoderException("unable to get lat/lng from Nominatim", e);
    }
  }
  @Override
  @Transactional
  public void deleteComponent(
      final String type,
      final String foreignSource,
      final String foreignId,
      final boolean deleteOrphans) {
    LogUtils.debugf(
        this,
        "deleteSubcomponent(%s, %s, %s, %s)",
        type,
        foreignSource,
        foreignId,
        Boolean.valueOf(deleteOrphans));

    final NCSComponent component = getComponent(type, foreignSource, foreignId);
    final ComponentIdentifier id = getIdentifier(component);
    final ComponentEventQueue ceq = new ComponentEventQueue();
    deleteComponent(id, ceq, deleteOrphans);
    try {
      ceq.sendAll(m_eventProxy);
    } catch (final EventProxyException e) {
      LogUtils.warnf(
          this,
          e,
          "Component %s deleted, but an error occured while sending delete/update events.",
          id);
    }
  }
Example #12
0
  /**
   * Stops the currently running service. If the service is not running then the command is silently
   * discarded.
   */
  public synchronized void onStop() {
    m_status = STOP_PENDING;

    // shutdown and wait on the background processing thread to exit.
    LogUtils.debugf(this, "stop: closing communication paths.");

    try {
      if (m_registeredForTraps) {
        LogUtils.debugf(this, "stop: Closing SNMP trap session.");
        SnmpUtils.unregisterForTraps(this, getInetAddress(), getSnmpTrapPort());
        LogUtils.debugf(this, "stop: SNMP trap session closed.");
      } else {
        LogUtils.debugf(
            this, "stop: not attemping to closing SNMP trap session--it was never opened");
      }

    } catch (final IOException e) {
      LogUtils.warnf(this, e, "stop: exception occurred closing session");
    } catch (final IllegalStateException e) {
      LogUtils.debugf(this, e, "stop: The SNMP session was already closed");
    }

    LogUtils.debugf(this, "stop: Stopping queue processor.");

    m_processor.stop();
    m_eventReader.close();

    m_status = STOPPED;

    LogUtils.debugf(this, "stop: Trapd stopped");
  }
 public void log(String level, String format, Object... args) {
   if ("TRACE".equals(level)) LogUtils.tracef(this, format, args);
   if ("DEBUG".equals(level)) LogUtils.debugf(this, format, args);
   if ("INFO".equals(level)) LogUtils.infof(this, format, args);
   if ("WARN".equals(level)) LogUtils.warnf(this, format, args);
   if ("ERROR".equals(level)) LogUtils.errorf(this, format, args);
   if ("FATAL".equals(level)) LogUtils.errorf(this, format, args);
 }
 /**
  * Adds a file to the queue.
  *
  * @param queue the queue
  * @param mibFile the MIB file
  */
 private void addFileToQueue(List<URL> queue, File mibFile) {
   try {
     URL url = mibFile.toURI().toURL();
     if (!queue.contains(url)) {
       LogUtils.debugf(this, "Adding %s to queue ", url);
       queue.add(url);
     }
   } catch (Exception e) {
     LogUtils.warnf(this, "Can't generate URL from %s", mibFile.getAbsolutePath());
   }
 }
  @Override
  @Transactional
  public NCSComponent addSubcomponent(
      final String type,
      final String foreignSource,
      final String foreignId,
      final NCSComponent subComponent,
      final boolean deleteOrphans) {
    final ComponentIdentifier subComponentId = getIdentifier(subComponent);

    LogUtils.debugf(
        this,
        "addSubcomponent(%s, %s, %s, %s, %s)",
        type,
        foreignSource,
        foreignId,
        subComponentId,
        Boolean.valueOf(deleteOrphans));

    final NCSComponent component = getComponent(type, foreignSource, foreignId);
    final ComponentIdentifier id = getIdentifier(component);
    final ComponentEventQueue ceq = new ComponentEventQueue();

    if (component == null) {
      throw new ObjectRetrievalFailureException(
          NCSComponent.class,
          "Unable to locate component with type="
              + type
              + ", foreignSource="
              + foreignSource
              + ", foreignId="
              + foreignId);
    }

    final NCSComponent updatedSubComponent =
        addOrUpdateComponents(subComponentId, subComponent, ceq, deleteOrphans);
    component.addSubcomponent(updatedSubComponent);

    m_componentDao.update(component);
    ceq.componentUpdated(id);

    try {
      ceq.sendAll(m_eventProxy);
    } catch (final EventProxyException e) {
      LogUtils.warnf(
          this,
          e,
          "Component %s added to %s, but an error occured while sending add/delete/update events.",
          subComponentId,
          id);
    }

    return getComponent(id);
  }
 /** {@inheritDoc} */
 @Override
 public void run() {
   try {
     final Date endDate = new Date();
     m_locationDataManager.doUpdate(m_lastUpdated, endDate, m_service);
     m_lastUpdated = endDate;
   } catch (final Exception e) {
     LogUtils.warnf(
         m_locationDataManager,
         e,
         "An error occurred while pushing monitor and application status updates.");
   }
 }
Example #17
0
  void suspendNodeCollection(int nodeid) {
    LogUtils.debugf(
        this, "suspendNodeCollection: suspend collection LinkableNode for node %d", nodeid);

    try {
      m_queryMgr.update(nodeid, QueryManager.ACTION_UPTODATE);
    } catch (SQLException sqlE) {
      LogUtils.errorf(
          this,
          sqlE,
          "suspendNodeCollection: SQL Exception while syncing node object with database information.");
    }

    LinkableNode node = getNode(nodeid);

    if (node == null) {
      LogUtils.warnf(this, "suspendNodeCollection: found null ReadyRunnable");
    } else {
      // get collections
      // get readyRunnuble
      // suspend RR
      Collection<SnmpCollection> collections =
          getSnmpCollections(nodeid, node.getSnmpPrimaryIpAddr(), node.getSysoid());
      LogUtils.debugf(
          this,
          "suspendNodeCollection: fetched SnmpCollections from scratch, iterating over %d objects to wake them up",
          collections.size());
      for (SnmpCollection collection : collections) {
        ReadyRunnable rr = getReadyRunnable(collection);
        if (rr == null) {
          LogUtils.warnf(this, "suspendNodeCollection: suspend: node not found: %d", nodeid);
          return;
        } else {
          rr.suspend();
        }
      }
    }
  }
Example #18
0
  void deleteNode(int nodeid) {
    LogUtils.debugf(this, "deleteNode: deleting LinkableNode for node %s", nodeid);

    try {
      m_queryMgr.update(nodeid, QueryManager.ACTION_DELETE);
    } catch (SQLException sqlE) {
      LogUtils.errorf(
          this,
          sqlE,
          "deleteNode: SQL Exception while syncing node object with database information.");
    }

    LinkableNode node = removeNode(nodeid);

    if (node == null) {
      LogUtils.warnf(this, "deleteNode: node not found: %d", nodeid);
    } else {
      Collection<SnmpCollection> collections =
          getSnmpCollections(nodeid, node.getSnmpPrimaryIpAddr(), node.getSysoid());
      LogUtils.debugf(
          this,
          "deleteNode: fetched SnmpCollections from scratch, iterating over %d objects to wake them up",
          collections.size());
      for (SnmpCollection collection : collections) {
        ReadyRunnable rr = getReadyRunnable(collection);

        if (rr == null) {
          LogUtils.warnf(this, "deleteNode: found null ReadyRunnable");
          return;
        } else {
          rr.unschedule();
        }
      }
    }

    // database changed need reload packageiplist
    m_linkdConfig.updatePackageIpListMap();
  }
  /**
   * addToSpecificsFromURL
   *
   * @param specifics a {@link java.util.List} object.
   * @param is a {@link java.io.InputStream} object.
   * @param timeout a long.
   * @param retries a int.
   * @return a boolean.
   * @throws java.io.IOException if any.
   */
  public static boolean addToSpecificsFromURL(
      final List<IPPollAddress> specifics,
      final InputStream is,
      final long timeout,
      final int retries)
      throws IOException {
    boolean bRet = true;

    try {
      final BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "UTF-8"));

      String ipLine = null;
      String specIP = null;

      // get each line of the file and turn it into a specific range
      while ((ipLine = buffer.readLine()) != null) {
        ipLine = ipLine.trim();
        if (ipLine.length() == 0 || ipLine.charAt(0) == DiscoveryConfigFactory.COMMENT_CHAR) {
          // blank line or skip comment
          continue;
        }

        // check for comments after IP
        final int comIndex = ipLine.indexOf(DiscoveryConfigFactory.COMMENT_STR);
        if (comIndex == -1) {
          specIP = ipLine;
        } else {
          specIP = ipLine.substring(0, comIndex);
          specIP = specIP.trim();
        }

        try {
          specifics.add(new IPPollAddress(specIP, timeout, retries));
        } catch (final UnknownHostException e) {
          LogUtils.warnf(
              DiscoveryConfigFactory.class,
              "Unknown host \'%s\' inside discovery include file: address ignored",
              specIP);
        }

        specIP = null;
      }
    } catch (final UnsupportedEncodingException e) {
      LogUtils.errorf(DiscoveryConfigFactory.class, "Your JVM doesn't support UTF-8");
      return false;
    }
    return bRet;
  }
 /**
  * setField
  *
  * @param name a {@link java.lang.String} object.
  * @param val a {@link java.lang.String} object.
  */
 public void setField(final String name, final String val) {
   if (name.equals("eventparms")) {
     String[] parts = val.split(";");
     for (String part : parts) {
       String[] pair = part.split("=");
       addParam(pair[0], pair[1].replaceFirst("[(]\\w+,\\w+[)]", ""));
     }
   } else {
     final BeanWrapper w = PropertyAccessorFactory.forBeanPropertyAccess(m_event);
     try {
       w.setPropertyValue(name, val);
     } catch (final BeansException e) {
       LogUtils.warnf(this, e, "Could not set field on event: %s", name);
     }
   }
 }
 /**
  * getPhysAddr
  *
  * @return a {@link java.lang.String} object.
  * @see {@link org.opennms.netmgt.linkd.snmp.IpNetToMediaTableEntry#getIpNetToMediaPhysAddress()}
  */
 public String getPhysAddr() {
   try {
     // Try to fetch the physical address value as a hex string.
     String hexString = getHexString(IfTableEntry.IF_PHYS_ADDR);
     if (hexString != null && hexString.length() == 12) {
       // If the hex string is 12 characters long, than the agent is kinda weird and
       // is returning the value as a raw binary value that is 6 bytes in length.
       // But that's OK, as long as we can convert it into a string, that's fine.
       return hexString;
     } else {
       // This is the normal case that most agents conform to: the value is an ASCII
       // string representing the colon-separated MAC address. We just need to reformat
       // it to remove the colons and convert it into a 12-character string.
       return normalizeMacAddress(getDisplayString(IfTableEntry.IF_PHYS_ADDR));
     }
   } catch (IllegalArgumentException e) {
     LogUtils.warnf(this, e, e.getMessage());
     return getDisplayString(IfTableEntry.IF_PHYS_ADDR);
   }
 }
 @Override
 @Transactional
 public NCSComponent addOrUpdateComponents(
     final NCSComponent component, final boolean deleteOrphans) {
   final ComponentIdentifier componentId = getIdentifier(component);
   LogUtils.debugf(
       this, "addOrUpdateComponents(%s, %s)", componentId, Boolean.valueOf(deleteOrphans));
   final ComponentEventQueue ceq = new ComponentEventQueue();
   final NCSComponent updatedComponent =
       addOrUpdateComponents(componentId, component, ceq, deleteOrphans);
   try {
     ceq.sendAll(m_eventProxy);
   } catch (final EventProxyException e) {
     LogUtils.warnf(
         this,
         e,
         "Component %s added, but an error occured while sending add/delete/update events.",
         componentId);
   }
   return updatedComponent;
 }
  /**
   * getSpecifics
   *
   * @return a {@link java.util.List} object.
   */
  public List<IPPollAddress> getSpecifics() {
    final List<IPPollAddress> specifics = new LinkedList<IPPollAddress>();

    getReadLock().lock();

    try {
      Long defaultTimeout = null;
      Integer defaultRetries = null;
      if (getConfiguration().hasTimeout()) defaultTimeout = getConfiguration().getTimeout();
      if (getConfiguration().hasRetries()) defaultRetries = getConfiguration().getRetries();

      for (final Specific s : getConfiguration().getSpecificCollection()) {

        long timeout = 800L;
        if (s.hasTimeout()) {
          timeout = s.getTimeout();
        } else if (defaultTimeout != null) {
          timeout = defaultTimeout;
        }

        int retries = 3;
        if (s.hasRetries()) {
          retries = s.getRetries();
        } else if (defaultRetries != null) {
          retries = defaultRetries;
        }

        try {
          specifics.add(new IPPollAddress(s.getContent(), timeout, retries));
        } catch (final UnknownHostException uhE) {
          LogUtils.warnf(this, uhE, "Failed to convert address %s", s.getContent());
        }
      }
      return specifics;
    } finally {
      getReadLock().unlock();
    }
  }
  private String createZip(String baseFileName) {
    File reportResourceDirectory = new File(baseFileName + "_files");
    String zipFile = baseFileName + ".zip";

    if (reportResourceDirectory.exists() && reportResourceDirectory.isDirectory()) {
      ZipOutputStream reportArchive;

      try {
        reportArchive = new ZipOutputStream(new FileOutputStream(zipFile));
        addFileToArchive(reportArchive, baseFileName);

        reportArchive.putNextEntry(new ZipEntry(baseFileName));
        for (String file : Arrays.asList(reportResourceDirectory.list())) {
          addFileToArchive(reportArchive, file);
        }
        reportArchive.close();
      } catch (final Exception e) {
        LogUtils.warnf(this, e, "unable to create %s", zipFile);
      }
    }

    return zipFile;
  }
  /** {@inheritDoc} */
  @Transactional
  public OnmsNode getRequisitionedNode(final String foreignSource, final String foreignId)
      throws ForeignSourceRepositoryException {
    final OnmsNodeRequisition nodeReq =
        m_foreignSourceRepository.getNodeRequisition(foreignSource, foreignId);
    if (nodeReq == null) {
      warnf(this, "nodeReq for node %s:%s cannot be null!", foreignSource, foreignId);
      return null;
    }
    final OnmsNode node = nodeReq.constructOnmsNodeFromRequisition();

    // fill in real database categories
    final HashSet<OnmsCategory> dbCategories = new HashSet<OnmsCategory>();
    for (final OnmsCategory category : node.getCategories()) {
      dbCategories.add(createCategoryIfNecessary(category.getName()));
    }

    node.setCategories(dbCategories);

    // fill in real service types
    node.visit(new ServiceTypeFulfiller());

    return node;
  }
Example #26
0
  /**
   * This method is responsible for building a list of RRDDataSource objects from the provided list
   * of MBeanObject objects.
   *
   * @param collectionName Collection name
   * @param oidList List of MBeanObject objects defining the oid's to be collected via JMX.
   * @return list of RRDDataSource objects
   */
  protected Map<String, JMXDataSource> buildDataSourceList(
      String collectionName, Map<String, List<Attrib>> attributeMap) {
    LogUtils.debugf(this, "buildDataSourceList - ***");

    /*
     * Retrieve the RRD expansion data source list which contains all
     * the expansion data source's. Use this list as a basis
     * for building a data source list for the current interface.
     */
    HashMap<String, JMXDataSource> dsList = new HashMap<String, JMXDataSource>();

    /*
     * Loop through the MBean object list to be collected for this
     * interface and add a corresponding RRD data source object. In this
     * manner each interface will have RRD files create which reflect only
     * the data sources pertinent to it.
     */

    LogUtils.debugf(this, "attributeMap size: %d", attributeMap.size());
    Iterator<String> objNameIter = attributeMap.keySet().iterator();
    while (objNameIter.hasNext()) {
      String objectName = objNameIter.next().toString();
      List<Attrib> list = attributeMap.get(objectName);

      LogUtils.debugf(this, "ObjectName: %s, Attributes: %d", objectName, list.size());

      Iterator<Attrib> iter = list.iterator();
      while (iter.hasNext()) {
        Attrib attr = iter.next();
        JMXDataSource ds = null;

        /*
         * Verify that this object has an appropriate "integer" data
         * type which can be stored in an RRD database file (must map to
         * one of the supported RRD data source types: COUNTER or GAUGE).
         * */
        String ds_type = JMXDataSource.mapType(attr.getType());
        if (ds_type != null) {
          /*
           * Passed!! Create new data source instance for this MBean
           * object.
           * Assign heartbeat using formula (2 * step) and hard code
           * min & max values to "U" ("unknown").
           */
          ds = new JMXDataSource();
          ds.setHeartbeat(2 * JMXDataCollectionConfigFactory.getInstance().getStep(collectionName));
          // For completeness, adding a minval option to the variable.
          String ds_minval = attr.getMinval();
          if (ds_minval == null) {
            ds_minval = "U";
          }
          ds.setMax(ds_minval);

          /*
           * In order to handle counter wraps, we need to set a max
           * value for the variable.
           */
          String ds_maxval = attr.getMaxval();
          if (ds_maxval == null) {
            ds_maxval = "U";
          }

          ds.setMax(ds_maxval);
          ds.setInstance(collectionName);

          /*
           * Truncate MBean object name/alias if it exceeds 19 char
           * max for RRD data source names.
           */
          String ds_name = attr.getAlias();
          if (ds_name.length() > MAX_DS_NAME_LENGTH) {
            LogUtils.warnf(
                this,
                "buildDataSourceList: alias '%s' exceeds 19 char maximum for RRD data source names, truncating.",
                attr.getAlias());
            char[] temp = ds_name.toCharArray();
            ds_name = String.copyValueOf(temp, 0, MAX_DS_NAME_LENGTH);
          }
          ds.setName(ds_name);

          // Map MBean object data type to RRD data type
          ds.setType(ds_type);

          /*
           * Assign the data source object identifier and instance
           * ds.setName(attr.getName());
           */
          ds.setOid(attr.getName());

          LogUtils.debugf(
              this,
              "buildDataSourceList: ds_name: %s ds_oid: %s.%s ds_max: %s ds_min: %s",
              ds.getName(),
              ds.getOid(),
              ds.getInstance(),
              ds.getMax(),
              ds.getMin());

          // Add the new data source to the list
          dsList.put(objectName + "|" + attr.getName(), ds);
        } else {
          LogUtils.warnf(
              this,
              "buildDataSourceList: Data type '%s' not supported.  Only integer-type data may be stored in RRD.  MBean object '%s' will not be mapped to RRD data source.",
              attr.getType(),
              attr.getAlias());
        }
      }
    }

    return dsList;
  }
  /** {@inheritDoc} */
  @Override
  protected ModelAndView handleRequestInternal(
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String fileName = request.getParameter("fileName");

    m_reportdConfigurationDao =
        BeanUtils.getBean("reportdContext", "reportdConfigDao", ReportdConfigurationDao.class);
    final File storageDirectory = new File(m_reportdConfigurationDao.getStorageDirectory());

    if (fileName != null) {
      final File requestedFile = new File(fileName);
      if (!requestedFile
          .getParentFile()
          .getCanonicalFile()
          .equals(storageDirectory.getCanonicalFile())) {
        LogUtils.warnf(
            this,
            "User attempted to retrieve file %s but was restricted to %s",
            requestedFile,
            storageDirectory);
        throw new IllegalArgumentException(
            "Cannot retrieve reports from outside Reportd storage directory");
      }

      if (fileName.toLowerCase().endsWith(".pdf")) {
        response.setContentType("application/pdf;charset=UTF-8");
      }
      if (fileName.toLowerCase().endsWith(".csv")) {
        response.setContentType("text/csv;charset=UTF-8");
      }
      response.setHeader("Content-disposition", "inline; filename=" + fileName);
      response.setHeader("Pragma", "public");
      response.setHeader("Cache-Control", "cache");
      response.setHeader("Cache-Control", "must-revalidate");
      StreamUtils.copy(new FileInputStream(new File(fileName)), response.getOutputStream());
      return null;
    }

    String[] requiredParameters = new String[] {"locatorId", "format"};

    for (String requiredParameter : requiredParameters) {
      if (request.getParameter(requiredParameter) == null) {
        throw new MissingParameterException(requiredParameter, requiredParameters);
      }
    }

    try {
      Integer reportCatalogEntryId =
          Integer.valueOf(WebSecurityUtils.safeParseInt(request.getParameter("locatorId")));

      String requestFormat = new String(request.getParameter("format"));

      if ((ReportFormat.PDF == ReportFormat.valueOf(requestFormat))
          || (ReportFormat.SVG == ReportFormat.valueOf(requestFormat))) {
        response.setContentType("application/pdf;charset=UTF-8");
        response.setHeader(
            "Content-disposition", "inline; filename=" + reportCatalogEntryId.toString() + ".pdf");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "cache");
        response.setHeader("Cache-Control", "must-revalidate");
      }
      if (ReportFormat.CSV == ReportFormat.valueOf(requestFormat)) {
        response.setContentType("text/csv;charset=UTF-8");
        response.setHeader(
            "Content-disposition", "inline; filename=" + reportCatalogEntryId.toString() + ".csv");
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "cache");
        response.setHeader("Cache-Control", "must-revalidate");
      }
      m_reportStoreService.render(
          reportCatalogEntryId,
          ReportFormat.valueOf(requestFormat),
          (OutputStream) response.getOutputStream());
    } catch (NumberFormatException e) {
      // TODO something useful here.
    }

    return null;
  }
  /* (non-Javadoc)
   * @see org.opennms.netmgt.notifd.NotificationStrategy#send(java.util.List)
   */
  @Override
  public int send(List<Argument> arguments) {
    m_arguments = arguments;
    String fileName = getFileName();
    String lang = getLangClass();
    String engine = getBsfEngine();
    String extensions[] = getFileExtensions();

    LogUtils.infof(this, "Loading notification script from file '%s'", fileName);
    File scriptFile = new File(fileName);
    BSFManager bsfManager = new BSFManager();
    int returnCode = -1;

    try {

      if (lang == null) lang = BSFManager.getLangFromFilename(fileName);

      // Declare some beans that can be used inside the script
      HashMap<String, String> results = new HashMap<String, String>();
      bsfManager.declareBean("results", results, HashMap.class);
      declareBeans(bsfManager);

      if (engine != null && lang != null && extensions != null && extensions.length > 0) {
        BSFManager.registerScriptingEngine(lang, engine, extensions);
      }

      if (scriptFile.exists() && scriptFile.canRead()) {
        String code =
            IOUtils.getStringFromReader(
                new InputStreamReader(new FileInputStream(scriptFile), "UTF-8"));

        // Check foot before firing
        checkAberrantScriptBehaviors(code);

        // Execute the script
        bsfManager.exec(lang, "BSFNotificationStrategy", 0, 0, code);

        // Check whether the script finished successfully
        if ("OK".equals(results.get("status"))) {
          LogUtils.infof(
              this,
              "Execution succeeded and successful status passed back for script '%s'",
              scriptFile);
          returnCode = 0;
        } else {
          LogUtils.warnf(
              this,
              "Execution succeeded for script '%s', but script did not indicate successful notification by putting an entry into the 'results' bean with key 'status' and value 'OK'",
              scriptFile);
          returnCode = -1;
        }
      } else {
        LogUtils.warnf(
            this,
            "Cannot locate or read BSF script file '%s'. Returning failure indication.",
            fileName);
        returnCode = -1;
      }
    } catch (BSFException e) {
      LogUtils.warnf(
          this,
          e,
          "Execution of script '%s' failed with BSFException: %s",
          scriptFile,
          e.getMessage());
      returnCode = -1;
    } catch (FileNotFoundException e) {
      LogUtils.warnf(this, "Could not find BSF script file '%s'.", fileName);
      returnCode = -1;
    } catch (IOException e) {
      LogUtils.warnf(
          this,
          e,
          "Execution of script '%s' failed with IOException: %s",
          scriptFile,
          e.getMessage());
      returnCode = -1;
    } catch (Throwable e) {
      // Catch any RuntimeException throws
      LogUtils.warnf(
          this,
          e,
          "Execution of script '%s' failed with unexpected throwable: %s",
          scriptFile,
          e.getMessage());
      returnCode = -1;
    } finally {
      bsfManager.terminate();
    }

    return returnCode;
  }
Example #29
0
 /** {@inheritDoc} */
 public void trapError(final int error, final String msg) {
   LogUtils.warnf(
       this,
       "Error Processing Received Trap: error = " + error + (msg != null ? ", ref = " + msg : ""));
 }
  public SyslogMessage parse() throws SyslogParserException {
    final SyslogMessage syslogMessage = new SyslogMessage();

    String message = getText();

    int lbIdx = message.indexOf('<');
    int rbIdx = message.indexOf('>');

    if (lbIdx < 0 || rbIdx < 0 || lbIdx >= (rbIdx - 1)) {
      LogUtils.warnf(this, "Syslogd received an unparsable message!");
    }

    int priCode = 0;
    String priStr = message.substring(lbIdx + 1, rbIdx);

    try {
      priCode = Integer.parseInt(priStr);
    } catch (final NumberFormatException ex) {
      LogUtils.debugf(this, "ERROR Bad priority code '%s'", priStr);
    }

    LogUtils.tracef(this, "priority code = %d", priCode);

    syslogMessage.setFacility(SyslogFacility.getFacilityForCode(priCode));
    syslogMessage.setSeverity(SyslogSeverity.getSeverityForCode(priCode));

    message = message.substring(rbIdx + 1, message.length());

    final Matcher idMatcher = m_messageIdPattern.matcher(message);
    if (idMatcher.find()) {
      final String messageId = idMatcher.group(2);
      LogUtils.tracef(this, "found message ID '%s'", messageId);
      syslogMessage.setMessageID(messageId);
      message = message.substring(idMatcher.group(1).length() - 1);
    }

    LogUtils.tracef(this, "message = %s", message);

    Matcher oldDateMatcher = m_oldDatePattern.matcher(message);
    if (!oldDateMatcher.find()) {
      oldDateMatcher = null;
    }
    LogUtils.tracef(this, "stdMsg = %s", Boolean.toString(oldDateMatcher != null));

    if (!this.find()) {
      if (traceEnabled()) {
        LogUtils.tracef(
            this, "Lenient Syslog pattern '%s' did not match '%s'", getPattern(), getText());
      }
      return null;
    }

    String timestamp;

    if (oldDateMatcher == null) {
      final Matcher stampMatcher = m_datePattern.matcher(message);
      if (stampMatcher.find()) {
        timestamp = stampMatcher.group(2);
        LogUtils.tracef(this, "found timestamp '%s'", timestamp);
        //                message = message.substring(stampMatcher.group(1).length());
      } else {
        try {
          timestamp = SyslogTimeStamp.getInstance().format(new Date());
        } catch (final IllegalArgumentException ex) {
          LogUtils.debugf(this, "ERROR INTERNAL DATE ERROR!");
          timestamp = "";
        }
      }
    } else {
      timestamp = oldDateMatcher.group(1);
      message = oldDateMatcher.replaceFirst("");
    }

    LogUtils.tracef(this, "timestamp = %s", timestamp);
    syslogMessage.setDate(parseDate(timestamp));

    // These 2 debugs will aid in analyzing the regexes as syslog seems
    // to differ a lot depending on implementation or message structure.

    if (LogUtils.isTraceEnabled(this)) {
      LogUtils.tracef(this, "message = %s", message);
      LogUtils.tracef(this, "pattern = %s", m_forwardingPattern);
      LogUtils.tracef(this, "host group = %d", m_matchingGroupHost);
      LogUtils.tracef(this, "message group = %d", m_matchingGroupMessage);
    }

    // We will also here find out if, the host needs to
    // be replaced, the message matched to a UEI, and
    // last if we need to actually hide the message.
    // this being potentially helpful in avoiding showing
    // operator a password or other data that should be
    // confidential.

    final Pattern pattern = m_forwardingPattern;
    final Matcher m = pattern.matcher(message);

    /*
     * We matched on a regexp for host/message pair.
     * This can be a forwarded message as in BSD Style
     * or syslog-ng.
     */

    if (m.matches()) {

      final String matchedMessage = m.group(m_matchingGroupMessage);
      syslogMessage.setMatchedMessage(matchedMessage);

      if (LogUtils.isTraceEnabled(this)) {
        LogUtils.tracef(
            this, "Syslog message '%s' matched regexp '%s'", message, m_forwardingPattern);
        LogUtils.tracef(this, "Found host '%s'", m.group(m_matchingGroupHost));
        LogUtils.tracef(this, "Found message '%s'", matchedMessage);
      }

      syslogMessage.setHostName(m.group(m_matchingGroupHost));

      message = matchedMessage;
    } else {
      LogUtils.debugf(this, "Regexp not matched: %s", message);
      return null;
    }

    lbIdx = message.indexOf('[');
    rbIdx = message.indexOf(']');
    final int colonIdx = message.indexOf(':');
    final int spaceIdx = message.indexOf(' ');

    int processId = 0;
    String processName = "";
    String processIdStr = "";

    if (lbIdx < (rbIdx - 1) && colonIdx == (rbIdx + 1) && spaceIdx == (colonIdx + 1)) {
      processName = message.substring(0, lbIdx);
      processIdStr = message.substring(lbIdx + 1, rbIdx);
      message = message.substring(colonIdx + 2);

      try {
        processId = Integer.parseInt(processIdStr);
      } catch (final NumberFormatException ex) {
        LogUtils.debugf(this, "Bad process id '%s'", processIdStr);
        processId = 0;
      }
    } else if (lbIdx < 0 && rbIdx < 0 && colonIdx > 0 && spaceIdx == (colonIdx + 1)) {
      processName = message.substring(0, colonIdx);
      message = message.substring(colonIdx + 2);
    }

    syslogMessage.setProcessId(processId);
    syslogMessage.setProcessName(processName);
    syslogMessage.setMessage(message.trim());

    return syslogMessage;
  }