MQTT, DHCPLSS work🔌

This commit is contained in:
Jokob-sk
2023-10-15 22:39:21 +11:00
parent a2f3666134
commit 2a4ac2f2be
6 changed files with 56 additions and 43 deletions

View File

@@ -30,6 +30,11 @@ services:
- ${DEV_LOCATION}/front/css:/home/pi/pialert/front/css - ${DEV_LOCATION}/front/css:/home/pi/pialert/front/css
- ${DEV_LOCATION}/front/lib/AdminLTE:/home/pi/pialert/front/lib/AdminLTE - ${DEV_LOCATION}/front/lib/AdminLTE:/home/pi/pialert/front/lib/AdminLTE
- ${DEV_LOCATION}/front/js:/home/pi/pialert/front/js - ${DEV_LOCATION}/front/js:/home/pi/pialert/front/js
- ${DEV_LOCATION}/dockerfiles/start.sh:/home/pi/pialert/dockerfiles/start.sh
- ${DEV_LOCATION}/dockerfiles/user-mapping.sh:/home/pi/pialert/dockerfiles/user-mapping.sh
- ${DEV_LOCATION}/install/install.sh:/home/pi/pialert/install/install.sh
- ${DEV_LOCATION}/install/install_python.sh:/home/pi/pialert/install/install_python.sh
- ${DEV_LOCATION}/install/install_dependencies.sh:/home/pi/pialert/install/install_dependencies.sh
- ${DEV_LOCATION}/front/api:/home/pi/pialert/front/api - ${DEV_LOCATION}/front/api:/home/pi/pialert/front/api
- ${DEV_LOCATION}/front/php:/home/pi/pialert/front/php - ${DEV_LOCATION}/front/php:/home/pi/pialert/front/php
- ${DEV_LOCATION}/front/deviceDetails.php:/home/pi/pialert/front/deviceDetails.php - ${DEV_LOCATION}/front/deviceDetails.php:/home/pi/pialert/front/deviceDetails.php

View File

@@ -67,7 +67,7 @@
"column": "Object_PrimaryID", "column": "Object_PrimaryID",
"css_classes": "col-sm-2", "css_classes": "col-sm-2",
"show": true, "show": true,
"type": "url", "type": "label",
"default_value":"", "default_value":"",
"options": [], "options": [],
"localized": ["name"], "localized": ["name"],
@@ -161,8 +161,8 @@
{ {
"column": "Watched_Value4", "column": "Watched_Value4",
"css_classes": "col-sm-2", "css_classes": "col-sm-2",
"show": false, "show": true,
"type": "label", "type": "device_mac",
"default_value":"", "default_value":"",
"options": [], "options": [],
"localized": ["name"], "localized": ["name"],

View File

@@ -78,7 +78,7 @@ def check_config():
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
class sensor_config: class sensor_config:
def __init__(self, deviceId, deviceName, sensorType, sensorName, icon): def __init__(self, deviceId, deviceName, sensorType, sensorName, icon, mac):
self.deviceId = deviceId self.deviceId = deviceId
self.deviceName = deviceName self.deviceName = deviceName
self.sensorType = sensorType self.sensorType = sensorType
@@ -102,13 +102,16 @@ class sensor_config:
# Log sensor # Log sensor
global plugin_objects global plugin_objects
if mac == '':
mac = "N/A"
plugin_objects.add_object( plugin_objects.add_object(
primaryId = deviceId, primaryId = deviceId,
secondaryId = sensorName, secondaryId = sensorName,
watched1 = deviceName, watched1 = deviceName,
watched2 = sensorType, watched2 = sensorType,
watched3 = hash, watched3 = hash_value,
watched4 = 'null', watched4 = mac,
extra = 'null', extra = 'null',
foreignKey = deviceId foreignKey = deviceId
) )
@@ -147,11 +150,11 @@ def create_generic_device(client):
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon): def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon, mac=""):
global mqtt_sensors global mqtt_sensors
new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon) new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon, mac)
# save if new # save if new
if new_sensor_config.isNew: if new_sensor_config.isNew:
@@ -271,11 +274,11 @@ def mqtt_start(db):
deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower() deviceId = 'mac_' + device["dev_MAC"].replace(" ", "").replace(":", "_").lower()
deviceNameDisplay = re.sub('[^a-zA-Z0-9-_\s]', '', device["dev_Name"]) deviceNameDisplay = re.sub('[^a-zA-Z0-9-_\s]', '', device["dev_Name"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'last_ip', 'ip-network') create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'last_ip', 'ip-network', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'binary_sensor', 'is_present', 'wifi') create_sensor(client, deviceId, deviceNameDisplay, 'binary_sensor', 'is_present', 'wifi', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network') create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'mac_address', 'folder-key-network', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline') create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline', device["dev_MAC"])
create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog') create_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog', device["dev_MAC"])
# update device sensors in home assistant # update device sensors in home assistant

