Plugins 0.1 - Multi-Execution support + Fix #177

This commit is contained in:
Jokob-sk
2023-02-08 22:53:05 +11:00
parent 5b8f8f2c5d
commit c287bc2f22
5 changed files with 65 additions and 32 deletions

View File

@@ -51,6 +51,15 @@ sql_nmap_scan_all = "SELECT * FROM Nmap_Scan"
sql_pholus_scan_all = "SELECT * FROM Pholus_Scan" sql_pholus_scan_all = "SELECT * FROM Pholus_Scan"
sql_events_pending_alert = "SELECT * FROM Events where eve_PendingAlertEmail is not 0" sql_events_pending_alert = "SELECT * FROM Events where eve_PendingAlertEmail is not 0"
sql_settings = "SELECT * FROM Settings" sql_settings = "SELECT * FROM Settings"
sql_new_devices = """SELECT * FROM ( SELECT eve_IP as dev_LastIP, eve_MAC as dev_MAC FROM Events_Devices
WHERE eve_PendingAlertEmail = 1
AND eve_EventType = 'New Device'
ORDER BY eve_DateTime ) t1
LEFT JOIN
(
SELECT dev_Name, dev_MAC as dev_MAC_t2 FROM Devices
) t2
ON t1.dev_MAC = t2.dev_MAC_t2"""
#=============================================================================== #===============================================================================
# PATHS # PATHS
@@ -439,7 +448,7 @@ def importConfigs ():
for plugin in plugins.list: for plugin in plugins.list:
print_plugin_info(plugin, ['display_name','description']) print_plugin_info(plugin, ['display_name','description'])
pref = plugin["settings_short_prefix"] pref = plugin["unique_prefix"]
# collect plugin level language strings # collect plugin level language strings
collect_lang_strings(plugin, pref) collect_lang_strings(plugin, pref)
@@ -532,7 +541,7 @@ def main ():
# Handle plugins executed ONCE # Handle plugins executed ONCE
if plugins_once_run == False: if plugins_once_run == False:
run_plugin_script('once') run_plugin_scripts('once')
plugins_once_run = True 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
@@ -556,9 +565,13 @@ def main ():
# Timestamp # Timestamp
startTime = time_started startTime = time_started
startTime = startTime.replace (microsecond=0) startTime = startTime.replace (microsecond=0)
# Check if any plugins need to run on schedule
run_plugin_scripts('schedule')
# determine run/scan type based on passed time # determine run/scan type based on passed time
# --------------------------------------------
# check for changes in Internet IP # check for changes in Internet IP
if last_internet_IP_scan + datetime.timedelta(minutes=3) < time_started: if last_internet_IP_scan + datetime.timedelta(minutes=3) < time_started:
@@ -629,23 +642,19 @@ def main ():
# Reporting # Reporting
if cycle in check_report: if cycle in check_report:
# Check if new devices need to be scanned with Nmap # Check if new devices found
if NMAP_ACTIVE: sql.execute (sql_new_devices)
sql.execute ("""SELECT * FROM newDevices = sql.fetchall()
( SELECT eve_IP as dev_LastIP, eve_MAC as dev_MAC FROM Events_Devices commitDB()
WHERE eve_PendingAlertEmail = 1
AND eve_EventType = 'New Device' # new devices were found
ORDER BY eve_DateTime ) t1 if len(newDevices) > 0:
LEFT JOIN # run all plugins registered to be run when new devices are found
( run_plugin_scripts('on_new_device')
SELECT dev_Name, dev_MAC as dev_MAC_t2 FROM Devices
) t2
ON t1.dev_MAC = t2.dev_MAC_t2""")
newDevices = sql.fetchall() # Scan newly found devices with Nmap if enabled
commitDB() if NMAP_ACTIVE and len(newDevices) > 0:
performNmapScan(newDevices)
performNmapScan(newDevices)
# send all configured notifications # send all configured notifications
send_notifications() send_notifications()
@@ -910,6 +919,7 @@ def update_devices_MAC_vendors (pArg = ''):
update_output = subprocess.check_output (update_args) update_output = subprocess.check_output (update_args)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
# An error occured, handle it # An error occured, handle it
file_print(' FAILED: Updating vendors DB ')
file_print(e.output) file_print(e.output)
# Initialize variables # Initialize variables
@@ -1079,6 +1089,9 @@ def scan_network ():
# Commit changes # Commit changes
commitDB() commitDB()
# Run splugin scripts which are set to run every timne after a scan finished
run_plugin_scripts('always_after_scan')
return reporting return reporting
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@@ -2887,7 +2900,7 @@ def mqtt_start():
# Get all devices # Get all devices
devices = get_all_devices() devices = get_all_devices()
file_print(" Estimated delay: ", (len(devices) * int(MQTT_DELAY_SEC)), 's ', '(', round((len(devices) * int(MQTT_DELAY_SEC))/60,1) , 'min)' ) file_print(" Estimated delay: ", (len(devices) * int(MQTT_DELAY_SEC)*5), 's ', '(', round((len(devices) * int(MQTT_DELAY_SEC))/60,1) , 'min)' )
for device in devices: for device in devices:
@@ -3586,19 +3599,38 @@ def custom_plugin_decoder(pluginDict):
return namedtuple('X', pluginDict.keys())(*pluginDict.values()) return namedtuple('X', pluginDict.keys())(*pluginDict.values())
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def run_plugin_script(runType): def run_plugin_scripts(runType):
global plugins global plugins, tz, mySchedules
file_print(' [Plugins] Check if any plugins need to be executed on run type: ', runType)
for plugin in plugins.list: for plugin in plugins.list:
shouldRun = False
set = get_plugin_setting(plugin, "RUN") set = get_plugin_setting(plugin, "RUN")
if set['value'] == runType: if set['value'] == runType:
file_print(' [Plugin] Run') if runType != "schedule":
print_plugin_info(plugin, ['display_name']) shouldRun = True
file_print(' [Plugin] CMD', get_plugin_setting(plugin, "CMD")["value"]) elif runType == "schedule":
# run if overdue scheduled time
prefix = plugin["unique_prefix"]
# check scheduels if any contains a unique plugin prefix matching the current plugin
for schd in mySchedules:
if schd.service == prefix:
# Check if schedule overdue
shouldRun = schd.runScheduleCheck()
if shouldRun:
# note the last time the scheduled plugin run was executed
schd.last_run = datetime.datetime.now(tz).replace(microsecond=0)
if shouldRun:
print_plugin_info(plugin, ['display_name'])
file_print(' [Plugins] CMD: ', get_plugin_setting(plugin, "CMD")["value"])
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def get_plugin_setting(plugin, key): def get_plugin_setting(plugin, key):
@@ -3628,11 +3660,11 @@ def get_plugin_string(props, el):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def print_plugin_info(plugin, elements = ['display_name']): def print_plugin_info(plugin, elements = ['display_name']):
file_print(' ---------------------------------------------') file_print(' [Plugins] ---------------------------------------------')
for el in elements: for el in elements:
res = get_plugin_string(plugin, el) res = get_plugin_string(plugin, el)
file_print(' ', el ,': ', res) file_print(' [Plugins] ', el ,': ', res)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Cron-like Scheduling # Cron-like Scheduling

