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

@@ -13,7 +13,7 @@
--> -->
<?php <?php
require 'php/templates/header.php'; require 'php/templates/header.php';
?> ?>
<!-- Page ------------------------------------------------------------------ --> <!-- Page ------------------------------------------------------------------ -->
@@ -22,7 +22,7 @@
<!-- Content header--------------------------------------------------------- --> <!-- Content header--------------------------------------------------------- -->
<section class="content-header"> <section class="content-header">
<h1 id="pageTitle"> <h1 id="pageTitle">
<i class=\"fa fa-bolt\"></i> <i class="fa fa-bolt"></i>
<?= lang('Events_Title');?> <?= lang('Events_Title');?>
</h1> </h1>

View File

@@ -366,7 +366,7 @@ function encode_single_quotes ($val) {
function getDateFromPeriod () { function getDateFromPeriod () {
$period = $_REQUEST['period']; $period = $_REQUEST['period'];
return '"'. date ('Y-m-d', strtotime ('+1 day -'. $period) ) .'"'; return '"'. date ('Y-m-d', strtotime ('+2 day -'. $period) ) .'"';
} }
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------

View File

@@ -1,4 +1,30 @@
from time import strftime from time import strftime
import pytz
from datetime import datetime
#-------------------------------------------------------------------------------
def read_config_file():
"""
retuns dict on the config file key:value pairs
config_dir[key]
"""
filename = '/home/pi/pialert/config/pialert.conf'
print('[plugin_helper] reading config file')
# load the variables from pialert.conf
code = compile(filename.read_text(), filename.name, "exec")
confDict = {} # config dictionary
exec(code, {"__builtins__": {}}, confDict)
return confDict
# -------------------------------------------------------------------
pialertConfigFile = read_config_file()
timeZoneSetting = pialertConfigFile['TIMEZONE']
timeZone = pytz.timezone(timeZoneSetting)
# ------------------------------------------------------------------- # -------------------------------------------------------------------
@@ -23,7 +49,7 @@ class Plugin_Object:
self.pluginPref = "" self.pluginPref = ""
self.primaryId = primaryId self.primaryId = primaryId
self.secondaryId = secondaryId self.secondaryId = secondaryId
self.created = strftime("%Y-%m-%d %H:%M:%S") self.created = datetime.now(timeZone).strftime("%Y-%m-%d %H:%M:%S")
self.changed = "" self.changed = ""
self.watched1 = watched1 self.watched1 = watched1
self.watched2 = watched2 self.watched2 = watched2
@@ -51,6 +77,7 @@ class Plugin_Object:
self.foreignKey, self.foreignKey,
) )
return line return line
class Plugin_Objects: class Plugin_Objects:
@@ -94,3 +121,7 @@ class Plugin_Objects:
for obj in self.objects: for obj in self.objects:
fp.write(obj.write()) fp.write(obj.write())
fp.close() fp.close()

View File

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

View File

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

View File

@@ -83,8 +83,7 @@ def importConfigs (db):
# General # General
conf.LOG_LEVEL = ccd('LOG_LEVEL', 'verbose' , c_d, 'Log verboseness', 'text.select', "['none', 'minimal', 'verbose', 'debug']", '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.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.PLUGINS_KEEP_HIST = ccd('PLUGINS_KEEP_HIST', 10000 , c_d, 'Keep history entries', 'integer', '', '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_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') 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_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.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.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) # ARPSCAN (+ other settings provided by the ARPSCAN plugin)
conf.ENABLE_ARPSCAN = ccd('ENABLE_ARPSCAN', True , c_d, 'Enable arpscan', 'boolean', '', 'ARPSCAN', ['run']) 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_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 = 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_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') 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')
# Init timezone in case it changed # Init timezone in case it changed
conf.tz = timezone(conf.TIMEZONE) conf.tz = timezone(conf.TIMEZONE)
@@ -280,7 +277,7 @@ def importConfigs (db):
update_api(db, False, ["settings"]) update_api(db, False, ["settings"])
# run plugins that are modifying the config # 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 # Used to determine the next import
conf.lastImportedConfFile = os.path.getmtime(config_file) 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 helper import timeNowTZ, updateState, get_file_content, write_file, get_setting, get_setting_value
from api import update_api 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 # Header
updateState(db,"Run: Plugins") updateState(db,"Run: Plugins")
@@ -47,7 +53,9 @@ def run_plugin_scripts(db, runType):
print_plugin_info(plugin, ['display_name']) print_plugin_info(plugin, ['display_name'])
mylog('debug', ['[Plugins] CMD: ', get_plugin_setting(plugin, "CMD")["value"]]) 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 # 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 sql = db.sql
# ------- necessary settings check -------- # ------- 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) 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() db.commitDB()
process_plugin_events(db, plugin) pluginsState = process_plugin_events(db, plugin, pluginsState)
# update API endpoints # 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): def custom_plugin_decoder(pluginDict):
@@ -423,11 +433,8 @@ def combine_plugin_objects(old, new):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Check if watched values changed for the given plugin # 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 sql = db.sql
# capturing if we need to process scan results for devices
conf.currentScanNeedsProcessing = False
pluginPref = plugin["unique_prefix"] pluginPref = plugin["unique_prefix"]
@@ -597,13 +604,16 @@ def process_plugin_events(db, plugin):
sql.executemany(q, sqlParams) sql.executemany(q, sqlParams)
db.commitDB() db.commitDB()
# perform scan if mapped to CurrentScan table
if dbTable == 'CurrentScan':
conf.currentScanNeedsProcessing = True
db.commitDB() db.commitDB()
# perform scan if mapped to CurrentScan table
if dbTable == 'CurrentScan':
pluginsState.processScan = True
return pluginsState
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
class plugin_object_class: class plugin_object_class:
@@ -648,5 +658,4 @@ class plugin_object_class:
self.watchedHash = str(hash(tmp)) self.watchedHash = str(hash(tmp))

View File

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