Events, CurrentScan, pluginsState, ENABLE_PLUGINS work

This commit is contained in:
Jokob-sk
2023-08-12 11:56:35 +10:00
parent 82d7c28fd7
commit 5397edc14d
9 changed files with 78 additions and 59 deletions

View File

@@ -120,8 +120,8 @@ def main ():
conf.newVersionAvailable = isNewVersion(conf.newVersionAvailable)
# Handle plugins executed ONCE
if conf.ENABLE_PLUGINS and conf.plugins_once_run == False:
run_plugin_scripts(db, 'once')
if conf.plugins_once_run == False:
pluginsState = run_plugin_scripts(db, 'once')
conf.plugins_once_run = True
# check if there is a front end initiated event which needs to be executed
@@ -145,8 +145,7 @@ def main ():
startTime = startTime.replace (microsecond=0)
# Check if any plugins need to run on schedule
if conf.ENABLE_PLUGINS:
run_plugin_scripts(db,'schedule')
pluginsState = run_plugin_scripts(db,'schedule', pluginsState)
# determine run/scan type based on passed time
# --------------------------------------------
@@ -201,16 +200,17 @@ def main ():
performNmapScan(db, get_all_devices(db))
# Run splugin scripts which are set to run every timne after a scans finished
if conf.ENABLE_PLUGINS:
run_plugin_scripts(db,'always_after_scan')
# Run splugin scripts which are set to run every timne after a scans finished
pluginsState = run_plugin_scripts(db,'always_after_scan', pluginsState)
# process all the scanned data into new devices
if conf.currentScanNeedsProcessing == True:
mylog('debug', "[MAIN] start processig scan results")
mylog('debug', [f'[MAIN] processScan: {pluginsState.processScan}'])
if pluginsState.processScan == True:
mylog('debug', "[MAIN] start processig scan results")
pluginsState.processScan = False
process_scan(db)
conf.currentScanNeedsProcessing = False
# Reporting
if conf.cycle in conf.check_report:
@@ -221,9 +221,8 @@ def main ():
# new devices were found
if len(newDevices) > 0:
# run all plugins registered to be run when new devices are found
if conf.ENABLE_PLUGINS:
run_plugin_scripts(db, 'on_new_device')
# run all plugins registered to be run when new devices are found
pluginsState = run_plugin_scripts(db, 'on_new_device', pluginsState)
# Scan newly found devices with Nmap if enabled
if conf.NMAP_ACTIVE and len(newDevices) > 0:
@@ -242,15 +241,6 @@ def main ():
# Commit SQL
db.commitDB()
# Final message
if conf.cycle != "":
action = str(conf.cycle)
if action == "1":
action = "network_scan"
mylog('verbose', ['[MAIN] Last action: ', action])
conf.cycle = ""
mylog('verbose', ['[MAIN] cycle:',conf.cycle])
# Footer
updateState(db,"Process: Wait")
mylog('verbose', ['[MAIN] Process: Wait'])

View File

