rewrite cron to loop

This commit is contained in:
Jokob-sk
2022-11-20 00:56:05 +11:00
parent e880e95c3d
commit 61425415c6
4 changed files with 145 additions and 89 deletions

View File

@@ -29,11 +29,10 @@ RUN groupadd --gid "${USER_GID}" "${USER}" && \
COPY . /home/pi/pialert COPY . /home/pi/pialert
# Pi.Alert # Pi.Alert
RUN python /home/pi/pialert/back/pialert.py update_vendors \ RUN rm /etc/nginx/sites-available/default \
&& 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 - # && (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

@@ -21,7 +21,9 @@ import sys
import subprocess import subprocess
import os import os
import re import re
import time
import datetime import datetime
from datetime import timedelta
import sqlite3 import sqlite3
import socket import socket
import io import io
@@ -33,6 +35,9 @@ from base64 import b64encode
from paho.mqtt import client as mqtt_client from paho.mqtt import client as mqtt_client
# sys.stdout = open('pialert_new.log', 'w')
# sys.stderr = sys.stdout
#=============================================================================== #===============================================================================
# CONFIG CONSTANTS # CONFIG CONSTANTS
#=============================================================================== #===============================================================================
@@ -141,39 +146,77 @@ except NameError:
#=============================================================================== #===============================================================================
# MAIN # MAIN
#=============================================================================== #===============================================================================
def main (): non_devices_scan_params = [ "internet_IP", "update_vendors", "cleanup", "update_vendors_silent"]
global startTime cycle = 1
global cycle
global log_timestamp # timestamps of last execution times
global sql_connection time_now = datetime.datetime.now()
global sql now_minus_one_day = time_now - timedelta(hours = 24)
global includedSections
last_network_scan = now_minus_one_day
last_internet_IP_scan = now_minus_one_day
last_run = now_minus_one_day
last_cleanup = now_minus_one_day
last_update_vendors = time_now - timedelta(days = 7)
def main ():
global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors
# second set of global variables
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:
# update NOW time
time_now = datetime.datetime.now()
# proceed if 1 minute passed
if last_run + timedelta(minutes=1) < time_now :
# determine run/scan type based on passed time
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
# Empty stdout and stderr .log files for debugging if needed
write_file (LOG_PATH + '/stderr.log', '')
write_file (LOG_PATH + '/stdout.log', '')
# Header # Header
print ('\nPi.Alert ' + VERSION +' ('+ VERSION_DATE +')') print ('\nPi.Alert ' + VERSION +' ('+ VERSION_DATE +')')
print ('---------------------------------------------------------') print ('---------------------------------------------------------')
# Initialize global variables # Initialize global variables
log_timestamp = datetime.datetime.now() log_timestamp = time_now
# DB # DB
sql_connection = None sql_connection = None
sql = None sql = None
# Timestamp # Timestamp
startTime = datetime.datetime.now() startTime = time_now
startTime = startTime.replace (second=0, microsecond=0) startTime = startTime.replace (second=0, microsecond=0)
# Check parameters
if len(sys.argv) != 2 :
print ('usage pialert [scan_cycle] | internet_IP | update_vendors | cleanup' )
return
cycle = str(sys.argv[1])
## Upgrade DB if needed ## Upgrade DB if needed
upgradeDB() upgradeDB()
@@ -186,15 +229,15 @@ def main ():
res = update_devices_MAC_vendors() res = update_devices_MAC_vendors()
elif cycle == 'update_vendors_silent': elif cycle == 'update_vendors_silent':
res = update_devices_MAC_vendors('-s') res = update_devices_MAC_vendors('-s')
elif os.path.exists(STOPARPSCAN) == False : elif os.path.exists(STOPARPSCAN) == False:
res = scan_network() res = scan_network()
elif os.path.exists(STOPARPSCAN) == True : elif os.path.exists(STOPARPSCAN) == True :
res = 0 res = 0
# Check error # # Check error
if res != 0 : # if res != 0 :
closeDB() # closeDB()
return res # return res
# Reporting # Reporting
if cycle != 'internet_IP' and cycle != 'cleanup': if cycle != 'internet_IP' and cycle != 'cleanup':
@@ -202,11 +245,18 @@ def main ():
# Close SQL # Close SQL
closeDB() closeDB()
closeDB() #closeDB()
# Final menssage # Final menssage
print ('\nDONE!!!\n\n') print ('\nDONE\n\n')
return 0 #return 0
else:
# do something
print ('\n20s passed\n\n')
#loop - recursion
time.sleep(20) # wait for N seconds
#=============================================================================== #===============================================================================
@@ -268,7 +318,7 @@ def check_internet_IP ():
print ('\nSkipping Dynamic DNS update...') print ('\nSkipping Dynamic DNS update...')
# OK # OK
return 0 # return 0
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def get_internet_IP (): def get_internet_IP ():
@@ -382,7 +432,7 @@ def cleanup_database ():
closeDB() closeDB()
# OK # OK
return 0 # return 0
#=============================================================================== #===============================================================================
@@ -554,7 +604,7 @@ def scan_network ():
closeDB() closeDB()
# OK # OK
return 0 # return 0
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def query_ScanCycle_Data (pOpenCloseDB = False): def query_ScanCycle_Data (pOpenCloseDB = False):
@@ -585,12 +635,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}))'
@@ -601,6 +653,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 = []
@@ -609,6 +662,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)
@@ -1316,6 +1370,9 @@ def skip_repeated_notifications ():
#=============================================================================== #===============================================================================
# REPORTING # REPORTING
#=============================================================================== #===============================================================================
# create a json for webhook and mqtt notifications to provide further integration options
json_final = []
def email_reporting (): def email_reporting ():
global mail_text global mail_text
global mail_html global mail_html
@@ -1504,9 +1561,6 @@ def email_reporting ():
'TABLE_EVENTS', mail_text_events, mail_html_events) 'TABLE_EVENTS', mail_text_events, mail_html_events)
# create a json for webhook notifications to provide further integration options
json_final = []
json_final = { json_final = {
"internet": json_internet, "internet": json_internet,
"new_devices": json_new_devices, "new_devices": json_new_devices,
@@ -1549,8 +1603,8 @@ def email_reporting ():
else : else :
print (' Skip PUSHSAFER...') print (' Skip PUSHSAFER...')
if reportMQTT : if reportMQTT :
print (' Sending report by MQTT...') print (' Establishing MQTT runtime...')
send_mqtt (connect_mqtt(), json_final) start_sending_mqtt (connect_mqtt(), json_final)
else : else :
print (' Skip MQTT...') print (' Skip MQTT...')
else : else :
@@ -1762,8 +1816,10 @@ def send_apprise (html):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
ConnectedMQTT = False #global variable for the state of the connection ConnectedMQTT = False #global variable for the state of the connection
client = mqtt_client.Client(mqttClientId) # Set Connecting Client ID
def connect_mqtt(): def connect_mqtt():
if ConnectedMQTT == False:
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
if rc == 0: if rc == 0:
ConnectedMQTT = True ConnectedMQTT = True
@@ -1771,15 +1827,14 @@ def connect_mqtt():
else: else:
ConnectedMQTT = False ConnectedMQTT = False
print(" Failed to connect, return code %d\n", rc) print(" Failed to connect, return code %d\n", rc)
# Set Connecting Client ID
client = mqtt_client.Client(mqttClientId)
client.username_pw_set(mqttUser, mqttPassword) client.username_pw_set(mqttUser, mqttPassword)
client.on_connect = on_connect client.on_connect = on_connect
client.connect(mqttBroker, mqttPort) client.connect(mqttBroker, mqttPort)
return client return client
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def send_mqtt(client, json_final): def start_sending_mqtt(client, json_final):
client.loop_start() #start the loop client.loop_start() #start the loop
index = 0 index = 0

View File

@@ -3,9 +3,9 @@
# 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/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 # crontab < /home/pi/pialert/install/pialert.cron
fi fi
if [ -n "${PORT}" ]; then if [ -n "${PORT}" ]; then
@@ -17,4 +17,6 @@ chown -R www-data:www-data /home/pi/pialert/db/pialert.db
/etc/init.d/php7.4-fpm start /etc/init.d/php7.4-fpm start
/etc/init.d/nginx start /etc/init.d/nginx start
cron -f #cron -f
python /home/pi/pialert/back/pialert.py > /home/pi/pialert/log/pialert.log 2>&1

0
install/pialert.cron Normal file → Executable file
View File