ARPSCAN to plugin rewrite

This commit is contained in:
Jokob-sk
2023-08-07 16:22:27 +10:00
parent 1bdfc1962a
commit 5f3f4c1a73
2 changed files with 75 additions and 68 deletions

View File

@@ -292,7 +292,6 @@ def passable_string_from_setting(globalSetting):
setVal = globalSetting[6] # setting value setVal = globalSetting[6] # setting value
setTyp = globalSetting[3] # setting type setTyp = globalSetting[3] # setting type
noConversion = ['text', 'string', 'integer', 'boolean', 'password', 'readonly', 'integer.select', 'text.select', 'integer.checkbox' ] noConversion = ['text', 'string', 'integer', 'boolean', 'password', 'readonly', 'integer.select', 'text.select', 'integer.checkbox' ]
arrayConversion = ['text.multiselect', 'list'] arrayConversion = ['text.multiselect', 'list']
arrayConversionBase64 = ['subnets'] arrayConversionBase64 = ['subnets']
@@ -515,77 +514,77 @@ def process_plugin_events(db, plugin):
# Perform databse table mapping if enabled for the plugin # Perform databse table mapping if enabled for the plugin
if len(pluginEvents) > 0 and "mapped_to_table" in plugin: if len(pluginEvents) > 0 and "mapped_to_table" in plugin:
# Initialize an empty list to store SQL parameters. # Initialize an empty list to store SQL parameters.
sqlParams = [] sqlParams = []
# Get the database table name from the 'mapped_to_table' key in the 'plugin' dictionary. # Get the database table name from the 'mapped_to_table' key in the 'plugin' dictionary.
dbTable = plugin['mapped_to_table'] dbTable = plugin['mapped_to_table']
# Log a debug message indicating the mapping of objects to the database table. # Log a debug message indicating the mapping of objects to the database table.
mylog('debug', ['[Plugins] Mapping objects to database table: ', dbTable]) mylog('debug', ['[Plugins] Mapping objects to database table: ', dbTable])
# Initialize lists to hold mapped column names, columnsStr, and valuesStr for SQL query. # Initialize lists to hold mapped column names, columnsStr, and valuesStr for SQL query.
mappedCols = [] mappedCols = []
columnsStr = '' columnsStr = ''
valuesStr = '' valuesStr = ''
# Loop through the 'database_column_definitions' in the 'plugin' dictionary to collect mapped columns. # Loop through the 'database_column_definitions' in the 'plugin' dictionary to collect mapped columns.
# Build the columnsStr and valuesStr for the SQL query. # Build the columnsStr and valuesStr for the SQL query.
for clmn in plugin['database_column_definitions']: for clmn in plugin['database_column_definitions']:
if 'mapped_to_column' in clmn: if 'mapped_to_column' in clmn:
mappedCols.append(clmn) mappedCols.append(clmn)
columnsStr = f'{columnsStr}, "{clmn["mapped_to_column"]}"' columnsStr = f'{columnsStr}, "{clmn["mapped_to_column"]}"'
valuesStr = f'{valuesStr}, ?' valuesStr = f'{valuesStr}, ?'
# Remove the first ',' from columnsStr and valuesStr. # Remove the first ',' from columnsStr and valuesStr.
if len(columnsStr) > 0: if len(columnsStr) > 0:
columnsStr = columnsStr[1:] columnsStr = columnsStr[1:]
valuesStr = valuesStr[1:] valuesStr = valuesStr[1:]
# Map the column names to plugin object event values and create a list of tuples 'sqlParams'. # Map the column names to plugin object event values and create a list of tuples 'sqlParams'.
for plgEv in pluginEvents: for plgEv in pluginEvents:
tmpList = [] tmpList = []
for col in mappedCols: for col in mappedCols:
if col['column'] == 'Index': if col['column'] == 'Index':
tmpList.append(plgEv.index) tmpList.append(plgEv.index)
elif col['column'] == 'Plugin': elif col['column'] == 'Plugin':
tmpList.append(plgEv.pluginPref) tmpList.append(plgEv.pluginPref)
elif col['column'] == 'Object_PrimaryID': elif col['column'] == 'Object_PrimaryID':
tmpList.append(plgEv.primaryId) tmpList.append(plgEv.primaryId)
elif col['column'] == 'Object_SecondaryID': elif col['column'] == 'Object_SecondaryID':
tmpList.append(plgEv.secondaryId) tmpList.append(plgEv.secondaryId)
elif col['column'] == 'DateTimeCreated': elif col['column'] == 'DateTimeCreated':
tmpList.append(plgEv.created) tmpList.append(plgEv.created)
elif col['column'] == 'DateTimeChanged': elif col['column'] == 'DateTimeChanged':
tmpList.append(plgEv.changed) tmpList.append(plgEv.changed)
elif col['column'] == 'Watched_Value1': elif col['column'] == 'Watched_Value1':
tmpList.append(plgEv.watched1) tmpList.append(plgEv.watched1)
elif col['column'] == 'Watched_Value2': elif col['column'] == 'Watched_Value2':
tmpList.append(plgEv.watched2) tmpList.append(plgEv.watched2)
elif col['column'] == 'Watched_Value3': elif col['column'] == 'Watched_Value3':
tmpList.append(plgEv.watched3) tmpList.append(plgEv.watched3)
elif col['column'] == 'Watched_Value4': elif col['column'] == 'Watched_Value4':
tmpList.append(plgEv.watched4) tmpList.append(plgEv.watched4)
elif col['column'] == 'UserData': elif col['column'] == 'UserData':
tmpList.append(plgEv.userData) tmpList.append(plgEv.userData)
elif col['column'] == 'Extra': elif col['column'] == 'Extra':
tmpList.append(plgEv.extra) tmpList.append(plgEv.extra)
elif col['column'] == 'Status': elif col['column'] == 'Status':
tmpList.append(plgEv.status) tmpList.append(plgEv.status)
# Append the mapped values to the list 'sqlParams' as a tuple. # Append the mapped values to the list 'sqlParams' as a tuple.
sqlParams.append(tuple(tmpList)) sqlParams.append(tuple(tmpList))
# Generate the SQL INSERT query using the collected information. # Generate the SQL INSERT query using the collected information.
q = f'INSERT into {dbTable} ({columnsStr}) VALUES ({valuesStr})' q = f'INSERT into {dbTable} ({columnsStr}) VALUES ({valuesStr})'
# Log a debug message showing the generated SQL query for mapping. # Log a debug message showing the generated SQL query for mapping.
mylog('debug', ['[Plugins] SQL query for mapping: ', q]) mylog('debug', ['[Plugins] SQL query for mapping: ', q])
# Execute the SQL query using 'sql.executemany()' and the 'sqlParams' list of tuples. # Execute the SQL query using 'sql.executemany()' and the 'sqlParams' list of tuples.
# This will insert multiple rows into the database in one go. # This will insert multiple rows into the database in one go.
sql.executemany(q, sqlParams) sql.executemany(q, sqlParams)
db.commitDB() db.commitDB()