@@ -21,9 +21,8 @@ def update_api(db, isNotification = False, updateOnlyDataSources = []):
# update notifications moved to reporting send_api()
# Save plugins
if conf.ENABLE_PLUGINS:
write_file(folder + 'plugins.json' , json.dumps({"data" : conf.plugins}))
# Save plugins
write_file(folder + 'plugins.json' , json.dumps({"data" : conf.plugins}))
# prepare database tables we want to expose
dataSourcesSQLs = [

View File

@@ -2,10 +2,6 @@
# TODO: Create and manage this as part of an app_state class object
#===============================================================================
# class app_state:
# def __init__(self, currentScanNeedsProcessing = False):
# self.currentScanNeedsProcessing = currentScanNeedsProcessing
# These are global variables, not config items and should not exist !
mySettings = []
@@ -21,8 +17,6 @@ tz = ''
# set to a small value to force import at first run
lastImportedConfFile = 1.1
currentScanNeedsProcessing = False
plugins_once_run = False
newVersionAvailable = False
time_started = ''
@@ -49,7 +43,6 @@ ENABLE_ARPSCAN = True
SCAN_SUBNETS = ['192.168.1.0/24 --interface=eth1', '192.168.1.0/24 --interface=eth0']
LOG_LEVEL = 'verbose'
TIMEZONE = 'Europe/Berlin'
ENABLE_PLUGINS = True
PIALERT_WEB_PROTECTION = False
PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
INCLUDED_SECTIONS = ['internet', 'new_devices', 'down_devices', 'events', 'ports']

View File

@@ -83,8 +83,7 @@ def importConfigs (db):
# General
conf.LOG_LEVEL = ccd('LOG_LEVEL', 'verbose' , c_d, 'Log verboseness', 'text.select', "['none', 'minimal', 'verbose', 'debug']", 'General')
conf.TIMEZONE = ccd('TIMEZONE', 'Europe/Berlin' , c_d, 'Time zone', 'text', '', 'General')
conf.ENABLE_PLUGINS = ccd('ENABLE_PLUGINS', True , c_d, 'Enable plugins', 'boolean', '', 'General')
conf.TIMEZONE = ccd('TIMEZONE', 'Europe/Berlin' , c_d, 'Time zone', 'text', '', 'General')
conf.PLUGINS_KEEP_HIST = ccd('PLUGINS_KEEP_HIST', 10000 , c_d, 'Keep history entries', 'integer', '', 'General')
conf.PIALERT_WEB_PROTECTION = ccd('PIALERT_WEB_PROTECTION', False , c_d, 'Enable logon', 'boolean', '', 'General')
conf.PIALERT_WEB_PASSWORD = ccd('PIALERT_WEB_PASSWORD', '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92' , c_d, 'Logon password', 'readonly', '', 'General')
@@ -94,7 +93,8 @@ def importConfigs (db):
conf.UI_LANG = ccd('UI_LANG', 'English' , c_d, 'Language Interface', 'text.select', "['English', 'German', 'Spanish']", 'General')
conf.UI_PRESENCE = ccd('UI_PRESENCE', ['online', 'offline', 'archived'] , c_d, 'Include in presence', 'text.multiselect', "['online', 'offline', 'archived']", 'General')
conf.DAYS_TO_KEEP_EVENTS = ccd('DAYS_TO_KEEP_EVENTS', 90 , c_d, 'Delete events days', 'integer', '', 'General')
conf.HRS_TO_KEEP_NEWDEV = ccd('HRS_TO_KEEP_NEWDEV', 0 , c_d, 'Keep new devices for', 'integer', "0", 'General')
conf.HRS_TO_KEEP_NEWDEV = ccd('HRS_TO_KEEP_NEWDEV', 0 , c_d, 'Keep new devices for', 'integer', "0", 'General')
conf.API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'General')
# ARPSCAN (+ other settings provided by the ARPSCAN plugin)
conf.ENABLE_ARPSCAN = ccd('ENABLE_ARPSCAN', True , c_d, 'Enable arpscan', 'boolean', '', 'ARPSCAN', ['run'])
@@ -166,10 +166,7 @@ def importConfigs (db):
conf.NMAP_TIMEOUT = ccd('NMAP_TIMEOUT', 150 , c_d, 'Nmap timeout', 'integer', '', 'Nmap')
conf.NMAP_RUN = ccd('NMAP_RUN', 'disabled' , c_d, 'Nmap enable schedule', 'text.select', "['disabled', 'once', 'schedule']", 'Nmap')
conf.NMAP_RUN_SCHD = ccd('NMAP_RUN_SCHD', '0 2 * * *' , c_d, 'Nmap schedule', 'text', '', 'Nmap')
conf.NMAP_ARGS = ccd('NMAP_ARGS', '-p -10000' , c_d, 'Nmap custom arguments', 'text', '', 'Nmap')
# API
conf.API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'API')
conf.NMAP_ARGS = ccd('NMAP_ARGS', '-p -10000' , c_d, 'Nmap custom arguments', 'text', '', 'Nmap')
# Init timezone in case it changed
conf.tz = timezone(conf.TIMEZONE)
@@ -280,7 +277,7 @@ def importConfigs (db):
update_api(db, False, ["settings"])
# run plugins that are modifying the config
run_plugin_scripts(db, 'before_config_save')
pluginsState = run_plugin_scripts(db, 'before_config_save')
# Used to determine the next import
conf.lastImportedConfFile = os.path.getmtime(config_file)

