mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2025-12-07 09:36:05 -08:00
rewrite cron to loop
This commit is contained in:
@@ -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/
|
||||||
|
|||||||
219
back/pialert.py
219
back/pialert.py
@@ -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,72 +146,117 @@ except NameError:
|
|||||||
#===============================================================================
|
#===============================================================================
|
||||||
# MAIN
|
# MAIN
|
||||||
#===============================================================================
|
#===============================================================================
|
||||||
|
non_devices_scan_params = [ "internet_IP", "update_vendors", "cleanup", "update_vendors_silent"]
|
||||||
|
cycle = 1
|
||||||
|
|
||||||
|
# timestamps of last execution times
|
||||||
|
time_now = datetime.datetime.now()
|
||||||
|
now_minus_one_day = time_now - timedelta(hours = 24)
|
||||||
|
|
||||||
|
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 ():
|
def main ():
|
||||||
global startTime
|
|
||||||
global cycle
|
global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors
|
||||||
global log_timestamp
|
# second set of global variables
|
||||||
global sql_connection
|
global startTime, log_timestamp, sql_connection, includedSections, sql
|
||||||
global sql
|
|
||||||
global includedSections
|
|
||||||
|
|
||||||
# Empty stdout and stderr .log files for debugging if needed
|
|
||||||
write_file (LOG_PATH + '/stderr.log', '')
|
|
||||||
write_file (LOG_PATH + '/stdout.log', '')
|
|
||||||
|
|
||||||
# Header
|
|
||||||
print ('\nPi.Alert ' + VERSION +' ('+ VERSION_DATE +')')
|
|
||||||
print ('---------------------------------------------------------')
|
|
||||||
|
|
||||||
# Initialize global variables
|
|
||||||
log_timestamp = datetime.datetime.now()
|
|
||||||
|
|
||||||
# DB
|
|
||||||
sql_connection = None
|
|
||||||
sql = None
|
|
||||||
|
|
||||||
# Timestamp
|
|
||||||
startTime = datetime.datetime.now()
|
|
||||||
startTime = startTime.replace (second=0, microsecond=0)
|
|
||||||
|
|
||||||
# Check parameters
|
# Check parameters
|
||||||
if len(sys.argv) != 2 :
|
if len(sys.argv) == 2 :
|
||||||
print ('usage pialert [scan_cycle] | internet_IP | update_vendors | cleanup' )
|
#print ('usage pialert [scan_cycle] | internet_IP | update_vendors | cleanup' )
|
||||||
return
|
cycle = str(sys.argv[1])
|
||||||
cycle = str(sys.argv[1])
|
# return
|
||||||
|
|
||||||
## Upgrade DB if needed
|
while True:
|
||||||
upgradeDB()
|
# update NOW time
|
||||||
|
time_now = datetime.datetime.now()
|
||||||
|
|
||||||
## Main Commands
|
# proceed if 1 minute passed
|
||||||
if cycle == 'internet_IP':
|
if last_run + timedelta(minutes=1) < time_now :
|
||||||
res = check_internet_IP()
|
|
||||||
elif cycle == 'cleanup':
|
# determine run/scan type based on passed time
|
||||||
res = cleanup_database()
|
if last_internet_IP_scan + timedelta(minutes=3) < time_now:
|
||||||
elif cycle == 'update_vendors':
|
cycle = 'internet_IP'
|
||||||
res = update_devices_MAC_vendors()
|
last_internet_IP_scan = time_now
|
||||||
elif cycle == 'update_vendors_silent':
|
elif last_cleanup + timedelta(hours = 24) < time_now:
|
||||||
res = update_devices_MAC_vendors('-s')
|
last_cleanup = time_now
|
||||||
elif os.path.exists(STOPARPSCAN) == False :
|
cycle = 'cleanup'
|
||||||
res = scan_network()
|
# elif last_update_vendors + timedelta(days = 7) < time_now:
|
||||||
elif os.path.exists(STOPARPSCAN) == True :
|
# last_update_vendors = time_now
|
||||||
res = 0
|
# cycle = 'update_vendors'
|
||||||
|
elif last_network_scan + timedelta(minutes=5) < time_now:
|
||||||
# Check error
|
last_network_scan = time_now
|
||||||
if res != 0 :
|
cycle = 1
|
||||||
closeDB()
|
else:
|
||||||
return res
|
cycle = 0 # don't do anything
|
||||||
|
|
||||||
# Reporting
|
|
||||||
if cycle != 'internet_IP' and cycle != 'cleanup':
|
|
||||||
email_reporting()
|
|
||||||
|
|
||||||
# Close SQL
|
print ("\n\nCYCLE:", cycle)
|
||||||
closeDB()
|
|
||||||
closeDB()
|
|
||||||
|
|
||||||
# Final menssage
|
# last time any scan or maintennace was run
|
||||||
print ('\nDONE!!!\n\n')
|
last_run = time_now
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Header
|
||||||
|
print ('\nPi.Alert ' + VERSION +' ('+ VERSION_DATE +')')
|
||||||
|
print ('---------------------------------------------------------')
|
||||||
|
|
||||||
|
# Initialize global variables
|
||||||
|
log_timestamp = time_now
|
||||||
|
|
||||||
|
# DB
|
||||||
|
sql_connection = None
|
||||||
|
sql = None
|
||||||
|
|
||||||
|
# Timestamp
|
||||||
|
startTime = time_now
|
||||||
|
startTime = startTime.replace (second=0, microsecond=0)
|
||||||
|
|
||||||
|
## Upgrade DB if needed
|
||||||
|
upgradeDB()
|
||||||
|
|
||||||
|
## Main Commands
|
||||||
|
if cycle == 'internet_IP':
|
||||||
|
res = check_internet_IP()
|
||||||
|
elif cycle == 'cleanup':
|
||||||
|
res = cleanup_database()
|
||||||
|
elif cycle == 'update_vendors':
|
||||||
|
res = update_devices_MAC_vendors()
|
||||||
|
elif cycle == 'update_vendors_silent':
|
||||||
|
res = update_devices_MAC_vendors('-s')
|
||||||
|
elif os.path.exists(STOPARPSCAN) == False:
|
||||||
|
res = scan_network()
|
||||||
|
elif os.path.exists(STOPARPSCAN) == True :
|
||||||
|
res = 0
|
||||||
|
|
||||||
|
# # Check error
|
||||||
|
# if res != 0 :
|
||||||
|
# closeDB()
|
||||||
|
# return res
|
||||||
|
|
||||||
|
# Reporting
|
||||||
|
if cycle != 'internet_IP' and cycle != 'cleanup':
|
||||||
|
email_reporting()
|
||||||
|
|
||||||
|
# Close SQL
|
||||||
|
closeDB()
|
||||||
|
#closeDB()
|
||||||
|
|
||||||
|
# Final menssage
|
||||||
|
print ('\nDONE\n\n')
|
||||||
|
#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):
|
||||||
@@ -583,14 +633,16 @@ def execute_arpscan ():
|
|||||||
|
|
||||||
# multiple interfaces
|
# multiple interfaces
|
||||||
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 :
|
||||||
@@ -1761,25 +1815,26 @@ def send_apprise (html):
|
|||||||
logResult (stdout, stderr)
|
logResult (stdout, stderr)
|
||||||
|
|
||||||
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
||||||
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():
|
||||||
def on_connect(client, userdata, flags, rc):
|
if ConnectedMQTT == False:
|
||||||
if rc == 0:
|
def on_connect(client, userdata, flags, rc):
|
||||||
ConnectedMQTT = True
|
if rc == 0:
|
||||||
print(" Connected to MQTT Broker!")
|
ConnectedMQTT = True
|
||||||
else:
|
print(" Connected to MQTT Broker!")
|
||||||
ConnectedMQTT = False
|
else:
|
||||||
print(" Failed to connect, return code %d\n", rc)
|
ConnectedMQTT = False
|
||||||
# Set Connecting Client ID
|
print(" Failed to connect, return code %d\n", rc)
|
||||||
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
|
||||||
|
|||||||
@@ -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
0
install/pialert.cron
Normal file → Executable file
Reference in New Issue
Block a user