View File

@@ -14,6 +14,7 @@ import conf
from const import pialertPath, logPath, apiPath from const import pialertPath, logPath, apiPath
from helper import noti_struc, generate_mac_links, removeDuplicateNewLines, timeNowTZ, hide_email, updateState, get_file_content, write_file from helper import noti_struc, generate_mac_links, removeDuplicateNewLines, timeNowTZ, hide_email, updateState, get_file_content, write_file
from logger import logResult, mylog, print_log from logger import logResult, mylog, print_log
from plugin import execute_plugin
from publishers.email import (check_config as email_check_config, from publishers.email import (check_config as email_check_config,
@@ -468,15 +469,18 @@ def check_and_run_event(db):
event, param = ['',''] event, param = ['','']
if len(rows) > 0 and rows[0]['par_Value'] != 'finished': if len(rows) > 0 and rows[0]['par_Value'] != 'finished':
event = rows[0]['par_Value'].split('|')[0] keyValue = rows[0]['par_Value'].split('|')
param = rows[0]['par_Value'].split('|')[1]
if len(keyValue) == 2:
event = keyValue[0]
param = keyValue[1]
else: else:
return return
if event == 'test': if event == 'test':
handle_test(param) handle_test(param)
if event == 'run': if event == 'run':
handle_run(param) handle_run(param, db)
# clear event execution flag # clear event execution flag
sql.execute ("UPDATE Parameters SET par_Value='finished' WHERE par_ID='Front_Event'") sql.execute ("UPDATE Parameters SET par_Value='finished' WHERE par_ID='Front_Event'")
@@ -485,16 +489,20 @@ def check_and_run_event(db):
db.commitDB() db.commitDB()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def handle_run(runType): def handle_run(runType, db):
global last_network_scan
mylog('minimal', ['[', timeNowTZ(), '] START Run: ', runType]) mylog('minimal', ['[', timeNowTZ(), '] START Run: ', runType])
if runType == 'ENABLE_ARPSCAN': if runType == 'ENABLE_ARPSCAN':
last_network_scan = conf.time_started - datetime.timedelta(hours = 24) # run the plugin to run
for plugin in conf.plugins:
if plugin["unique_prefix"] == 'ARPSCAN':
execute_plugin(db, plugin)
mylog('minimal', ['[', timeNowTZ(), '] END Run: ', runType]) mylog('minimal', ['[', timeNowTZ(), '] END Run: ', runType])
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def handle_test(testType): def handle_test(testType):