diff --git a/front/events.php b/front/events.php index 77581fad..2a45feed 100755 --- a/front/events.php +++ b/front/events.php @@ -13,7 +13,7 @@ --> @@ -22,7 +22,7 @@

- +

diff --git a/front/php/server/util.php b/front/php/server/util.php index b4ee1e3e..466352ec 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -366,7 +366,7 @@ function encode_single_quotes ($val) { function getDateFromPeriod () { $period = $_REQUEST['period']; - return '"'. date ('Y-m-d', strtotime ('+1 day -'. $period) ) .'"'; + return '"'. date ('Y-m-d', strtotime ('+2 day -'. $period) ) .'"'; } // ------------------------------------------------------------------------------------------- diff --git a/front/plugins/plugin_helper.py b/front/plugins/plugin_helper.py index e198f30a..7072d4f7 100755 --- a/front/plugins/plugin_helper.py +++ b/front/plugins/plugin_helper.py @@ -1,4 +1,30 @@ 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.primaryId = primaryId 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.watched1 = watched1 self.watched2 = watched2 @@ -51,6 +77,7 @@ class Plugin_Object: self.foreignKey, ) return line + class Plugin_Objects: @@ -94,3 +121,7 @@ class Plugin_Objects: for obj in self.objects: fp.write(obj.write()) fp.close() + + + + diff --git a/pialert/__main__.py b/pialert/__main__.py index 536aaec2..46d8e4de 100755 --- a/pialert/__main__.py +++ b/pialert/__main__.py @@ -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']) diff --git a/pialert/api.py b/pialert/api.py index b9efc5e8..ff725180 100755 --- a/pialert/api.py +++ b/pialert/api.py @@ -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 = [ diff --git a/pialert/conf.py b/pialert/conf.py index 0b7f41a9..97fc6d1f 100755 --- a/pialert/conf.py +++ b/pialert/conf.py @@ -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'] diff --git a/pialert/initialise.py b/pialert/initialise.py index 3a526106..a0d4eaee 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -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) diff --git a/pialert/plugin.py b/pialert/plugin.py index d22d53c9..b85d7dd5 100755 --- a/pialert/plugin.py +++ b/pialert/plugin.py @@ -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)) - diff --git a/pialert/reporting.py b/pialert/reporting.py index 1aedc111..80ce270c 100755 --- a/pialert/reporting.py +++ b/pialert/reporting.py @@ -250,7 +250,7 @@ def send_notifications (db): mail_text = mail_text.replace ('', 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"""