mqtt prep 10 + readme cleanup

This commit is contained in:
Jokob-sk
2022-12-04 17:57:21 +11:00
parent 1a61bb4670
commit 442323f2dc
4 changed files with 63 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
# Pi.Alert # Pi.Alert
<!--- --------------------------------------------------------------------- ---> <!--- --------------------------------------------------------------------- --->
WIFI / LAN intruder detector. 💻🔍 WIFI / LAN intruder detector.
Scan the devices connected to your WIFI / LAN and alert you the connection of Scan the devices connected to your WIFI / LAN and alert you the connection of
unknown devices. It also warns if a "always connected" devices disconnects. unknown devices. It also warns if a "always connected" devices disconnects.
@@ -9,14 +9,14 @@ unknown devices. It also warns if a "always connected" devices disconnects.
![Main screen][main] ![Main screen][main]
# Docker image 🐳 # 🐳 Docker image
[![Docker](https://img.shields.io/github/workflow/status/jokob-sk/Pi.Alert/docker?label=Build&logo=GitHub)](https://github.com/jokob-sk/Pi.Alert/actions/workflows/docker.yml) [![Docker](https://img.shields.io/github/workflow/status/jokob-sk/Pi.Alert/docker?label=Build&logo=GitHub)](https://github.com/jokob-sk/Pi.Alert/actions/workflows/docker.yml)
[![GitHub Committed](https://img.shields.io/github/last-commit/jokob-sk/Pi.Alert?color=40ba12&label=Committed&logo=GitHub&logoColor=fff)](https://github.com/jokob-sk/Pi.Alert) [![GitHub Committed](https://img.shields.io/github/last-commit/jokob-sk/Pi.Alert?color=40ba12&label=Committed&logo=GitHub&logoColor=fff)](https://github.com/jokob-sk/Pi.Alert)
[![Docker Size](https://img.shields.io/docker/image-size/jokobsk/pi.alert?label=Size&logo=Docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert) [![Docker Size](https://img.shields.io/docker/image-size/jokobsk/pi.alert?label=Size&logo=Docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert)
[![Docker Pulls](https://img.shields.io/docker/pulls/jokobsk/pi.alert?label=Pulls&logo=docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert) [![Docker Pulls](https://img.shields.io/docker/pulls/jokobsk/pi.alert?label=Pulls&logo=docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert)
[![Docker Pushed](https://img.shields.io/badge/dynamic/json?color=0aa8d2&logoColor=fff&label=Pushed&query=last_updated&url=https%3A%2F%2Fhub.docker.com%2Fv2%2Frepositories%2Fjokobsk%2Fpi.alert%2F&logo=docker&link=http://left&link=https://hub.docker.com/repository/docker/jokobsk/pi.alert)](https://hub.docker.com/r/jokobsk/pi.alert) [![Docker Pushed](https://img.shields.io/badge/dynamic/json?color=0aa8d2&logoColor=fff&label=Pushed&query=last_updated&url=https%3A%2F%2Fhub.docker.com%2Fv2%2Frepositories%2Fjokobsk%2Fpi.alert%2F&logo=docker&link=http://left&link=https://hub.docker.com/repository/docker/jokobsk/pi.alert)](https://hub.docker.com/r/jokobsk/pi.alert)
🐳 Docker hub: [jokobsk/Pi.Alert](https://registry.hub.docker.com/r/jokobsk/pi.alert) <br/> 🐳 [Docker hub](https://registry.hub.docker.com/r/jokobsk/pi.alert) <br/>
📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile) <br/> 📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile) <br/>
📚 [Docker instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md) 📚 [Docker instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md)
@@ -32,7 +32,7 @@ The system continuously scans the network for:
- Devices IP changes - Devices IP changes
- Internet IP address changes - Internet IP address changes
## Scan Methods ## 🔍 Scan Methods
Up to three scanning methods are used: Up to three scanning methods are used:
- **Method 1: arp-scan**. The arp-scan system utility is used to search - **Method 1: arp-scan**. The arp-scan system utility is used to search
for devices on the network using arp frames. for devices on the network using arp frames.
@@ -46,7 +46,7 @@ Up to three scanning methods are used:
that were not discovered by the other methods. that were not discovered by the other methods.
### Notification Service Support ### 🧩 Integrations
- [Apprise](https://hub.docker.com/r/caronc/apprise) - [Apprise](https://hub.docker.com/r/caronc/apprise)
- [Pushsafer](https://www.pushsafer.com/) - [Pushsafer](https://www.pushsafer.com/)
- [NTFY](https://ntfy.sh/) - [NTFY](https://ntfy.sh/)
@@ -56,13 +56,13 @@ Up to three scanning methods are used:
| ![Report 1][report1] | ![Report 2][report2] | | ![Report 1][report1] | ![Report 2][report2] |
| -------------------- | -------------------- | | -------------------- | -------------------- |
### Security ### 🔐 Security
There is a configurable login to prevent unauthorized use. There is a configurable login to prevent unauthorized use.
> * Set `PIALERT_WEB_PROTECTION = True` in `pialert.conf` to enable. The default password is `123456`. > * Set `PIALERT_WEB_PROTECTION = True` in `pialert.conf` to enable. The default password is `123456`.
> To change password run `/home/pi/pialert/back/pialert-cli` > To change password run `/home/pi/pialert/back/pialert-cli`
## Features ## 📑 Features
- Display in a visual way all the information collected by the back - Display in a visual way all the information collected by the back
- Sessions, Connected devices, Favorites - Sessions, Connected devices, Favorites
- Events, Presence, Concurrent devices - Events, Presence, Concurrent devices
@@ -85,12 +85,10 @@ There is a configurable login to prevent unauthorized use.
| ![Screen 3][screen3] | ![Screen 4][screen4] | | ![Screen 3][screen3] | ![Screen 4][screen4] |
| ![Screen 5][screen5] | ![Screen 6][screen6] | | ![Screen 5][screen5] | ![Screen 6][screen6] |
# Installation # 📥 Installation
<!--- --------------------------------------------------------------------- ---> <!--- --------------------------------------------------------------------- --->
Initially designed to run on a Raspberry Pi, probably it can run on many other
Linux distributions.
> ⚠ Please note, this [fork (jokob-sk)](https://github.com/jokob-sk/Pi.Alert) is only tested via the [docker install method](dockerfiles/README.md). Check out [leiweibau's fork](https://github.com/leiweibau/Pi.Alert/) if you want to install Pi.Alert on the server directly. ⚠ Please note, this [fork (jokob-sk)](https://github.com/jokob-sk/Pi.Alert) is only tested via the [docker install method](dockerfiles/README.md). Check out [leiweibau's fork](https://github.com/leiweibau/Pi.Alert/) if you want to install Pi.Alert on the server directly.
Instructions for [pucherot's original code can be found here](https://github.com/pucherot/Pi.Alert/) Instructions for [pucherot's original code can be found here](https://github.com/pucherot/Pi.Alert/)
@@ -115,7 +113,7 @@ Instructions for [pucherot's original code can be found here](https://github.com
https://github.com/adobe-fonts/source-sans https://github.com/adobe-fonts/source-sans
### Special thanks 🥇 ### 🥇 Special thanks
This code is a collaborative body of work, with special thanks to: This code is a collaborative body of work, with special thanks to:

View File

@@ -62,6 +62,11 @@ try:
except NameError: # variable not defined, use a default except NameError: # variable not defined, use a default
strdaystokeepEV = str(90) strdaystokeepEV = str(90)
try:
network_scan_minutes = SCAN_CYCLE_MINUTES
except NameError:
network_scan_minutes = 5
# Which sections to include in the reports. Include everything by default # Which sections to include in the reports. Include everything by default
try: try:
includedSections = INCLUDED_SECTIONS includedSections = INCLUDED_SECTIONS
@@ -146,6 +151,11 @@ try:
except NameError: except NameError:
mqttQoS = 0 mqttQoS = 0
try:
mqttDelay = MQTT_DELAY_SEC
except NameError:
mqttDelay = 2
#=============================================================================== #===============================================================================
@@ -153,7 +163,6 @@ except NameError:
#=============================================================================== #===============================================================================
cycle = "" cycle = ""
check_report = [1, "internet_IP", "update_vendors_silent"] check_report = [1, "internet_IP", "update_vendors_silent"]
network_scan_minutes = 5
mqtt_thread_up = False mqtt_thread_up = False
# timestamps of last execution times # timestamps of last execution times
@@ -166,8 +175,6 @@ last_run = now_minus_24h
last_cleanup = now_minus_24h last_cleanup = now_minus_24h
last_update_vendors = time_now - timedelta(days = 7) last_update_vendors = time_now - timedelta(days = 7)
debug_once = False
def main (): def main ():
# Initialize global variables # Initialize global variables
global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors, network_scan_minutes, mqtt_thread_up global time_now, cycle, last_network_scan, last_internet_IP_scan, last_run, last_cleanup, last_update_vendors, network_scan_minutes, mqtt_thread_up
@@ -188,7 +195,7 @@ def main ():
# Header # Header
print ('\nLoop start') print ('\nLoop start')
print ('---------------------------------------------------------') print ('---------------------------')
log_timestamp = time_now log_timestamp = time_now
@@ -219,16 +226,16 @@ def main ():
last_network_scan = time_now last_network_scan = time_now
cycle = 1 # network scan cycle = 1 # network scan
scan_network() scan_network()
# Reporting
if cycle in check_report:
email_reporting()
# clean up the DB once a day # clean up the DB once a day
if last_cleanup + timedelta(hours = 24) < time_now: if last_cleanup + timedelta(hours = 24) < time_now:
last_cleanup = time_now last_cleanup = time_now
cycle = 'cleanup' cycle = 'cleanup'
cleanup_database() cleanup_database()
# Reporting
if cycle in check_report:
email_reporting()
# Close SQL # Close SQL
closeDB() closeDB()
@@ -240,7 +247,7 @@ def main ():
# Footer # Footer
print ('\nLoop end') print ('\nLoop end')
print ('---------------------------------------------------------') print ('---------------------------')
else: else:
# do something # do something
cycle = "" cycle = ""
@@ -1625,10 +1632,8 @@ def email_reporting ():
# write_file (LOG_PATH + '/report_output.txt', mail_text) # write_file (LOG_PATH + '/report_output.txt', mail_text)
# write_file (LOG_PATH + '/report_output.html', mail_html) # write_file (LOG_PATH + '/report_output.html', mail_html)
# Send Mail # Send Mail
if mail_section_Internet == True or mail_section_new_devices == True \ if json_internet != [] or json_new_devices != [] or json_down_devices != [] or json_events != []:
or mail_section_devices_down == True or mail_section_events == True :
print ('\nChanges detected, sending reports...') print ('\nChanges detected, sending reports...')
if REPORT_MAIL : if REPORT_MAIL :
@@ -1880,7 +1885,7 @@ def send_apprise (html):
print(e.output) print(e.output)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
mqtt_connected_to_broker = 0 mqtt_connected_to_broker = False
mqtt_sensors = [] mqtt_sensors = []
def publish_mqtt(client, topic, message): def publish_mqtt(client, topic, message):
@@ -1930,8 +1935,7 @@ def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon):
break break
# save if unique # save if unique
if is_unique: if is_unique:
mqtt_sensors.append(new_sensor_config)
publish_sensor(client, new_sensor_config) publish_sensor(client, new_sensor_config)
@@ -1946,7 +1950,9 @@ class sensor_config:
self.hash = str(hash(str(deviceId) + str(deviceName)+ str(sensorType)+ str(sensorName)+ str(icon))) self.hash = str(hash(str(deviceId) + str(deviceName)+ str(sensorType)+ str(sensorName)+ str(icon)))
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def publish_sensor(client, sensorConf): def publish_sensor(client, sensorConf):
global mqtt_sensors
message = '{ \ message = '{ \
"name":"'+ sensorConf.deviceName +' '+sensorConf.sensorName+'", \ "name":"'+ sensorConf.deviceName +' '+sensorConf.sensorName+'", \
@@ -1964,24 +1970,32 @@ def publish_sensor(client, sensorConf):
topic='homeassistant/'+sensorConf.sensorType+'/'+sensorConf.deviceId+'/'+sensorConf.sensorName+'/config' topic='homeassistant/'+sensorConf.sensorType+'/'+sensorConf.deviceId+'/'+sensorConf.sensorName+'/config'
publish_mqtt(client, topic, message) # add the sensor to the global list to keep track of succesfully added sensors
if publish_mqtt(client, topic, message):
# hack - delay adding to the queue in case the process is
time.sleep(mqttDelay) # restarted and previous publish processes aborted
# (it takes ~2s to update a sensor config on the broker)
mqtt_sensors.append(sensorConf)
# print(len(mqtt_sensors))
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def mqtt_start(): def mqtt_create_client():
def on_disconnect(client, userdata, rc): def on_disconnect(client, userdata, rc):
global mqtt_connected_to_broker global mqtt_connected_to_broker
mqtt_connected_to_broker = 0 mqtt_connected_to_broker = False
# not sure is below line is correct / necessary
# client = mqtt_create_client()
def on_connect(client, userdata, flags, rc): def on_connect(client, userdata, flags, rc):
global mqtt_connected_to_broker global mqtt_connected_to_broker
if rc == 0: if rc == 0:
# print("Connected to broker") print("Connected to broker")
mqtt_connected_to_broker = True #Signal connection mqtt_connected_to_broker = True # Signal connection
else: else:
print("Connection failed") print("Connection failed")
mqtt_connected_to_broker = False
client = mqtt_client.Client(mqttClientId) # Set Connecting Client ID client = mqtt_client.Client(mqttClientId) # Set Connecting Client ID
@@ -1989,7 +2003,18 @@ def mqtt_start():
client.on_connect = on_connect client.on_connect = on_connect
client.on_disconnect = on_disconnect client.on_disconnect = on_disconnect
client.connect(mqttBroker, mqttPort) client.connect(mqttBroker, mqttPort)
client.loop_start() client.loop_start()
return client
#-------------------------------------------------------------------------------
def mqtt_start():
global client, mqtt_connected_to_broker
if mqtt_connected_to_broker == False:
mqtt_connected_to_broker = True
client = mqtt_create_client()
# General stats # General stats

View File

@@ -6,7 +6,7 @@
# 🐳 A docker image for Pi.Alert # 🐳 A docker image for Pi.Alert
🐳 Docker hub: [jokobsk/Pi.Alert](https://registry.hub.docker.com/r/jokobsk/pi.alert) <br/> 🐳 [Docker hub](https://registry.hub.docker.com/r/jokobsk/pi.alert) <br/>
📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile) <br/> 📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile) <br/>
📚 [Docker instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md) 📚 [Docker instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md)

View File

@@ -16,4 +16,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
python /home/pi/pialert/back/pialert.py > /home/pi/pialert/log/pialert.log 2>&1 python /home/pi/pialert/back/pialert.py > /home/pi/pialert/log/pialert.log 2>&1