removing DB opens/closes

This commit is contained in:
Jokob-sk
2023-01-01 12:49:11 +11:00
parent ec32fca3f9
commit b86cbb3a39
11 changed files with 114 additions and 187 deletions

View File

@@ -288,14 +288,13 @@ DHCP_ACTIVE = False
# Pholus settings # Pholus settings
# ---------------------- # ----------------------
PHOLUS_ACTIVE = True PHOLUS_ACTIVE = False
PHOLUS_TIMEOUT = 180 PHOLUS_TIMEOUT = 20
PHOLUS_FORCE = False PHOLUS_FORCE = False
PHOLUS_DAYS_DATA = 7 PHOLUS_DAYS_DATA = 0
PHOLUS_RUN = 'once'
PHOLUS_RUN = 'none' PHOLUS_RUN_TIMEOUT = 300
PHOLUS_RUN_SCHD = '0 4 * * *' PHOLUS_RUN_SCHD = '0 4 * * *'
PHOLUS_RUN_TIMEOUT = 600
#=============================================================================== #===============================================================================
@@ -324,7 +323,7 @@ def openDB ():
sql = sql_connection.cursor() sql = sql_connection.cursor()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def closeDB (): def commitDB ():
global sql_connection global sql_connection
global sql global sql
@@ -333,12 +332,12 @@ def closeDB ():
return return
# Log # Log
print_log ('Closing DB') print_log ('Commiting DB changes')
# Close DB # Close DB
sql_connection.commit() sql_connection.commit()
sql_connection.close() # sql_connection.close()
sql_connection = None # sql_connection = None
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# Import user values # Import user values
@@ -463,8 +462,6 @@ def importConfig ():
PHOLUS_DAYS_DATA = check_config_dict('PHOLUS_DAYS_DATA', PHOLUS_DAYS_DATA , config_dict) PHOLUS_DAYS_DATA = check_config_dict('PHOLUS_DAYS_DATA', PHOLUS_DAYS_DATA , config_dict)
openDB()
# Code_Name, Display_Name, Description, Type, Options, Value, Group # Code_Name, Display_Name, Description, Type, Options, Value, Group
settings = [ settings = [
@@ -553,7 +550,7 @@ def importConfig ():
# Used to determine the next import # Used to determine the next import
lastTimeImported = time.time() lastTimeImported = time.time()
closeDB() commitDB()
# Update scheduler # Update scheduler
global schedule, tz, last_next_pholus_schedule, last_next_pholus_schedule_used global schedule, tz, last_next_pholus_schedule, last_next_pholus_schedule_used
@@ -603,6 +600,10 @@ def main ():
sql_connection = None sql_connection = None
sql = None sql = None
# Open DB once and keep open
# Opening / closing DB frequently actually casues more issues
openDB() # main
# Upgrade DB if needed # Upgrade DB if needed
upgradeDB() upgradeDB()
@@ -674,8 +675,8 @@ def main ():
cycle = 'cleanup' cycle = 'cleanup'
cleanup_database() cleanup_database()
# Close SQL # Commit SQL
closeDB() commitDB()
# Final message # Final message
if cycle != "": if cycle != "":
@@ -820,15 +821,13 @@ def get_previous_internet_IP ():
previous_IP = '0.0.0.0' previous_IP = '0.0.0.0'
# get previous internet IP stored in DB # get previous internet IP stored in DB
openDB()
sql.execute ("SELECT dev_LastIP FROM Devices WHERE dev_MAC = 'Internet' ") sql.execute ("SELECT dev_LastIP FROM Devices WHERE dev_MAC = 'Internet' ")
result = sql.fetchone() result = sql.fetchone()
if len(result) > 0 and type(result) is not None: if len(result) > 0 and type(result) is not None:
previous_IP = result[0] previous_IP = result[0]
closeDB() commitDB()
# return previous IP # return previous IP
return previous_IP return previous_IP
@@ -840,8 +839,6 @@ def save_new_internet_IP (pNewIP):
'['+str(startTime) +']\t'+ pNewIP +'\n') '['+str(startTime) +']\t'+ pNewIP +'\n')
prevIp = get_previous_internet_IP() prevIp = get_previous_internet_IP()
openDB()
# Save event # Save event
sql.execute ("""INSERT INTO Events (eve_MAC, eve_IP, eve_DateTime, sql.execute ("""INSERT INTO Events (eve_MAC, eve_IP, eve_DateTime,
eve_EventType, eve_AdditionalInfo, eve_EventType, eve_AdditionalInfo,
@@ -856,8 +853,7 @@ def save_new_internet_IP (pNewIP):
(pNewIP,) ) (pNewIP,) )
# commit changes # commit changes
sql_connection.commit() commitDB()
closeDB()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def check_IP_format (pIP): def check_IP_format (pIP):
@@ -882,26 +878,35 @@ def cleanup_database ():
updateState("Upkeep: Clean DB") updateState("Upkeep: Clean DB")
file_print('[', startTime, '] Upkeep Database:' ) file_print('[', startTime, '] Upkeep Database:' )
openDB()
# Cleanup Online History # Cleanup Online History
file_print(' Upkeep Online_History') file_print(' Online_History: Delete all older than 1 day')
sql.execute ("DELETE FROM Online_History WHERE Scan_Date <= date('now', '-1 day')") sql.execute ("DELETE FROM Online_History WHERE Scan_Date <= date('now', '-1 day')")
file_print(' Optimize Database') file_print(' Optimize Database')
# Cleanup Events # Cleanup Events
file_print(' Upkeep Events, delete all older than '+str(DAYS_TO_KEEP_EVENTS)+' days') file_print(' Events: Delete all older than '+str(DAYS_TO_KEEP_EVENTS)+' days')
sql.execute ("DELETE FROM Events WHERE eve_DateTime <= date('now', '-"+str(DAYS_TO_KEEP_EVENTS)+" day')") sql.execute ("DELETE FROM Events WHERE eve_DateTime <= date('now', '-"+str(DAYS_TO_KEEP_EVENTS)+" day')")
# Cleanup Pholus_Scan # Cleanup Pholus_Scan
file_print(' Upkeep Pholus_Scan, delete all older than ' + str(PHOLUS_DAYS_DATA) + ' days') if PHOLUS_DAYS_DATA != 0:
file_print(' Pholus_Scan: Delete all older than ' + str(PHOLUS_DAYS_DATA) + ' days')
sql.execute ("DELETE FROM Pholus_Scan WHERE Time <= date('now', '-"+ str(PHOLUS_DAYS_DATA) +" day')") # improvement possibility: keep at least N per mac sql.execute ("DELETE FROM Pholus_Scan WHERE Time <= date('now', '-"+ str(PHOLUS_DAYS_DATA) +" day')") # improvement possibility: keep at least N per mac
# De-Dupe (de-duplicate - remove duplicate entries) from the Pholus_Scan table
file_print(' Pholus_Scan: Delete all duplicates')
sql.execute ("""DELETE FROM Pholus_Scan
WHERE rowid > (
SELECT MIN(rowid) FROM Pholus_Scan p2
WHERE Pholus_Scan.MAC = p2.MAC
AND Pholus_Scan.Value = p2.Value
AND Pholus_Scan.Record_Type = p2.Record_Type
);""")
# Shrink DB # Shrink DB
file_print(' Shrink Database') file_print(' Shrink Database')
sql.execute ("VACUUM;") sql.execute ("VACUUM;")
closeDB() commitDB()
#=============================================================================== #===============================================================================
# UPDATE DEVICE MAC VENDORS # UPDATE DEVICE MAC VENDORS
@@ -934,7 +939,6 @@ def update_devices_MAC_vendors (pArg = ''):
# All devices loop # All devices loop
file_print(' Searching devices vendor') file_print(' Searching devices vendor')
openDB()
for device in sql.execute ("SELECT * FROM Devices") : for device in sql.execute ("SELECT * FROM Devices") :
# Search vendor in HW Vendors DB # Search vendor in HW Vendors DB
vendor = query_MAC_vendor (device['dev_MAC']) vendor = query_MAC_vendor (device['dev_MAC'])
@@ -959,8 +963,8 @@ def update_devices_MAC_vendors (pArg = ''):
# DEBUG - print number of rows updated # DEBUG - print number of rows updated
# file_print(sql.rowcount) # file_print(sql.rowcount)
# Close DB # Commit DB
closeDB() commitDB()
if len(recordsToUpdate) > 0: if len(recordsToUpdate) > 0:
return True return True
@@ -1058,6 +1062,8 @@ def scan_network ():
file_print(' Exiting...\n') file_print(' Exiting...\n')
return False return False
commitDB()
# ScanCycle data # ScanCycle data
cycle_interval = scanCycle_data['cic_EveryXmin'] cycle_interval = scanCycle_data['cic_EveryXmin']
@@ -1072,20 +1078,17 @@ def scan_network ():
# Pi-hole method # Pi-hole method
if PIHOLE_ACTIVE : if PIHOLE_ACTIVE :
file_print(' Pi-hole start') file_print(' Pi-hole start')
openDB()
copy_pihole_network() copy_pihole_network()
closeDB() commitDB()
# DHCP Leases method # DHCP Leases method
if DHCP_ACTIVE : if DHCP_ACTIVE :
file_print(' DHCP Leases start') file_print(' DHCP Leases start')
openDB()
read_DHCP_leases () read_DHCP_leases ()
closeDB() commitDB()
# Load current scan data # Load current scan data
file_print(' Processing scan results') file_print(' Processing scan results')
openDB()
save_scanned_devices (arpscan_devices, cycle_interval) save_scanned_devices (arpscan_devices, cycle_interval)
# Print stats # Print stats
@@ -1128,28 +1131,18 @@ def scan_network ():
skip_repeated_notifications () skip_repeated_notifications ()
# Commit changes # Commit changes
openDB() commitDB()
sql_connection.commit()
closeDB()
return reporting return reporting
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def query_ScanCycle_Data (pOpenCloseDB = False): def query_ScanCycle_Data (pOpenCloseDB = False):
# Check if is necesary open DB
if pOpenCloseDB :
openDB()
# Query Data # Query Data
sql.execute ("""SELECT cic_arpscanCycles, cic_EveryXmin sql.execute ("""SELECT cic_arpscanCycles, cic_EveryXmin
FROM ScanCycles FROM ScanCycles
WHERE cic_ID = ? """, (cycle,)) WHERE cic_ID = ? """, (cycle,))
sqlRow = sql.fetchone() sqlRow = sql.fetchone()
# Check if is necesary close DB
if pOpenCloseDB :
closeDB()
# Return Row # Return Row
return sqlRow return sqlRow
@@ -1264,8 +1257,6 @@ def read_DHCP_leases ():
def save_scanned_devices (p_arpscan_devices, p_cycle_interval): def save_scanned_devices (p_arpscan_devices, p_cycle_interval):
cycle = 1 # always 1, only one cycle supported cycle = 1 # always 1, only one cycle supported
openDB()
# Delete previous scan data # Delete previous scan data
sql.execute ("DELETE FROM CurrentScan WHERE cur_ScanCycle = ?", sql.execute ("DELETE FROM CurrentScan WHERE cur_ScanCycle = ?",
(cycle,)) (cycle,))
@@ -1685,10 +1676,9 @@ def update_devices_names ():
# Devices without name # Devices without name
file_print(' Trying to resolve devices without name') file_print(' Trying to resolve devices without name')
# BUGFIX #97 - Updating name of Devices w/o IP # BUGFIX #97 - Updating name of Devices w/o IP
openDB()
sql.execute ("SELECT * FROM Devices WHERE dev_Name IN ('(unknown)','', '(name not found)') AND dev_LastIP <> '-'") sql.execute ("SELECT * FROM Devices WHERE dev_Name IN ('(unknown)','', '(name not found)') AND dev_LastIP <> '-'")
unknownDevices = sql.fetchall() unknownDevices = sql.fetchall()
closeDB() commitDB()
# perform Pholus scan if (unknown) devices found # perform Pholus scan if (unknown) devices found
if PHOLUS_ACTIVE and (len(unknownDevices) > 0 or PHOLUS_FORCE): if PHOLUS_ACTIVE and (len(unknownDevices) > 0 or PHOLUS_FORCE):
@@ -1696,10 +1686,9 @@ def update_devices_names ():
# get names from Pholus scan # get names from Pholus scan
# sql.execute ('SELECT * FROM Pholus_Scan where "MAC" in (select "dev_MAC" from Devices where "dev_Name" IN ("(unknown)","")) and "Record_Type"="Answer"') # sql.execute ('SELECT * FROM Pholus_Scan where "MAC" in (select "dev_MAC" from Devices where "dev_Name" IN ("(unknown)","")) and "Record_Type"="Answer"')
openDB()
sql.execute ('SELECT * FROM Pholus_Scan where "Record_Type"="Answer"') sql.execute ('SELECT * FROM Pholus_Scan where "Record_Type"="Answer"')
pholusResults = list(sql.fetchall()) pholusResults = list(sql.fetchall())
closeDB() commitDB()
# Number of entries from previous Pholus scans # Number of entries from previous Pholus scans
file_print(" Pholus entries from prev scans: ", len(pholusResults)) file_print(" Pholus entries from prev scans: ", len(pholusResults))
@@ -1732,12 +1721,12 @@ def update_devices_names ():
file_print(" Names Found (DiG/Pholus): ", len(recordsToUpdate), " (",foundDig,"/",foundPholus ,")" ) file_print(" Names Found (DiG/Pholus): ", len(recordsToUpdate), " (",foundDig,"/",foundPholus ,")" )
file_print(" Names Not Found : ", len(recordsNotFound) ) file_print(" Names Not Found : ", len(recordsNotFound) )
openDB()
# update not found devices with (name not found) # update not found devices with (name not found)
sql.executemany ("UPDATE Devices SET dev_Name = ? WHERE dev_MAC = ? ", recordsNotFound ) sql.executemany ("UPDATE Devices SET dev_Name = ? WHERE dev_MAC = ? ", recordsNotFound )
# update names of devices which we were bale to resolve # update names of devices which we were bale to resolve
sql.executemany ("UPDATE Devices SET dev_Name = ? WHERE dev_MAC = ? ", recordsToUpdate ) sql.executemany ("UPDATE Devices SET dev_Name = ? WHERE dev_MAC = ? ", recordsToUpdate )
closeDB() commitDB()
# DEBUG - print number of rows updated # DEBUG - print number of rows updated
# file_print(sql.rowcount) # file_print(sql.rowcount)
@@ -1780,6 +1769,7 @@ def performPholusScan (timeout):
for line in output.split("\n"): for line in output.split("\n"):
append_line_to_file (logPath + '/pialert_pholus.log', line +'\n') append_line_to_file (logPath + '/pialert_pholus.log', line +'\n')
# build SQL query parameters to insert into the DB
params = [] params = []
for line in output.split("\n"): for line in output.split("\n"):
@@ -1788,9 +1778,8 @@ def performPholusScan (timeout):
params.append(( interface + " " + mask, timeNow() , columns[0].replace(" ", ""), columns[1].replace(" ", ""), columns[2].replace(" ", ""), columns[3], '')) params.append(( interface + " " + mask, timeNow() , columns[0].replace(" ", ""), columns[1].replace(" ", ""), columns[2].replace(" ", ""), columns[3], ''))
if len(params) > 0: if len(params) > 0:
openDB ()
sql.executemany ("""INSERT INTO Pholus_Scan ("Info", "Time", "MAC", "IP_v4_or_v6", "Record_Type", "Value", "Extra") VALUES (?, ?, ?, ?, ?, ?, ?)""", params) sql.executemany ("""INSERT INTO Pholus_Scan ("Info", "Time", "MAC", "IP_v4_or_v6", "Record_Type", "Value", "Extra") VALUES (?, ?, ?, ?, ?, ?, ?)""", params)
closeDB () commitDB ()
else: else:
file_print('[', timeNow(), '] Scan: Pholus FAIL - check logs') file_print('[', timeNow(), '] Scan: Pholus FAIL - check logs')
@@ -1950,7 +1939,6 @@ def resolve_device_name_dig (pMAC, pIP):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def void_ghost_disconnections (): def void_ghost_disconnections ():
openDB()
# Void connect ghost events (disconnect event exists in last X min.) # Void connect ghost events (disconnect event exists in last X min.)
print_log ('Void - 1 Connect ghost events') print_log ('Void - 1 Connect ghost events')
sql.execute ("""UPDATE Events SET eve_PairEventRowid = Null, sql.execute ("""UPDATE Events SET eve_PairEventRowid = Null,
@@ -2009,7 +1997,7 @@ def void_ghost_disconnections ():
) """, ) """,
(cycle, startTime) ) (cycle, startTime) )
print_log ('Void end') print_log ('Void end')
closeDB() commitDB()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def pair_sessions_events (): def pair_sessions_events ():
@@ -2020,7 +2008,6 @@ def pair_sessions_events ():
# WHERE eve_EventType IN ('New Device', 'Connected') # WHERE eve_EventType IN ('New Device', 'Connected')
# """ ) # """ )
openDB()
# Pair Connection / New Device events # Pair Connection / New Device events
print_log ('Pair session - 1 Connections / New Devices') print_log ('Pair session - 1 Connections / New Devices')
@@ -2049,12 +2036,11 @@ def pair_sessions_events ():
""" ) """ )
print_log ('Pair session end') print_log ('Pair session end')
closeDB() commitDB()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def create_sessions_snapshot (): def create_sessions_snapshot ():
openDB()
# Clean sessions snapshot # Clean sessions snapshot
print_log ('Sessions Snapshot - 1 Clean') print_log ('Sessions Snapshot - 1 Clean')
sql.execute ("DELETE FROM SESSIONS" ) sql.execute ("DELETE FROM SESSIONS" )
@@ -2078,15 +2064,13 @@ def create_sessions_snapshot ():
# SELECT * FROM Convert_Events_to_Sessions_Phase2""" ) # SELECT * FROM Convert_Events_to_Sessions_Phase2""" )
print_log ('Sessions end') print_log ('Sessions end')
closeDB() commitDB()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def skip_repeated_notifications (): def skip_repeated_notifications ():
openDB()
# Skip repeated notifications # Skip repeated notifications
# due strfime : Overflow --> use "strftime / 60" # due strfime : Overflow --> use "strftime / 60"
print_log ('Skip Repeated') print_log ('Skip Repeated')
@@ -2103,7 +2087,7 @@ def skip_repeated_notifications ():
""" ) """ )
print_log ('Skip Repeated end') print_log ('Skip Repeated end')
closeDB() commitDB()
#=============================================================================== #===============================================================================
@@ -2120,7 +2104,6 @@ def email_reporting ():
# Reporting section # Reporting section
file_print(' Check if something to report') file_print(' Check if something to report')
openDB()
# prepare variables for JSON construction # prepare variables for JSON construction
json_internet = [] json_internet = []
@@ -2346,8 +2329,6 @@ def email_reporting ():
else : else :
file_print(' No changes to report') file_print(' No changes to report')
openDB()
# Clean Pending Alert Events # Clean Pending Alert Events
sql.execute ("""UPDATE Devices SET dev_LastNotification = ? sql.execute ("""UPDATE Devices SET dev_LastNotification = ?
WHERE dev_MAC IN (SELECT eve_MAC FROM Events WHERE dev_MAC IN (SELECT eve_MAC FROM Events
@@ -2360,8 +2341,7 @@ def email_reporting ():
file_print(' Notifications: ', sql.rowcount) file_print(' Notifications: ', sql.rowcount)
# Commit changes # Commit changes
sql_connection.commit() commitDB()
closeDB()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def check_config(service): def check_config(service):
@@ -2811,8 +2791,6 @@ def mqtt_start():
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def upgradeDB (): def upgradeDB ():
openDB()
# indicates, if Online_History table is available # indicates, if Online_History table is available
onlineHistoryAvailable = sql.execute(""" onlineHistoryAvailable = sql.execute("""
SELECT name FROM sqlite_master WHERE type='table' SELECT name FROM sqlite_master WHERE type='table'
@@ -2949,16 +2927,15 @@ def upgradeDB ():
""") """)
# don't hog DB access # don't hog DB access
closeDB () commitDB ()
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def updateState(newState): def updateState(newState):
openDB()
sql.execute ("UPDATE Parameters SET par_Value='"+ newState +"' WHERE par_ID='Back_App_State'") sql.execute ("UPDATE Parameters SET par_Value='"+ newState +"' WHERE par_ID='Back_App_State'")
# don't hog DB access # don't hog DB access
closeDB () commitDB ()
#=============================================================================== #===============================================================================
@@ -3084,8 +3061,6 @@ def to_text(_json):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def get_device_stats(): def get_device_stats():
openDB()
# columns = ["online","down","all","archived","new","unknown"] # columns = ["online","down","all","archived","new","unknown"]
sql.execute(""" sql.execute("""
SELECT Online_Devices as online, Down_Devices as down, All_Devices as 'all', Archived_Devices as archived, (select count(*) from Devices a where dev_NewDevice = 1 ) as new, (select count(*) from Devices a where dev_Name = '(unknown)' or dev_Name = '(name not found)' ) as unknown from Online_History order by Scan_Date desc limit 1 SELECT Online_Devices as online, Down_Devices as down, All_Devices as 'all', Archived_Devices as archived, (select count(*) from Devices a where dev_NewDevice = 1 ) as new, (select count(*) from Devices a where dev_Name = '(unknown)' or dev_Name = '(name not found)' ) as unknown from Online_History order by Scan_Date desc limit 1
@@ -3093,20 +3068,19 @@ def get_device_stats():
row = sql.fetchone() row = sql.fetchone()
closeDB() commitDB()
return row return row
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def get_all_devices(): def get_all_devices():
openDB()
sql.execute(""" sql.execute("""
select dev_MAC, dev_Name, dev_DeviceType, dev_Vendor, dev_Group, dev_FirstConnection, dev_LastConnection, dev_LastIP, dev_StaticIP, dev_PresentLastScan, dev_LastNotification, dev_NewDevice, dev_Network_Node_MAC_ADDR from Devices select dev_MAC, dev_Name, dev_DeviceType, dev_Vendor, dev_Group, dev_FirstConnection, dev_LastConnection, dev_LastIP, dev_StaticIP, dev_PresentLastScan, dev_LastNotification, dev_NewDevice, dev_Network_Node_MAC_ADDR from Devices
""") """)
row = sql.fetchall() row = sql.fetchall()
closeDB() commitDB()
return row return row

View File

@@ -6,9 +6,9 @@ services:
network_mode: "host" network_mode: "host"
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- ${APP_DATA_LOCATION}/pialert/config:/home/pi/pialert/config - ${APP_DATA_LOCATION}/pialert/config2:/home/pi/pialert/config
# - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db # - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db
- ${APP_DATA_LOCATION}/pialert/db:/home/pi/pialert/db - ${APP_DATA_LOCATION}/pialert/db2:/home/pi/pialert/db
# (optional) map an empty file with the name 'setting_darkmode' if you want to force the dark mode on container rebuilt # (optional) map an empty file with the name 'setting_darkmode' if you want to force the dark mode on container rebuilt
- ${APP_DATA_LOCATION}/pialert/db/setting_darkmode:/home/pi/pialert/db/setting_darkmode - ${APP_DATA_LOCATION}/pialert/db/setting_darkmode:/home/pi/pialert/db/setting_darkmode
# (optional) useful for debugging if you have issues setting up the container # (optional) useful for debugging if you have issues setting up the container

View File

@@ -595,6 +595,7 @@ if ($_REQUEST['mac'] == 'Internet') {
</tr> </tr>
</thead> </thead>
<!-- Comment out tbody when trying to implement better table with datatables here --> <!-- Comment out tbody when trying to implement better table with datatables here -->
<!-- IDEA: Show unmatched pholus entries? -->
<tbody id="tablePholusBody"> <tbody id="tablePholusBody">
<tr id="tablePholusPlc" class="text-center"><td colspan='7'><span>Nothing sniffed out with Polus for this device.</span></td></tr> <tr id="tablePholusPlc" class="text-center"><td colspan='7'><span>Nothing sniffed out with Polus for this device.</span></td></tr>
</tbody> </tbody>

View File

@@ -10,11 +10,7 @@
define('circle_online', '<div class="badge bg-green text-white" style="width: 10px; height: 10px; padding:2px; margin-top: -25px;">&nbsp;</div>'); define('circle_online', '<div class="badge bg-green text-white" style="width: 10px; height: 10px; padding:2px; margin-top: -25px;">&nbsp;</div>');
define('circle_offline', '<div class="badge bg-red text-white" style="width: 10px; height: 10px; padding:2px; margin-top: -25px;">&nbsp;</div>'); define('circle_offline', '<div class="badge bg-red text-white" style="width: 10px; height: 10px; padding:2px; margin-top: -25px;">&nbsp;</div>');
$DBFILE = '../db/pialert.db';
$NETWORKTYPES = getNetworkTypes(); $NETWORKTYPES = getNetworkTypes();
OpenDB();
?> ?>
<!-- Page ------------------------------------------------------------------ --> <!-- Page ------------------------------------------------------------------ -->
@@ -34,7 +30,6 @@
// Create top-level node (network devices) tabs // Create top-level node (network devices) tabs
function createDeviceTabs($node_mac, $node_name, $node_status, $node_type, $node_ports_count, $activetab) { function createDeviceTabs($node_mac, $node_name, $node_status, $node_type, $node_ports_count, $activetab) {
global $pia_lang; //language strings
// prepare string with port number in brackets if available // prepare string with port number in brackets if available
$str_port = ""; $str_port = "";
@@ -65,7 +60,6 @@
// Create pane content (displayed inside of the tabs) // Create pane content (displayed inside of the tabs)
function createPane($node_mac, $node_name, $node_status, $node_type, $node_ports_count, $node_parent_mac, $activetab){ function createPane($node_mac, $node_name, $node_status, $node_type, $node_ports_count, $node_parent_mac, $activetab){
global $pia_lang; //language strings
// online/offline status circle (red/green) // online/offline status circle (red/green)
$node_badge = ""; $node_badge = "";
@@ -325,7 +319,7 @@
} }
$db->close(); commitDB ();
?> ?>
<!-- /.tab-pane --> <!-- /.tab-pane -->
@@ -334,8 +328,6 @@
<!-- Unassigned devices --> <!-- Unassigned devices -->
<?php <?php
global $pia_lang; //language strings
OpenDB();
// Get all Unassigned / unconnected nodes // Get all Unassigned / unconnected nodes
$func_sql = 'SELECT dev_MAC as mac, $func_sql = 'SELECT dev_MAC as mac,
@@ -412,7 +404,7 @@
echo $str_table_header.$str_table_rows.$str_table_close; echo $str_table_header.$str_table_rows.$str_table_close;
} }
$db->close(); CommitDB ();
?> ?>
<!-- /.content --> <!-- /.content -->

View File

@@ -8,43 +8,11 @@
# Puche 2021 / 2022+ jokob jokob@duck.com GNU GPLv3 # Puche 2021 / 2022+ jokob jokob@duck.com GNU GPLv3
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// ## TimeZone processing require '/home/pi/pialert/front/php/templates/timezone.php';
$configFolderPath = "/home/pi/pialert/config/";
$config_file = "pialert.conf";
$logFolderPath = "/home/pi/pialert/front/log/";
$log_file = "pialert_front.log";
$fullConfPath = $configFolderPath.$config_file;
$config_file_lines = file($fullConfPath);
$config_file_lines_timezone = array_values(preg_grep('/^TIMEZONE\s.*/', $config_file_lines));
$timeZone = "";
foreach ($config_file_lines as $line)
{
if( preg_match('/TIMEZONE(.*?)/', $line, $match) == 1 )
{
if (preg_match('/\'(.*?)\'/', $line, $match) == 1) {
$timeZone = $match[1];
}
}
}
if($timeZone == "")
{
$timeZone = "Europe/Berlin";
}
date_default_timezone_set($timeZone);
$date = new DateTime("now", new DateTimeZone($timeZone) );
$timestamp = $date->format('Y-m-d_H-i-s');
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DB File Path // DB File Path
$DBFILE = '../../../db/pialert.db'; $DBFILE = '/home/pi/pialert/db/pialert.db';
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Connect DB // Connect DB
@@ -102,4 +70,15 @@ function OpenDB (...$DBPath) {
$db->exec('PRAGMA journal_mode = wal;'); $db->exec('PRAGMA journal_mode = wal;');
} }
function CommitDB () {
global $db;
// $db->commit();
}
// # Open DB once and keep open
// # Opening / closing DB frequently actually casues more issues
OpenDB (); // main
?> ?>

View File

@@ -20,9 +20,6 @@
// Set maximum execution time to 15 seconds // Set maximum execution time to 15 seconds
ini_set ('max_execution_time','30'); ini_set ('max_execution_time','30');
// Open DB
OpenDB();
// Action functions // Action functions
if (isset ($_REQUEST['action']) && !empty ($_REQUEST['action'])) { if (isset ($_REQUEST['action']) && !empty ($_REQUEST['action'])) {
$action = $_REQUEST['action']; $action = $_REQUEST['action'];
@@ -64,6 +61,7 @@
} }
} }
CommitDB();
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Query Device Data // Query Device Data
@@ -538,7 +536,7 @@ function ImportCSV() {
} }
$db->close(); CommitDB();
} }
@@ -939,8 +937,6 @@ function getPholus() {
'Extra' => $row['Extra']); 'Extra' => $row['Extra']);
} }
$db->close();
if(count($tableData) == 0) if(count($tableData) == 0)
{ {
echo "false"; echo "false";

View File

@@ -19,9 +19,6 @@
// Set maximum execution time to 1 minute // Set maximum execution time to 1 minute
ini_set ('max_execution_time','60'); ini_set ('max_execution_time','60');
// Open DB
OpenDB();
// Action functions // Action functions
if (isset ($_REQUEST['action']) && !empty ($_REQUEST['action'])) { if (isset ($_REQUEST['action']) && !empty ($_REQUEST['action'])) {
$action = $_REQUEST['action']; $action = $_REQUEST['action'];

View File

@@ -49,9 +49,6 @@ function getParameter() {
// query the database if no cache entry found or requesting live data for the Back_App_State in the header // query the database if no cache entry found or requesting live data for the Back_App_State in the header
if($parameter == "Back_App_State" || $value == "" ) if($parameter == "Back_App_State" || $value == "" )
{ {
// Open DB
OpenDB();
global $db; global $db;
$sql = 'SELECT par_Value FROM Parameters $sql = 'SELECT par_Value FROM Parameters
@@ -61,8 +58,8 @@ function getParameter() {
$row = $result -> fetchArray (SQLITE3_NUM); $row = $result -> fetchArray (SQLITE3_NUM);
$value = $row[0]; $value = $row[0];
// Close DB // Commit DB
$db->close(); CommitDB();
// update cookie cache // update cookie cache
setCache($parameter, $value); setCache($parameter, $value);
@@ -80,9 +77,6 @@ function setParameter() {
$parameter = $_REQUEST['parameter']; $parameter = $_REQUEST['parameter'];
$value = $_REQUEST['value']; $value = $_REQUEST['value'];
// Open DB
OpenDB();
global $db; global $db;
// Update value // Update value
@@ -109,8 +103,8 @@ function setParameter() {
} }
} }
// Close DB // Commit DB
$db->close(); CommitDB();
// update cookie cache // update cookie cache
setCache($parameter, $value); setCache($parameter, $value);

View File

@@ -1,8 +1,6 @@
<?php <?php
require 'php/server/db.php'; require 'php/server/db.php';
$DBFILE = '../db/pialert.db';
OpenDB();
$Pia_Graph_Device_Time = array(); $Pia_Graph_Device_Time = array();
$Pia_Graph_Device_All = array(); $Pia_Graph_Device_All = array();
@@ -28,6 +26,6 @@ function pia_graph_devices_data($Pia_Graph_Array) {
echo ","; echo ",";
} }
} }
$db->close();
?> ?>

View File

@@ -551,20 +551,20 @@ the arp-scan will take hours to complete instead of seconds.
// Pholus // Pholus
'PHOLUS_ACTIVE_name' => 'Cycle run', 'PHOLUS_ACTIVE_name' => 'Cycle run',
'PHOLUS_ACTIVE_description' => 'If enabled will execute during every network scan cycle until there are no <code>(unknown)</code> or <code>(name not found)</code> devices. <a href="https://github.com/jokob-sk/Pi.Alert/tree/main/pholus" target="_blank" >Pholus</a> is a sniffing tool to discover additional information about the devices on the network, including the device name. Please be aware it can spam the network with unnecessary traffic. Depends on the <a href="#SCAN_SUBNETS"><code>SCAN_SUBNETS</code> setting</a>.', 'PHOLUS_ACTIVE_description' => '<a href="https://github.com/jokob-sk/Pi.Alert/tree/main/pholus" target="_blank" >Pholus</a> is a sniffing tool to discover additional information about the devices on the network, including the device name. If enabled this will execute the scan before every network scan cycle until there are no <code>(unknown)</code> or <code>(name not found)</code> devices. Please be aware it can spam the network with unnecessary traffic. Depends on the <a href="#SCAN_SUBNETS"><code>SCAN_SUBNETS</code> setting</a>. For a scheduled or one-off scan, check the <a href="#PHOLUS_RUN"><code>PHOLUS_RUN</code> setting</a>.',
'PHOLUS_TIMEOUT_name' => 'Cycle run timeout', 'PHOLUS_TIMEOUT_name' => 'Cycle run timeout',
'PHOLUS_TIMEOUT_description' => 'How long in seconds should Pholus be sniffing on each interface if above condition is fulfilled. The longer you leave it on, the more likely devices would broadcast more info. This timeout adds to the time it takes to perform an arp-scan on your network.', 'PHOLUS_TIMEOUT_description' => 'How long in seconds should Pholus be sniffing on each interface if above condition is fulfilled. The longer you leave it on, the more likely devices would broadcast more info. This timeout adds to the time it takes to perform an arp-scan on your network.',
'PHOLUS_FORCE_name' => 'Cycle force scan', 'PHOLUS_FORCE_name' => 'Cycle force scan',
'PHOLUS_FORCE_description' => 'Force scan every network scan, even if there are no <code>(unknown)</code> or <code>(name not found)</code> devices. Be careful enabling this as the sniffing can easily flood your network.', 'PHOLUS_FORCE_description' => 'Force scan every network scan, even if there are no <code>(unknown)</code> or <code>(name not found)</code> devices. Be careful enabling this as the sniffing can easily flood your network.',
'PHOLUS_DAYS_DATA_name' => 'Data retention',
'PHOLUS_DAYS_DATA_description' => 'How many days of Pholus scan entries should be kept (globally, not device specific!). The <a href="/maintenance.php#tab_Logging">pialert_pholus.log</a> file is not touched.',
'PHOLUS_RUN_name' => 'Scheduled run', 'PHOLUS_RUN_name' => 'Scheduled run',
'PHOLUS_RUN_description' => 'Enable a regular Pholus scan / sniff on your network. The scheduling settings can be found below.', 'PHOLUS_RUN_description' => 'Enable a regular Pholus scan / sniff on your network. The scheduling settings can be found below. If you select <code>once</code> Pholus is run only once on start for the time specified in <a href="#PHOLUS_RUN_TIMEOUT"><code>PHOLUS_RUN_TIMEOUT</code> setting</a>.',
'PHOLUS_RUN_TIMEOUT_name' => 'Scheduled run timeout', 'PHOLUS_RUN_TIMEOUT_name' => 'Scheduled run timeout',
'PHOLUS_RUN_TIMEOUT_description' => 'The timeout in seconds for the scheduled Pholus scan. Same notes regarding the duration apply as on the <a href="#PHOLUS_TIMEOUT"><code>PHOLUS_TIMEOUT</code> setting</a>. A scheduled scan doesn\'t check if there are <code>(unknown)</code> devices, the scan is executed either way.', 'PHOLUS_RUN_TIMEOUT_description' => 'The timeout in seconds for the scheduled Pholus scan. Same notes regarding the duration apply as on the <a href="#PHOLUS_TIMEOUT"><code>PHOLUS_TIMEOUT</code> setting</a>. A scheduled scan doesn\'t check if there are <code>(unknown)</code> or <code>(name not found)</code> devices, the scan is executed either way.',
'PHOLUS_RUN_SCHD_name' => 'Schedule', 'PHOLUS_RUN_SCHD_name' => 'Schedule',
'PHOLUS_RUN_SCHD_description' => 'Schedule in cron format. Make sure you enter the schedule in the correct format 'PHOLUS_RUN_SCHD_description' => 'Make sure you enter the schedule in the correct cron-like format
(e.g. validate at <a href="https://crontab.guru/" target="_blank">crontab.guru</a>). Will be run NEXT time the time passes. For example <code>0 4 * * *</code> will run the scan after 4 am in the <a href="#TIMEZONE"><code>TIMEZONE</code> you set above</a>.', (e.g. validate at <a href="https://crontab.guru/" target="_blank">crontab.guru</a>). Will be run NEXT time the time passes. For example entering <code>0 4 * * *</code> will run the scan after 4 am in the <a href="#TIMEZONE"><code>TIMEZONE</code> you set above</a>.',
'PHOLUS_DAYS_DATA_name' => 'Data retention',
'PHOLUS_DAYS_DATA_description' => 'How many days of Pholus scan entries should be kept (globally, not device specific!). The <a href="/maintenance.php#tab_Logging">pialert_pholus.log</a> file is not touched. Enter <code>0</code> to disable.',
); );

View File

@@ -20,11 +20,7 @@ $confPath = "../config/pialert.conf";
checkPermissions([$dbPath, $confPath]); checkPermissions([$dbPath, $confPath]);
// Open DB
OpenDB($dbPath);
global $db; global $db;
global $pia_lang;
$result = $db->query("SELECT * FROM Settings"); $result = $db->query("SELECT * FROM Settings");
@@ -43,7 +39,7 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {
); );
} }
$db->close(); CommitDB();
?> ?>
<!-- Page ------------------------------------------------------------------ --> <!-- Page ------------------------------------------------------------------ -->