more cleanup

This commit is contained in:
Jokob-sk
2022-11-20 14:08:54 +11:00
parent 7d4d5433ef
commit 5164d6566d
7 changed files with 58 additions and 97 deletions

View File

@@ -12,7 +12,6 @@ install/pialert_install.sh
install/pialert_install_no_webserver.sh install/pialert_install_no_webserver.sh
install/pialert_uninstall.sh install/pialert_uninstall.sh
install/pialert_update.sh install/pialert_update.sh
db/pialert.db
LICENSE.txt LICENSE.txt
README.md README.md
tar tar

View File

@@ -32,7 +32,6 @@ COPY . /home/pi/pialert
RUN rm /etc/nginx/sites-available/default \ RUN rm /etc/nginx/sites-available/default \
&& ln -s /home/pi/pialert/install/default /etc/nginx/sites-available/default \ && ln -s /home/pi/pialert/install/default /etc/nginx/sites-available/default \
&& sed -ie 's/listen 80/listen '${PORT}'/g' /etc/nginx/sites-available/default && sed -ie 's/listen 80/listen '${PORT}'/g' /etc/nginx/sites-available/default
# && (crontab -l 2>/dev/null; cat /home/pi/pialert/install/pialert.cron) | crontab -
# it's easy for permissions set in Git to be overridden, so doing it manually # it's easy for permissions set in Git to be overridden, so doing it manually
RUN chmod -R a+rxw /home/pi/pialert/ RUN chmod -R a+rxw /home/pi/pialert/

View File