View File

@@ -1623,7 +1623,7 @@ function overwriteIconType () {
var icon = $('#txtIcon').val(); var icon = $('#txtIcon').val();
// Delete device events // Mass update icons
$.get('php/server/devices.php?action=overwriteIconType&mac='+ mac + '&icon=' + icon, function(msg) { $.get('php/server/devices.php?action=overwriteIconType&mac='+ mac + '&icon=' + icon, function(msg) {
showMessage (msg); showMessage (msg);
}); });

View File

@@ -1184,7 +1184,7 @@ function overwriteIconType()
if ($result == TRUE) { if ($result == TRUE) {
echo 'OK'; echo 'OK';
} else { } else {
echo 'KO'; echo lang('BackDevices_Device_UpdDevError');
} }
} }

View File

@@ -347,6 +347,7 @@ $lang['en_us'] = array(
'BackDevices_DBTools_ImportCSV' => 'The devices from the CSV file were imported successfully.', 'BackDevices_DBTools_ImportCSV' => 'The devices from the CSV file were imported successfully.',
'BackDevices_DBTools_ImportCSVError' => 'The CSV file couldn\'t be imported. Make sure the format is correct.', 'BackDevices_DBTools_ImportCSVError' => 'The CSV file couldn\'t be imported. Make sure the format is correct.',
'BackDevices_DBTools_ImportCSVMissing' => 'The CSV file couldn\'t be found under <b>/config/devices.csv.</b>', 'BackDevices_DBTools_ImportCSVMissing' => 'The CSV file couldn\'t be found under <b>/config/devices.csv.</b>',
'BackDevices_Device_UpdDevError' => 'Error updating devices, try later. The database is probable locked due to an ongoing task.',
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////

View File

@@ -1,6 +1,6 @@
{ {
"code_name": "website_monitor", "code_name": "website_monitor",
"settings_short_prefix": "WEBMON", "unique_prefix": "WEBMON",
"localized": ["display_name", "description", "icon"], "localized": ["display_name", "description", "icon"],
"display_name" : [{ "display_name" : [{
"language_code":"en_us", "language_code":"en_us",