🔃 Sync Hub v0.6.4 - Guid, SyncHubNodeName added

This commit is contained in:
jokob-sk
2024-06-06 23:00:05 +10:00
parent 73db99fe2f
commit 23703e4e22
22 changed files with 1435 additions and 1319 deletions

View File

@@ -4,7 +4,7 @@ import uuid
# Register NetAlertX modules
import conf
from const import applicationPath, logPath, apiPath, confFileName
from const import applicationPath, logPath, apiPath, confFileName, sql_generateGuid
from logger import logResult, mylog, print_log
from helper import timeNowTZ
@@ -53,17 +53,6 @@ class AppEvent_obj:
);
""")
# Generate a GUID
sql_generateGuid = '''
lower(
hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
substr(hex( randomblob(2)), 2) || '-' ||
substr('AB89', 1 + (abs(random()) % 4) , 1) ||
substr(hex(randomblob(2)), 2) || '-' ||
hex(randomblob(6))
)
'''
# -------------
# Device events

View File

@@ -47,4 +47,15 @@ sql_new_devices = """SELECT * FROM (
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"""
ON t1.dev_MAC = t2.dev_MAC_t2"""
sql_generateGuid = '''
lower(
hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-' || '4' ||
substr(hex( randomblob(2)), 2) || '-' ||
substr('AB89', 1 + (abs(random()) % 4) , 1) ||
substr(hex(randomblob(2)), 2) || '-' ||
hex(randomblob(6))
)
'''

View File

@@ -4,7 +4,7 @@ import sqlite3
import base64
# Register NetAlertX modules
from const import fullDbPath, sql_devices_stats, sql_devices_all
from const import fullDbPath, sql_devices_stats, sql_devices_all, sql_generateGuid
from logger import mylog
from helper import json_obj, initOrSetParam, row_to_json, timeNowTZ#, split_string #, updateState
@@ -150,6 +150,34 @@ class DB():
ALTER TABLE "Devices" ADD "dev_Icon" TEXT
""")
# dev_GUID column
dev_GUID_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Devices') WHERE name='dev_GUID'
""").fetchone()[0] == 0
if dev_GUID_missing :
mylog('verbose', ["[upgradeDB] Adding dev_GUID to the Devices table"])
self.sql.execute("""
ALTER TABLE "Devices" ADD "dev_GUID" TEXT
""")
# SQL query to update missing dev_GUID
self.sql.execute(f'''
UPDATE Devices
SET dev_GUID = {sql_generateGuid}
WHERE dev_GUID IS NULL
''')
# dev_SyncHubNodeName column
dev_SyncHubNodeName_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Devices') WHERE name='dev_SyncHubNodeName'
""").fetchone()[0] == 0
if dev_SyncHubNodeName_missing :
mylog('verbose', ["[upgradeDB] Adding dev_SyncHubNodeName to the Devices table"])
self.sql.execute("""
ALTER TABLE "Devices" ADD "dev_SyncHubNodeName" TEXT
""")
# -------------------------------------------------------------------------
# Settings table setup
@@ -324,6 +352,17 @@ class DB():
); """
self.sql.execute(sql_Plugins_Objects)
# syncHubNodeName column
plug_SyncHubNodeName_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Plugins_Objects') WHERE name='SyncHubNodeName'
""").fetchone()[0] == 0
if plug_SyncHubNodeName_missing :
mylog('verbose', ["[upgradeDB] Adding SyncHubNodeName to the Plugins_Objects table"])
self.sql.execute("""
ALTER TABLE "Plugins_Objects" ADD "SyncHubNodeName" TEXT
""")
# Plugin execution results
sql_Plugins_Events = """ CREATE TABLE IF NOT EXISTS Plugins_Events(
"Index" INTEGER,
@@ -344,6 +383,18 @@ class DB():
); """
self.sql.execute(sql_Plugins_Events)
# syncHubNodeName column
plug_SyncHubNodeName_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Plugins_Events') WHERE name='SyncHubNodeName'
""").fetchone()[0] == 0
if plug_SyncHubNodeName_missing :
mylog('verbose', ["[upgradeDB] Adding SyncHubNodeName to the Plugins_Events table"])
self.sql.execute("""
ALTER TABLE "Plugins_Events" ADD "SyncHubNodeName" TEXT
""")
# Plugin execution history
sql_Plugins_History = """ CREATE TABLE IF NOT EXISTS Plugins_History(
"Index" INTEGER,
@@ -364,6 +415,18 @@ class DB():
); """
self.sql.execute(sql_Plugins_History)
# syncHubNodeName column
plug_SyncHubNodeName_missing = self.sql.execute ("""
SELECT COUNT(*) AS CNTREC FROM pragma_table_info('Plugins_History') WHERE name='SyncHubNodeName'
""").fetchone()[0] == 0
if plug_SyncHubNodeName_missing :
mylog('verbose', ["[upgradeDB] Adding SyncHubNodeName to the Plugins_History table"])
self.sql.execute("""
ALTER TABLE "Plugins_History" ADD "SyncHubNodeName" TEXT
""")
# -------------------------------------------------------------------------
# Plugins_Language_Strings table setup
# -------------------------------------------------------------------------
@@ -389,14 +452,15 @@ class DB():
# indicates, if CurrentScan table is available
self.sql.execute("DROP TABLE IF EXISTS CurrentScan;")
self.sql.execute(""" CREATE TABLE CurrentScan (
self.sql.execute(""" CREATE TABLE IF NOT EXISTS CurrentScan (
cur_MAC STRING(50) NOT NULL COLLATE NOCASE,
cur_IP STRING(50) NOT NULL COLLATE NOCASE,
cur_Vendor STRING(250),
cur_ScanMethod STRING(10),
cur_Name STRING(250),
cur_LastQuery STRING(250),
cur_DateTime STRING(250)
cur_DateTime STRING(250),
cur_SyncHubNodeName STRING(50)
);
""")

View File

@@ -6,7 +6,7 @@ import os
import re
from helper import timeNowTZ, get_setting, get_setting_value, list_to_where, resolve_device_name_dig, resolve_device_name_pholus, get_device_name_nslookup, check_IP_format
from logger import mylog, print_log
from const import vendorsPath, vendorsPathNewest
from const import vendorsPath, vendorsPathNewest, sql_generateGuid
#-------------------------------------------------------------------------------
# Device object handling (WIP)
@@ -198,12 +198,12 @@ def create_new_devices (db):
# Bulk-inserting devices from the CurrentScan table as new devices in the table Devices ...
# ... with new device defaults and ignoring specidfied IPs and MACs)
sqlQuery = f"""INSERT OR IGNORE INTO Devices (dev_MAC, dev_name, dev_Vendor,
dev_LastIP, dev_FirstConnection, dev_LastConnection,
dev_LastIP, dev_FirstConnection, dev_LastConnection, dev_SyncHubNodeName, dev_GUID,
{newDevColumns})
SELECT cur_MAC,
CASE WHEN LENGTH(TRIM(cur_Name)) > 0 THEN cur_Name
ELSE '(unknown)' END,
cur_Vendor, cur_IP, ?, ?,
cur_Vendor, cur_IP, ?, ?, cur_SyncHubNodeName, {sql_generateGuid},
{newDevDefaults}
FROM CurrentScan
WHERE 1=1

View File

@@ -242,8 +242,13 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ):
# Check if the file exists
if os.path.exists(file_path):
tmp_SyncHubNodeName = 'null'
# Check if the file name contains "encoded"
if '.encoded.' in filename and encryption_key != '':
# store e.g. Node_1 from last_result.encoded.Node_1.1.log
tmp_SyncHubNodeName = filename.split('.')[2]
# Decrypt the entire file
with open(file_path, 'r+') as f:
@@ -255,7 +260,7 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ):
f.write(decrypted_data)
f.truncate()
# Rename the file
# Rename the file e.g. from last_result.encoded.Node_1.1.log to last_result.decoded.Node_1.1.log
new_filename = filename.replace('.encoded.', '.decoded.')
os.rename(file_path, os.path.join(file_dir, new_filename))
@@ -295,7 +300,8 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ):
'not-processed', # "Status" column (placeholder)
columns[7], # "Extra" value from columns list
'null', # Placeholder for "UserData" column
columns[8] # "ForeignKey" value from columns list
columns[8], # "ForeignKey" value from columns list
tmp_SyncHubNodeName # Sync Hub Node name
)
)
else:
@@ -339,7 +345,8 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ):
'not-processed', # "Status" column (placeholder)
row[7], # "Extra" row
'null', # Placeholder "UserData" column
row[8] # "ForeignKey" row
row[8], # "ForeignKey" row
'null' # Sync Hub Node name - Only supported with scripts
)
)
else:
@@ -396,7 +403,9 @@ def execute_plugin(db, all_plugins, plugin, pluginsState = plugins_state() ):
'not-processed', # "Status" column (placeholder)
row[7], # "Extra"
'null', # "UserData" column (null placeholder)
row[8])) # "ForeignKey"
row[8], # "ForeignKey"
'null' # Sync Hub Node name - Only supported with scripts
))
else:
mylog('none', ['[Plugins] Skipped invalid sql result'])
@@ -535,12 +544,12 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
for plugObj in pluginObjects:
# keep old createdTime time if the plugObj already was created before
createdTime = plugObj.changed if plugObj.status == 'new' else plugObj.created
# 13 values without Index
# 14 values without Index
values = (
plugObj.pluginPref, plugObj.primaryId, plugObj.secondaryId, createdTime,
plugObj.changed, plugObj.watched1, plugObj.watched2, plugObj.watched3,
plugObj.watched4, plugObj.status, plugObj.extra, plugObj.userData,
plugObj.foreignKey
plugObj.foreignKey, plugObj.syncHubNodeName
)
if plugObj.status == 'new':
@@ -573,8 +582,8 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
INSERT INTO Plugins_Objects
("Plugin", "Object_PrimaryID", "Object_SecondaryID", "DateTimeCreated",
"DateTimeChanged", "Watched_Value1", "Watched_Value2", "Watched_Value3",
"Watched_Value4", "Status", "Extra", "UserData", "ForeignKey")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"Watched_Value4", "Status", "Extra", "UserData", "ForeignKey", "SyncHubNodeName")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", objects_to_insert
)
@@ -585,7 +594,7 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
UPDATE Plugins_Objects
SET "Plugin" = ?, "Object_PrimaryID" = ?, "Object_SecondaryID" = ?, "DateTimeCreated" = ?,
"DateTimeChanged" = ?, "Watched_Value1" = ?, "Watched_Value2" = ?, "Watched_Value3" = ?,
"Watched_Value4" = ?, "Status" = ?, "Extra" = ?, "UserData" = ?, "ForeignKey" = ?
"Watched_Value4" = ?, "Status" = ?, "Extra" = ?, "UserData" = ?, "ForeignKey" = ?, "SyncHubNodeName" = ?
WHERE "Index" = ?
""", objects_to_update
)
@@ -598,8 +607,8 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
INSERT INTO Plugins_Events
("Plugin", "Object_PrimaryID", "Object_SecondaryID", "DateTimeCreated",
"DateTimeChanged", "Watched_Value1", "Watched_Value2", "Watched_Value3",
"Watched_Value4", "Status", "Extra", "UserData", "ForeignKey")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"Watched_Value4", "Status", "Extra", "UserData", "ForeignKey", "SyncHubNodeName")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", events_to_insert
)
@@ -611,8 +620,8 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
INSERT INTO Plugins_History
("Plugin", "Object_PrimaryID", "Object_SecondaryID", "DateTimeCreated",
"DateTimeChanged", "Watched_Value1", "Watched_Value2", "Watched_Value3",
"Watched_Value4", "Status", "Extra", "UserData", "ForeignKey")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"Watched_Value4", "Status", "Extra", "UserData", "ForeignKey", "SyncHubNodeName")
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", history_to_insert
)
@@ -689,6 +698,8 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
tmpList.append(plgEv.extra)
elif col['column'] == 'Status':
tmpList.append(plgEv.status)
elif col['column'] == 'SyncHubNodeName':
tmpList.append(plgEv.syncHubNodeName)
# Check if there's a default value specified for this column in the JSON.
if 'mapped_to_column_data' in col and 'value' in col['mapped_to_column_data']:
@@ -723,20 +734,21 @@ def process_plugin_events(db, plugin, pluginsState, plugEventsArr):
#-------------------------------------------------------------------------------
class plugin_object_class:
def __init__(self, plugin, objDbRow):
self.index = objDbRow[0]
self.pluginPref = objDbRow[1]
self.primaryId = objDbRow[2]
self.secondaryId = objDbRow[3]
self.created = objDbRow[4] # can be null
self.changed = objDbRow[5] # never null (data coming from plugin)
self.watched1 = objDbRow[6]
self.watched2 = objDbRow[7]
self.watched3 = objDbRow[8]
self.watched4 = objDbRow[9]
self.status = objDbRow[10]
self.extra = objDbRow[11]
self.userData = objDbRow[12]
self.foreignKey = objDbRow[13]
self.index = objDbRow[0]
self.pluginPref = objDbRow[1]
self.primaryId = objDbRow[2]
self.secondaryId = objDbRow[3]
self.created = objDbRow[4] # can be null
self.changed = objDbRow[5] # never null (data coming from plugin)
self.watched1 = objDbRow[6]
self.watched2 = objDbRow[7]
self.watched3 = objDbRow[8]
self.watched4 = objDbRow[9]
self.status = objDbRow[10]
self.extra = objDbRow[11]
self.userData = objDbRow[12]
self.foreignKey = objDbRow[13]
self.syncHubNodeName = objDbRow[14]
# Check if self.status is valid
if self.status not in ["exists", "watched-changed", "watched-not-changed", "new", "not-processed", "missing-in-last-scan"]: