mqtt prep 7

This commit is contained in:
Jokob-sk
2022-11-28 23:30:44 +11:00
parent c790b1ee88
commit 525f6c3bf1
2 changed files with 80 additions and 81 deletions

65
README.md Normal file → Executable file
View File

@@ -7,8 +7,7 @@ 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.
![Main screen][main] ![Main screen][main]
*(Apologies for my English and my limited knowledge of Python, php and
JavaScript)*
# 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)
@@ -17,7 +16,6 @@ JavaScript)*
[![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)
🥇 Pi.Alert credit goes to [pucherot/Pi.Alert](https://github.com/pucherot/Pi.Alert) <br/>
🐳 Docker Image: [jokobsk/Pi.Alert](https://registry.hub.docker.com/r/jokobsk/pi.alert) <br/> 🐳 Docker Image: [jokobsk/Pi.Alert](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/>
📚 [Dockerfile instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md) 📚 [Dockerfile instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md)
@@ -47,41 +45,30 @@ Up to three scanning methods are used:
examines the DHCP leases (addresses assigned) to find active devices examines the DHCP leases (addresses assigned) to find active devices
that were not discovered by the other methods. that were not discovered by the other methods.
## Components
The system consists of two parts:
### Back ### Notification Service Support
In charge of: - [Apprise](https://hub.docker.com/r/caronc/apprise)
- Scan the network searching connected devices using the scanning methods - [Pushsafer](https://www.pushsafer.com/)
described - [NTFY](https://ntfy.sh/)
- Store the information in the DB - Webhooks ([sample JSON](docs/webhook_json_sample.json))
- Report the changes detected by e-mail and/or other services ([Apprise](https://hub.docker.com/r/caronc/apprise), [Pushsafer](https://www.pushsafer.com/), [NTFY](https://ntfy.sh/), Webhooks ([sample JSON](docs/webhook_json_sample.json))) - (❗Experimental) Home Assistant (via [MQTT](https://www.home-assistant.io/integrations/mqtt/) (processing ~5s per device, deleting not supported, use [MQTT Explorer](https://mqtt-explorer.com/) for now))
- Optional speedtest for Device "Internet"
- DB cleanup tasks via cron
- a pialert-cli that helps to configure login and password
| ![Report 1][report1] | ![Report 2][report2] | | ![Report 1][report1] | ![Report 2][report2] |
| -------------------- | -------------------- | | -------------------- | -------------------- |
### Front ### 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`
A web frontend that allows: ## Features
- Manage the devices inventory and the characteristics
- 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 - Sessions, Connected devices, Favorites
- Connected devices - Events, Presence, Concurrent devices
- Favorites
- Events
- Presence
- Concurrent devices
- Down alerts - Down alerts
- IP's - IP's
- Manual Nmap scans - Manual Nmap scans, Optional speedtest for Device "Internet"
- Optional speedtest for Device "Internet"
- Simple Network relationship display - Simple Network relationship display
- Maintenance tasks and Settings like: - Maintenance tasks and Settings like:
- Status Infos (active scans, database size, backup counter) - Status Infos (active scans, database size, backup counter)
@@ -89,9 +76,8 @@ A web frontend that allows:
- Language Selection (english, german, spanish) - Language Selection (english, german, spanish)
- Light/Dark-Mode Switch - Light/Dark-Mode Switch
- Pause arp-scan - Pause arp-scan
- DB maintenance tools - DB maintenance, Backup, Restore tools
- DB Backup and Restore - CSV Export / Import
- CSV Export / Import (Experimental)
- Help/FAQ Section - Help/FAQ Section
| ![Screen 1][screen1] | ![Screen 2][screen2] | | ![Screen 1][screen1] | ![Screen 2][screen2] |
@@ -118,24 +104,6 @@ Instructions for [pucherot's original code can be found here](https://github.com
### [Versions History](docs/VERSIONS_HISTORY.md) ### [Versions History](docs/VERSIONS_HISTORY.md)
### Powered by:
| Product | Objetive |
| ------------- | ------------------------------------------------------- |
| Python | Programming language for the Back |
| PHP | Programming language for the Front-end |
| JavaScript | Programming language for the Front-end |
| Bootstrap | Front-end framework |
| Admin.LTE | Bootstrap template |
| FullCalendar | Calendar component |
| Sqlite | DB engine |
| Lighttpd | Webserver |
| arp-scan | Scan network using arp commands |
| Pi.hole | DNS Server with Ad-block |
| dnsmasq | DHCP Server |
| nmap | Network Scanner |
| zip | Filecompression Tool |
| speedtest-cli | Python SpeedTest https://github.com/sivel/speedtest-cli |
### License ### License
GPL 3.0 GPL 3.0
[Read more here](LICENSE.txt) [Read more here](LICENSE.txt)
@@ -146,15 +114,12 @@ Instructions for [pucherot's original code can be found here](https://github.com
Source of the selfhosted Fonts Source of the selfhosted Fonts
https://github.com/adobe-fonts/source-sans https://github.com/adobe-fonts/source-sans
### Contact
pi.alert.application@gmail.com
***Suggestions and comments are welcome***
### 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:
- 🏆 [pucherot/Pi.Alert](https://github.com/pucherot/Pi.Alert) is the original creator od PiAlert
- [leiweibau](https://github.com/leiweibau/Pi.Alert): Things - [leiweibau](https://github.com/leiweibau/Pi.Alert): Things
- [Macleykun](https://github.com/Macleykun): Help with Dockerfile clean-up - [Macleykun](https://github.com/Macleykun): Help with Dockerfile clean-up
- [Final-Hawk](https://github.com/Final-Hawk): Help with NTFY, styling and other fixes - [Final-Hawk](https://github.com/Final-Hawk): Help with NTFY, styling and other fixes

View File

@@ -141,6 +141,12 @@ try:
except NameError: except NameError:
mqttPassword = '' mqttPassword = ''
try:
mqttQoS = MQTT_QOS
except NameError:
mqttQoS = 0
#=============================================================================== #===============================================================================
# MAIN # MAIN
@@ -441,8 +447,7 @@ def update_devices_MAC_vendors (pArg = ''):
update_output = subprocess.check_output (update_args) update_output = subprocess.check_output (update_args)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
# An error occured, handle it # An error occured, handle it
print(e.output) print(e.output)
update_output = ""
# DEBUG # DEBUG
# update_args = ['./vendors_db_update.sh'] # update_args = ['./vendors_db_update.sh']
@@ -562,7 +567,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) save_scanned_devices (arpscan_devices, cycle_interval)
# Print stats # Print stats
print_log ('Print Stats') print_log ('Print Stats')
@@ -752,7 +757,8 @@ def read_DHCP_leases ():
# print (sql.rowcount) # print (sql.rowcount)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def save_scanned_devices (p_arpscan_devices): def save_scanned_devices (p_arpscan_devices, p_cycle_interval):
cycle = 1 # always 1, only one cycle supported
# 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,))
@@ -773,7 +779,7 @@ def save_scanned_devices (p_arpscan_devices):
WHERE cur_MAC = PH_MAC WHERE cur_MAC = PH_MAC
AND cur_ScanCycle = ? )""", AND cur_ScanCycle = ? )""",
(cycle, (cycle,
(int(startTime.strftime('%s')) - 60 * cycle), (int(startTime.strftime('%s')) - 60 * p_cycle_interval),
cycle) ) cycle) )
# Check Internet connectivity # Check Internet connectivity
@@ -1843,6 +1849,7 @@ def send_apprise (html):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
mqtt_connected_to_broker = 0 mqtt_connected_to_broker = 0
mqtt_sensors = []
def publish_mqtt(client, topic, message): def publish_mqtt(client, topic, message):
status = 1 status = 1
@@ -1850,7 +1857,7 @@ def publish_mqtt(client, topic, message):
result = client.publish( result = client.publish(
topic=topic, topic=topic,
payload=message, payload=message,
qos=1, qos=mqttQoS,
retain=True, retain=True,
) )
@@ -1862,17 +1869,38 @@ def publish_mqtt(client, topic, message):
return True return True
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def create_generic_device(client): def create_generic_device(client):
deviceName = 'PiAlert' deviceName = 'PiAlert'
deviceId = 'pialert' deviceId = 'pialert'
device_sensor(client, deviceId, deviceName, 'sensor', 'online', 'wifi-check') device_sensor(client, deviceId, deviceName, 'sensor', 'online', 'wifi-check')
device_sensor(client, deviceId, deviceName, 'sensor', 'down', 'wifi-cancel') device_sensor(client, deviceId, deviceName, 'sensor', 'down', 'wifi-cancel')
device_sensor(client, deviceId, deviceName, 'sensor', 'all', 'wifi') device_sensor(client, deviceId, deviceName, 'sensor', 'all', 'wifi')
device_sensor(client, deviceId, deviceName, 'sensor', 'archived', 'wifi-lock') device_sensor(client, deviceId, deviceName, 'sensor', 'archived', 'wifi-lock')
device_sensor(client, deviceId, deviceName, 'sensor', 'new', 'wifi-plus') device_sensor(client, deviceId, deviceName, 'sensor', 'new', 'wifi-plus')
device_sensor(client, deviceId, deviceName, 'sensor', 'unknown', 'wifi-alert') device_sensor(client, deviceId, deviceName, 'sensor', 'unknown', 'wifi-alert')
# #-------------------------------------------------------------------------------
# def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon):
# global mqtt_sensors
# new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon)
# if new_sensor_config in mqtt_sensors
# mqtt_sensors
# #-------------------------------------------------------------------------------
# class sensor_config:
# def __init__(self, deviceId, deviceName, sensorType, sensorName, icon):
# self.deviceId = deviceId
# self.deviceName = deviceName
# self.sensorType = sensorType
# self.sensorName = sensorName
# self.icon = icon
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@@ -1882,25 +1910,32 @@ class device_sensor:
self.deviceName = deviceName self.deviceName = deviceName
self.sensorType = sensorType self.sensorType = sensorType
self.sensorName = sensorName self.sensorName = sensorName
self.icon = icon self.icon = icon
message = '{ \ global mqtt_sensors
"name":"'+ deviceName +' '+sensorName+'", \
"state_topic":"system-sensors/'+sensorType+'/'+deviceId+'/state", \
"value_template":"{{value_json.'+sensorName+'}}", \
"unique_id":"'+deviceId+'_sensor_'+sensorName+'", \
"device": \
{ \
"identifiers": ["'+deviceId+'_sensor"], \
"manufacturer": "PiAlert", \
"name":"'+deviceName+'" \
}, \
"icon":"mdi:'+icon+'" \
}'
topic='homeassistant/'+sensorType+'/'+deviceId+'/'+sensorName+'/config' if (self in mqtt_sensors) == False:
mqtt_sensors.append(self)
message = '{ \
"name":"'+ deviceName +' '+sensorName+'", \
"state_topic":"system-sensors/'+sensorType+'/'+deviceId+'/state", \
"value_template":"{{value_json.'+sensorName+'}}", \
"unique_id":"'+deviceId+'_sensor_'+sensorName+'", \
"device": \
{ \
"identifiers": ["'+deviceId+'_sensor"], \
"manufacturer": "PiAlert", \
"name":"'+deviceName+'" \
}, \
"icon":"mdi:'+icon+'" \
}'
topic='homeassistant/'+sensorType+'/'+deviceId+'/'+sensorName+'/config'
publish_mqtt(client, topic, message)
publish_mqtt(client, topic, message)
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def mqtt_start(): def mqtt_start():
@@ -1965,7 +2000,6 @@ def mqtt_start():
device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network') device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network')
device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline') device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline')
device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog') device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog')
# update device sensors in home assistant # update device sensors in home assistant
@@ -1992,8 +2026,8 @@ def mqtt_start():
# qos=1, # qos=1,
# retain=True, # retain=True,
# ) # )
time.sleep(10) # time.sleep(10)
client.loop() # client.loop()