View File

@@ -44,25 +44,31 @@ def main():
def get_entries(path, plugin_objects): def get_entries(path, plugin_objects):
# Check if the path exists # Check if the path exists
if os.path.exists(path) == False: if not os.path.exists(path):
mylog('none', [f'[{pluginName}] Error: "{path}" does not exist.']) mylog('none', [f'[{pluginName}] Error: "{path}" does not exist.'])
else: else:
# Handle pihole specific dhcp.leases files # Detect file encoding
if 'pihole' in path: with open(path, 'rb') as f:
result = chardet.detect(f.read())
with open(path, 'r') as f:
for line in f: # Use the detected encoding
encoding = result['encoding']
# Handle pihole-specific dhcp.leases files
if 'pihole' in path:
with open(path, 'r', encoding=encoding, errors='replace') as f:
for line in f:
row = line.rstrip().split() row = line.rstrip().split()
if len(row) == 5: if len(row) == 5:
plugin_objects.add_object( plugin_objects.add_object(
primaryId = handleEmpty(row[1]), primaryId=handleEmpty(row[1]),
secondaryId = handleEmpty(row[2]), secondaryId=handleEmpty(row[2]),
watched1 = handleEmpty('True'), watched1=handleEmpty('True'),
watched2 = handleEmpty(row[3]), watched2=handleEmpty(row[3]),
watched3 = handleEmpty(row[4]), watched3=handleEmpty(row[4]),
watched4 = handleEmpty('True'), watched4=handleEmpty('True'),
extra = handleEmpty(path), extra=handleEmpty(path),
foreignKey = handleEmpty(row[1]) foreignKey=handleEmpty(row[1])
) )
else: else:
# Handle generic dhcp.leases files # Handle generic dhcp.leases files

View File

@@ -1,12 +0,0 @@
# ------------------------------------------------------------------------------
# Pi.Alert
# Open Source Network Guard / WIFI & LAN intrusion detector
#
# pialert_front.conf - lighttpd domain redirection
# ------------------------------------------------------------------------------
# Puche 2021 GNU GPLv3
# ------------------------------------------------------------------------------
$HTTP["host"] == "pi.alert" {
server.document-root = "/var/www/html/"
}

View File

@@ -82,13 +82,24 @@ def list_to_csv(arr):
mylog('debug', f'[{module_name}] isinstance(arr, list) : {isinstance(arr, list)} | isinstance(arr, str) : {isinstance(arr, str)}') mylog('debug', f'[{module_name}] isinstance(arr, list) : {isinstance(arr, list)} | isinstance(arr, str) : {isinstance(arr, str)}')
if isinstance(arr, str): if isinstance(arr, str):
return arr.replace('[','').replace(']','').replace("'", '') # removing brackets and single quotes (not allowed) tmpStr = arr.replace('[','').replace(']','').replace("'", '') # removing brackets and single quotes (not allowed)
if ',' in tmpStr:
# Split the string into a list and trim whitespace
cleanedStr = [tmpSubStr.strip() for tmpSubStr in tmpStr.split(',')]
# Join the list elements using a comma
result_string = ",".join(cleanedStr)
else:
result_string = tmpStr
return result_string
elif isinstance(arr, list): elif isinstance(arr, list):
for arrayItem in arr: for arrayItem in arr:
# only one column flattening is supported # only one column flattening is supported
if isinstance(arrayItem, list): if isinstance(arrayItem, list):
arrayItemStr = str(arrayItem[0]).replace("'", '') # removing single quotes - not allowed arrayItemStr = str(arrayItem[0]).replace("'", '').strip() # removing single quotes - not allowed
else: else:
# is string already # is string already
arrayItemStr = arrayItem arrayItemStr = arrayItem