View File

@@ -13,8 +13,14 @@ from logger import mylog
from helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value
from api import update_api
#-------------------------------------------------------------------------------
def run_plugin_scripts(db, runType):
class plugins_state:
def __init__(self, processScan = False):
self.processScan = processScan
#-------------------------------------------------------------------------------
def run_plugin_scripts(db, runType, pluginsState = plugins_state()):
# Header
updateState(db,"Run: Plugins")
@@ -47,7 +53,9 @@ def run_plugin_scripts(db, runType):
print_plugin_info(plugin, ['display_name'])
mylog('debug', ['[Plugins] CMD: ', get_plugin_setting(plugin, "CMD")["value"]])
execute_plugin(db, plugin)
pluginsState = execute_plugin(db, plugin, pluginsState)
return pluginsState
@@ -120,7 +128,7 @@ def get_plugin_string(props, el):
#-------------------------------------------------------------------------------
# Executes the plugin command specified in the setting with the function specified as CMD
def execute_plugin(db, plugin):
def execute_plugin(db, plugin, pluginsState = plugins_state() ):
sql = db.sql
# ------- necessary settings check --------
@@ -289,10 +297,12 @@ def execute_plugin(db, plugin):
sql.executemany ("""INSERT INTO Plugins_History ("Plugin", "Object_PrimaryID", "Object_SecondaryID", "DateTimeCreated", "DateTimeChanged", "Watched_Value1", "Watched_Value2", "Watched_Value3", "Watched_Value4", "Status" ,"Extra", "UserData", "ForeignKey") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", sqlParams)
db.commitDB()
process_plugin_events(db, plugin)
pluginsState = process_plugin_events(db, plugin, pluginsState)
# update API endpoints
update_api(db, False, ["plugins_events","plugins_objects"])
update_api(db, False, ["plugins_events","plugins_objects"])
return pluginsState
#-------------------------------------------------------------------------------
def custom_plugin_decoder(pluginDict):
@@ -423,11 +433,8 @@ def combine_plugin_objects(old, new):
#-------------------------------------------------------------------------------
# Check if watched values changed for the given plugin
def process_plugin_events(db, plugin):
def process_plugin_events(db, plugin, pluginsState):
sql = db.sql
# capturing if we need to process scan results for devices
conf.currentScanNeedsProcessing = False
pluginPref = plugin["unique_prefix"]
@@ -597,13 +604,16 @@ def process_plugin_events(db, plugin):
sql.executemany(q, sqlParams)
db.commitDB()
# perform scan if mapped to CurrentScan table
if dbTable == 'CurrentScan':
conf.currentScanNeedsProcessing = True
db.commitDB()
# perform scan if mapped to CurrentScan table
if dbTable == 'CurrentScan':
pluginsState.processScan = True
return pluginsState
#-------------------------------------------------------------------------------
class plugin_object_class:
@@ -648,5 +658,4 @@ class plugin_object_class:
self.watchedHash = str(hash(tmp))

View File

@@ -250,7 +250,7 @@ def send_notifications (db):
mail_text = mail_text.replace ('<PORTS_TABLE>', portsTxt )
mylog('verbose', ['[Notification] Ports sections done.'])
if 'plugins' in conf.INCLUDED_SECTIONS and conf.ENABLE_PLUGINS:
if 'plugins' in conf.INCLUDED_SECTIONS:
# Compose Plugins Section
sqlQuery = """SELECT Plugin, Object_PrimaryId, Object_SecondaryId, DateTimeChanged, Watched_Value1, Watched_Value2, Watched_Value3, Watched_Value4, Status from Plugins_Events"""