Events, CurrentScan, pluginsState, ENABLE_PLUGINS work
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
<!-- Content header--------------------------------------------------------- -->
|
||||
<section class="content-header">
|
||||
<h1 id="pageTitle">
|
||||
<i class=\"fa fa-bolt\"></i>
|
||||
<i class="fa fa-bolt"></i>
|
||||
<?= lang('Events_Title');?>
|
||||
</h1>
|
||||
|
||||
|
||||
@@ -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) ) .'"';
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -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
|
||||
@@ -53,6 +79,7 @@ class Plugin_Object:
|
||||
return line
|
||||
|
||||
|
||||
|
||||
class Plugin_Objects:
|
||||
"""
|
||||
Plugin_Objects is the class that manages and holds all the objects created by the plugin.
|
||||
@@ -94,3 +121,7 @@ class Plugin_Objects:
|
||||
for obj in self.objects:
|
||||
fp.write(obj.write())
|
||||
fp.close()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
# --------------------------------------------
|
||||
@@ -202,15 +201,16 @@ def main ():
|
||||
|
||||
|
||||
# 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')
|
||||
pluginsState = run_plugin_scripts(db,'always_after_scan', pluginsState)
|
||||
|
||||
|
||||
# process all the scanned data into new devices
|
||||
if conf.currentScanNeedsProcessing == True:
|
||||
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:
|
||||
@@ -222,8 +222,7 @@ 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')
|
||||
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'])
|
||||
|
||||
@@ -22,7 +22,6 @@ 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}))
|
||||
|
||||
# prepare database tables we want to expose
|
||||
|
||||
@@ -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']
|
||||
|
||||
@@ -84,7 +84,6 @@ 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.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')
|
||||
@@ -95,6 +94,7 @@ def importConfigs (db):
|
||||
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.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'])
|
||||
@@ -168,9 +168,6 @@ def importConfigs (db):
|
||||
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')
|
||||
|
||||
# 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)
|
||||
|
||||
@@ -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,11 +297,13 @@ 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"])
|
||||
|
||||
return pluginsState
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
def custom_plugin_decoder(pluginDict):
|
||||
return namedtuple('X', pluginDict.keys())(*pluginDict.values())
|
||||
@@ -423,12 +433,9 @@ 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"]
|
||||
|
||||
mylog('debug', ['[Plugins] Processing : ', pluginPref])
|
||||
@@ -598,12 +605,15 @@ def process_plugin_events(db, plugin):
|
||||
|
||||
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:
|
||||
@@ -649,4 +659,3 @@ class plugin_object_class:
|
||||
self.watchedHash = str(hash(tmp))
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user