@@ -104,9 +104,7 @@ except NameError:
ntfyHost = 'https://ntfy.sh' ntfyHost = 'https://ntfy.sh'
# MQTT # MQTT
try: try:
reportMQTT = REPORT_MQTT reportMQTT = REPORT_MQTT
except NameError: except NameError:
@@ -146,8 +144,9 @@ except NameError:
#=============================================================================== #===============================================================================
# MAIN # MAIN
#=============================================================================== #===============================================================================
non_devices_scan_params = [ "internet_IP", "update_vendors", "cleanup", "update_vendors_silent"] send_report_on_cycles = [1, "internet_IP", "update_vendors_silent"]
cycle = 1 cycle = ""
network_scan_minutes = 5
# timestamps of last execution times # timestamps of last execution times
time_now = datetime.datetime.now() time_now = datetime.datetime.now()
@@ -160,53 +159,26 @@ last_cleanup = now_minus_one_day
last_update_vendors = time_now - timedelta(days = 7) last_update_vendors = time_now - timedelta(days = 7)
def main (): def main ():
global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors # Initialize global variables
global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors, network_scan_minutes
# second set of global variables # second set of global variables
global startTime, log_timestamp, sql_connection, includedSections, sql global startTime, log_timestamp, sql_connection, includedSections, sql
# Check parameters
if len(sys.argv) == 2 :
#print ('usage pialert [scan_cycle] | internet_IP | update_vendors | cleanup' )
cycle = str(sys.argv[1])
# return
while True: while True:
# update NOW time # update NOW time
time_now = datetime.datetime.now() time_now = datetime.datetime.now()
# proceed if 1 minute passed # proceed if 1 minute passed
if last_run + timedelta(minutes=1) < time_now : if last_run + timedelta(minutes=1) < time_now :
# determine run/scan type based on passed time # last time any scan or maintennace was run
if last_internet_IP_scan + timedelta(minutes=3) < time_now:
cycle = 'internet_IP'
last_internet_IP_scan = time_now
elif last_cleanup + timedelta(hours = 24) < time_now:
last_cleanup = time_now
cycle = 'cleanup'
# elif last_update_vendors + timedelta(days = 7) < time_now:
# last_update_vendors = time_now
# cycle = 'update_vendors'
elif last_network_scan + timedelta(minutes=5) < time_now:
last_network_scan = time_now
cycle = 1
else:
cycle = 0 # don't do anything
print ("\n\nCYCLE:", cycle)
# last time any scan or maintennace was run
last_run = time_now last_run = time_now
# Header # Header
print ('\nPi.Alert ' + VERSION +' ('+ VERSION_DATE +')') print ('\nPi.Alert ' + VERSION +' ('+ VERSION_DATE +')')
print ('---------------------------------------------------------') print ('---------------------------------------------------------')\
# Initialize global variables
log_timestamp = time_now log_timestamp = time_now
# DB # DB
@@ -220,43 +192,44 @@ def main ():
## Upgrade DB if needed ## Upgrade DB if needed
upgradeDB() upgradeDB()
## Main Commands # determine run/scan type based on passed time
if cycle == 'internet_IP': if last_internet_IP_scan + timedelta(minutes=3) < time_now:
res = check_internet_IP() cycle = 'internet_IP'
elif cycle == 'cleanup': last_internet_IP_scan = time_now
res = cleanup_database() check_internet_IP()
elif cycle == 'update_vendors':
res = update_devices_MAC_vendors() if last_update_vendors + timedelta(days = 7) < time_now:
elif cycle == 'update_vendors_silent': last_update_vendors = time_now
res = update_devices_MAC_vendors('-s') cycle = 'update_vendors'
elif os.path.exists(STOPARPSCAN) == False: update_devices_MAC_vendors()
res = scan_network()
elif os.path.exists(STOPARPSCAN) == True : if last_network_scan + timedelta(minutes=network_scan_minutes) < time_now and os.path.exists(STOPARPSCAN) == False:
res = 0 last_network_scan = time_now
cycle = 1 # network scan
# # Check error scan_network()
# if res != 0 :
# closeDB() if last_cleanup + timedelta(hours = 24) < time_now:
# return res last_cleanup = time_now
cycle = 'cleanup'
cleanup_database()
# Reporting # Reporting
if cycle != 'internet_IP' and cycle != 'cleanup': if cycle in send_report_on_cycles:
email_reporting() email_reporting()
# Close SQL # Close SQL
closeDB() closeDB()
#closeDB()
# Final menssage # Final menssage
print ('\nDONE\n\n') if cycle != "":
#return 0 print ('\nFinished cycle: ', cycle, '\n')
else: else:
# do something # do something
cycle = ""
print ('\n20s passed') print ('\n20s passed')
#loop - recursion #loop - recursion
time.sleep(20) # wait for N seconds time.sleep(20) # wait for N seconds
#=============================================================================== #===============================================================================
@@ -268,7 +241,7 @@ def check_internet_IP ():
print (' Timestamp:', startTime ) print (' Timestamp:', startTime )
# Get Internet IP # Get Internet IP
print ('\nRetrieving Internet IP...') print ('\n Retrieving Internet IP...')
internet_IP = get_internet_IP() internet_IP = get_internet_IP()
# TESTING - Force IP # TESTING - Force IP
# internet_IP = "1.2.3.4" # internet_IP = "1.2.3.4"
@@ -281,7 +254,7 @@ def check_internet_IP ():
print (' ', internet_IP) print (' ', internet_IP)
# Get previous stored IP # Get previous stored IP
print ('\nRetrieving previous IP...') print ('\n Retrieving previous IP...')
openDB() openDB()
previous_IP = get_previous_internet_IP () previous_IP = get_previous_internet_IP ()
print (' ', previous_IP) print (' ', previous_IP)
@@ -297,7 +270,7 @@ def check_internet_IP ():
# Get Dynamic DNS IP # Get Dynamic DNS IP
if DDNS_ACTIVE : if DDNS_ACTIVE :
print ('\nRetrieving Dynamic DNS IP...') print ('\n Retrieving Dynamic DNS IP...')
dns_IP = get_dynamic_DNS_IP() dns_IP = get_dynamic_DNS_IP()
# Check Dynamic DNS IP # Check Dynamic DNS IP
@@ -315,10 +288,8 @@ def check_internet_IP ():
else : else :
print (' No changes to perform') print (' No changes to perform')
else : else :
print ('\nSkipping Dynamic DNS update...') print ('\n Skipping Dynamic DNS update...')
# OK
# return 0
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def get_internet_IP (): def get_internet_IP ():
@@ -418,22 +389,19 @@ def cleanup_database ():
openDB() openDB()
# Cleanup Online History # Cleanup Online History
print ('\nCleanup Online_History...') print (' Cleanup Online_History...')
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')")
print ('\nOptimize Database...') print (' Optimize Database...')
# Cleanup Events # Cleanup Events
print ('\nCleanup Events, up to the lastest '+strdaystokeepEV+' days...') print (' Cleanup Events, up to the lastest '+strdaystokeepEV+' days...')
sql.execute ("DELETE FROM Events WHERE eve_DateTime <= date('now', '-"+strdaystokeepEV+" day')") sql.execute ("DELETE FROM Events WHERE eve_DateTime <= date('now', '-"+strdaystokeepEV+" day')")
# Shrink DB # Shrink DB
print ('\nShrink Database...') print (' Shrink Database...')
sql.execute ("VACUUM;") sql.execute ("VACUUM;")
closeDB() closeDB()
# OK
# return 0
#=============================================================================== #===============================================================================
# UPDATE DEVICE MAC VENDORS # UPDATE DEVICE MAC VENDORS
@@ -524,7 +492,7 @@ def scan_network ():
print (' ScanCycle:', cycle) print (' ScanCycle:', cycle)
print (' Timestamp:', startTime ) print (' Timestamp:', startTime )
# Query ScanCycle properties # # Query ScanCycle properties
print_log ('Query ScanCycle confinguration...') print_log ('Query ScanCycle confinguration...')
scanCycle_data = query_ScanCycle_Data (True) scanCycle_data = query_ScanCycle_Data (True)
if scanCycle_data is None: if scanCycle_data is None:
@@ -558,7 +526,7 @@ def scan_network ():
# Load current scan data # Load current scan data
print ('\nProcessing scan results...') print ('\nProcessing scan results...')
print_log ('Save scanned devices') print_log ('Save scanned devices')
save_scanned_devices (arpscan_devices, cycle_interval) save_scanned_devices (arpscan_devices)
# Print stats # Print stats
print_log ('Print Stats') print_log ('Print Stats')
@@ -603,9 +571,6 @@ def scan_network ():
sql_connection.commit() sql_connection.commit()
closeDB() closeDB()
# OK
# return 0
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def query_ScanCycle_Data (pOpenCloseDB = False): def query_ScanCycle_Data (pOpenCloseDB = False):
# Check if is necesary open DB # Check if is necesary open DB
@@ -635,14 +600,14 @@ def execute_arpscan ():
if type(SCAN_SUBNETS) is list: if type(SCAN_SUBNETS) is list:
print(" arp-scan: Multiple interfaces") print(" arp-scan: Multiple interfaces")
for interface in SCAN_SUBNETS : for interface in SCAN_SUBNETS :
print(" DEBUG 1")
arpscan_output += execute_arpscan_on_interface (interface) arpscan_output += execute_arpscan_on_interface (interface)
# one interface only # one interface only
else: else:
print(" arp-scan: One interface") print(" arp-scan: One interface")
arpscan_output += execute_arpscan_on_interface (SCAN_SUBNETS) arpscan_output += execute_arpscan_on_interface (SCAN_SUBNETS)
print(" DEBUG 2")
# Search IP + MAC + Vendor as regular expresion # Search IP + MAC + Vendor as regular expresion
re_ip = r'(?P<ip>((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))' re_ip = r'(?P<ip>((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))'
re_mac = r'(?P<mac>([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2}))' re_mac = r'(?P<mac>([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2}))'
@@ -653,7 +618,7 @@ def execute_arpscan ():
devices_list = [device.groupdict() devices_list = [device.groupdict()
for device in re.finditer (re_pattern, arpscan_output)] for device in re.finditer (re_pattern, arpscan_output)]
print(" DEBUG 3")
# Delete duplicate MAC # Delete duplicate MAC
unique_mac = [] unique_mac = []
unique_devices = [] unique_devices = []
@@ -662,7 +627,7 @@ def execute_arpscan ():
if device['mac'] not in unique_mac: if device['mac'] not in unique_mac:
unique_mac.append(device['mac']) unique_mac.append(device['mac'])
unique_devices.append(device) unique_devices.append(device)
print(" DEBUG 4")
# DEBUG # DEBUG
# print (devices_list) # print (devices_list)
@@ -744,7 +709,7 @@ def read_DHCP_leases ():
# print (sql.rowcount) # print (sql.rowcount)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def save_scanned_devices (p_arpscan_devices, p_cycle_interval): def save_scanned_devices (p_arpscan_devices):
# 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,))
@@ -752,7 +717,7 @@ def save_scanned_devices (p_arpscan_devices, p_cycle_interval):
# Insert new arp-scan devices # Insert new arp-scan devices
sql.executemany ("INSERT INTO CurrentScan (cur_ScanCycle, cur_MAC, "+ sql.executemany ("INSERT INTO CurrentScan (cur_ScanCycle, cur_MAC, "+
" cur_IP, cur_Vendor, cur_ScanMethod) "+ " cur_IP, cur_Vendor, cur_ScanMethod) "+
"VALUES ("+ cycle + ", :mac, :ip, :hw, 'arp-scan')", "VALUES ("+ str(cycle) + ", :mac, :ip, :hw, 'arp-scan')",
p_arpscan_devices) p_arpscan_devices)
# Insert Pi-hole devices # Insert Pi-hole devices
@@ -765,7 +730,7 @@ def save_scanned_devices (p_arpscan_devices, p_cycle_interval):
WHERE cur_MAC = PH_MAC WHERE cur_MAC = PH_MAC
AND cur_ScanCycle = ? )""", AND cur_ScanCycle = ? )""",
(cycle, (cycle,
(int(startTime.strftime('%s')) - 60 * p_cycle_interval), (int(startTime.strftime('%s')) - 60 * cycle),
cycle) ) cycle) )
# Check Internet connectivity # Check Internet connectivity
@@ -1963,7 +1928,7 @@ def openDB ():
return return
# Log # Log
print_log ('Opening DB...') print_log ('Opening DB...')
# Open DB and Cursor # Open DB and Cursor
sql_connection = sqlite3.connect (DB_PATH, isolation_level=None) sql_connection = sqlite3.connect (DB_PATH, isolation_level=None)

0
config/pialert.conf Normal file → Executable file
View File

4
config/version.conf Normal file → Executable file
View File

@@ -1,3 +1,3 @@
VERSION = '3.6_jokobsk' VERSION = '3.7_jokobsk'
VERSION_YEAR = '2022' VERSION_YEAR = '2022'
VERSION_DATE = '2022-10-09' VERSION_DATE = '2022-11-20'

0
db/pialert.db Normal file → Executable file
View File

View File

@@ -2,10 +2,8 @@
/home/pi/pialert/dockerfiles/user-mapping.sh /home/pi/pialert/dockerfiles/user-mapping.sh
# if custom variables not set we do not need to do anything # if custom variables not set we do not need to do anything
if [ -n "${TZ}" ]; then if [ -n "${TZ}" ]; then
# sed -ie "s|Europe/Berlin|${TZ}|g" /home/pi/pialert/install/pialert.cron sed -ie "s|Europe/Berlin|${TZ}|g" /home/pi/pialert/config/pialert.conf
sed -ie "s|Europe/Berlin|${TZ}|g" /home/pi/pialert/config/pialert.conf
# crontab < /home/pi/pialert/install/pialert.cron
fi fi
if [ -n "${PORT}" ]; then if [ -n "${PORT}" ]; then