📄 scheduler.java
字号:
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Copyright (C) 1999-2001 Oculan Corp. All rights reserved.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details. //// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.// // For more information contact: // OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///package org.opennms.netmgt.vulnscand;import java.lang.reflect.UndeclaredThrowableException;import java.net.InetAddress;import java.net.UnknownHostException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.sql.Timestamp;import java.util.Collections;import java.util.Enumeration;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Set;import java.util.TreeSet;import org.apache.log4j.Category;import org.opennms.core.fiber.PausableFiber;import org.opennms.core.queue.FifoQueue;import org.opennms.core.queue.FifoQueueException;import org.opennms.core.utils.ThreadCategory;import org.opennms.netmgt.config.DatabaseConnectionFactory;import org.opennms.netmgt.config.VulnscandConfigFactory;import org.opennms.netmgt.config.vulnscand.ScanLevel;import org.opennms.netmgt.config.vulnscand.VulnscandConfiguration;/** * This class implements a simple scheduler to ensure that Vulnscand rescans * occurs at the expected intervals. * * @author <a href="mailto:mike@opennms.org">Mike Davidson </a> * @author <a href="http://www.opennms.org/">OpenNMS </a> * */final class Scheduler implements Runnable, PausableFiber { /** * The prefix for the fiber name. */ private static final String FIBER_NAME = "Vulnscand Scheduler"; /** * The SQL statement used to retrieve all non-deleted/non-forced unamanaged * IP interfaces from the 'ipInterface' table. */ private static final String SQL_DB_RETRIEVE_IP_INTERFACE = "SELECT ipaddr FROM ipinterface WHERE ipaddr!='0.0.0.0' AND isManaged!='D' AND isManaged!='F'"; /** * SQL used to retrieve the last poll time for all the managed interfaces * belonging to a particular node. */ private static final String SQL_GET_LAST_POLL_TIME = "SELECT lastAttemptTime FROM vulnerabilities WHERE ipaddr=? ORDER BY lastAttemptTime DESC"; /** * The name of this fiber. */ private String m_name; /** * The status for this fiber. */ private int m_status; /** * The worker thread that executes this instance. */ private Thread m_worker; /** * List of NessusScanConfiguration objects representing the IP addresses * that will be scheduled. */ private List m_knownAddresses; /** * The configured interval (in milliseconds) between rescans */ private long m_interval; /** * The configured initial sleep (in milliseconds) prior to scheduling * rescans */ private long m_initialSleep; /** * The rescan queue where new NessusScan objects are enqueued for execution. */ private FifoQueue m_scheduledScanQ; /** * Constructs a new instance of the scheduler. * */ Scheduler(FifoQueue rescanQ) throws SQLException { Category log = ThreadCategory.getInstance(Scheduler.class); m_scheduledScanQ = rescanQ; m_name = FIBER_NAME; m_status = START_PENDING; m_worker = null; m_knownAddresses = Collections.synchronizedList(new LinkedList()); // Get rescan interval from configuration factory // m_interval = VulnscandConfigFactory.getInstance().getRescanFrequency(); if (log.isDebugEnabled()) log.debug("Scheduler: rescan interval(millis): " + m_interval); // Get initial rescan sleep time from configuration factory // m_initialSleep = VulnscandConfigFactory.getInstance().getInitialSleepTime(); if (log.isDebugEnabled()) log.debug("Scheduler: initial rescan sleep time(millis): " + m_initialSleep); // Load the list of IP addresses from the config file and schedule // them in the appropriate level VulnscandConfigFactory configFactory = VulnscandConfigFactory.getInstance(); VulnscandConfiguration config = VulnscandConfigFactory.getConfiguration(); // If the status of the daemon is "true" (meaning "on")... if (config.getStatus()) { Enumeration scanLevels = config.enumerateScanLevel(); while (scanLevels.hasMoreElements()) { ScanLevel scanLevel = (ScanLevel) scanLevels.nextElement(); int level = scanLevel.getLevel(); // Grab the list of included addresses for this level Set levelAddresses = new TreeSet(); // If scanning of the managed IPs is enabled... if (configFactory.getManagedInterfacesStatus()) { /* * And the managed IPs are set to be scanned at the * current level... */ if (configFactory.getManagedInterfacesScanLevel() == level) { // Then schedule those puppies to be scanned levelAddresses.addAll(getAllManagedInterfaces()); log.info("Scheduled the managed interfaces at scan level " + level + "."); } } /* * Remove all of the excluded addresses. The excluded * addresses are cached, so this operation is lighter * than constructing the exclusion list each time. */ levelAddresses.removeAll(configFactory.getAllExcludes()); log.info("Adding " + levelAddresses.size() + " addresses to the vulnerability scan scheduler."); Iterator itr = levelAddresses.iterator(); while (itr.hasNext()) { Object next = itr.next(); String nextAddress = null; if (next instanceof String) { nextAddress = (String) next; log.debug("LevelAddresses : " + nextAddress); } try { // All we know right now is the IP..... InetAddress nextInetAddr = InetAddress.getByName(nextAddress); addToKnownAddresses(nextInetAddr, level); } catch (UnknownHostException ex) { log.error("Could not add invalid address to schedule: " + nextAddress, ex); } } } } else { log.info("Vulnerability scanning is DISABLED."); } } private Set getAllManagedInterfaces() { Category log = ThreadCategory.getInstance(Scheduler.class); Set retval = new TreeSet(); String addressString = null; Connection connection = null; Statement selectInterfaces = null; ResultSet interfaces = null; try { connection = DatabaseConnectionFactory.getInstance().getConnection(); selectInterfaces = connection.createStatement(); interfaces = selectInterfaces.executeQuery(SQL_DB_RETRIEVE_IP_INTERFACE); int i = 0; while (interfaces.next()) { addressString = interfaces.getString(1); if (addressString != null) { retval.add(addressString); log.debug("address: " + addressString); } else { log.warn("UNEXPECTED CONDITION: NULL string in the results of the query for managed interfaces from the ipinterface table."); } i++; } log.info("Loaded " + i + " managed interfaces from the database."); } catch (SQLException ex) { log.error(ex.getLocalizedMessage(), ex); } finally { try { if (interfaces != null) { interfaces.close(); } if (selectInterfaces != null) { selectInterfaces.close(); } } catch (Exception ex) { } finally { try { if (connection != null) { connection.close(); } } catch (Exception e) { } } } return retval; } /** * Creates a NessusScanConfiguration object representing the specified node * and adds it to the known node list for scheduling. * * @param address * the internet address. * @param scanLevel * the scan level. * * @throws SQLException * if there is any problem accessing the database */ void addToKnownAddresses(InetAddress address, int scanLevel) throws SQLException { Category log = ThreadCategory.getInstance(getClass());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -