From 07367a2ca32f253b6d9890b2e104df6e82bef05f Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Fri, 22 Sep 2023 08:22:28 +1000 Subject: [PATCH] NETWORK_DEVICE_TYPES #452 --- docs/NETWORK_TREE.md | 2 +- front/network.php | 7 +++-- front/php/server/devices.php | 9 ++++-- front/php/server/util.php | 41 +++++++++++++++++++------ front/php/templates/language/en_us.json | 4 ++- pialert/initialise.py | 8 ++++- 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/docs/NETWORK_TREE.md b/docs/NETWORK_TREE.md index 700e7d68..480691a3 100755 --- a/docs/NETWORK_TREE.md +++ b/docs/NETWORK_TREE.md @@ -29,7 +29,7 @@ In this example you will setup a device named `rapberrypi` as a `Switch` in our - In the (2) `Details` tab navigate to the the `Type` (3) dropdown and select the type `Switch` (4). > Note: Only the following device types will show up as selectable Network nodes ( = devices you can connect other devices to): -> AP, Firewall, Gateway, Hypervisor, PLC, Powerline, Router, Switch, USB LAN Adapter, USB WIFI Adapter and WLAN. +> AP, Firewall, Gateway, Hypervisor, PLC, Powerline, Router, Switch, USB LAN Adapter, USB WIFI Adapter and WLAN. Custom types can be added via the `NETWORK_DEVICE_TYPES` setting. - Assign a device to your root device from the `Node` (5) dropdown which has the MAC `Internet` (6) (Your name may differ, but the MAC needs to be set to `Internet` - this is done by default). diff --git a/front/network.php b/front/network.php index 8dcd49d6..b28b0b80 100755 --- a/front/network.php +++ b/front/network.php @@ -6,9 +6,8 @@ define('badge_online', '
Online
'); define('badge_offline', '
Offline
'); define('circle_online', '
 
'); - define('circle_offline', '
 
'); + define('circle_offline', '
 
'); - $NETWORKTYPES = getNetworkTypes(); ?> @@ -267,6 +266,8 @@ // Smart TV (leaf) Switch 2 (node (for the PC) and leaf (for Switch 1)) // \ // PC (leaf) <------- leafs are not included in this SQL query + + $networkDeviceTypes = str_replace("]", "",(str_replace("[", "", getSettingValue("NETWORK_DEVICE_TYPES")))); $sql = "SELECT node_name, node_mac, online, node_type, node_ports_count, parent_mac, node_icon FROM @@ -278,7 +279,7 @@ a.dev_Network_Node_MAC_ADDR as parent_mac, a.dev_Icon as node_icon FROM Devices a - WHERE a.dev_DeviceType in ('AP', 'Gateway', 'Firewall', 'Hypervisor', 'Powerline', 'Switch', 'WLAN', 'PLC', 'Router','USB LAN Adapter', 'USB WIFI Adapter', 'Internet') + WHERE a.dev_DeviceType in (".$networkDeviceTypes.") ) t1 LEFT JOIN ( diff --git a/front/php/server/devices.php b/front/php/server/devices.php index 67af2ffa..fcebb78c 100755 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -754,8 +754,13 @@ function getOwners() { function getNetworkNodes() { global $db; - // Device Data - $sql = 'SELECT * FROM Devices WHERE dev_DeviceType in ( "AP", "Gateway", "Firewall", "Hypervisor", "Powerline", "Switch", "WLAN", "PLC", "Router","USB LAN Adapter", "USB WIFI Adapter")'; + // Device Data + $networkDeviceTypes = str_replace("]", "",(str_replace("[", "", getSettingValue("NETWORK_DEVICE_TYPES")))); + + + $sql = 'SELECT * FROM Devices WHERE dev_DeviceType in ( '. $networkDeviceTypes .' )'; + + // echo $sql; $result = $db->query($sql); diff --git a/front/php/server/util.php b/front/php/server/util.php index 0561c1f2..991e0238 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -349,6 +349,38 @@ function getString ($codeName, $default) { return $default; } +// ------------------------------------------------------------------------------------------- +function getSettingValue($codeName) { + // Define the JSON endpoint URL + $url = dirname(__FILE__).'/../../../front/api/table_settings.json'; + + // Fetch the JSON data + $json = file_get_contents($url); + + // Check if the JSON data was successfully fetched + if ($json === false) { + return 'Could not get json data'; + } + + // Decode the JSON data + $data = json_decode($json, true); + + // Check if the JSON decoding was successful + if (json_last_error() !== JSON_ERROR_NONE) { + return 'Could not decode json data'; + } + + // Search for the setting by Code_Name + foreach ($data['data'] as $setting) { + if ($setting['Code_Name'] === $codeName) { + return $setting['Value']; + // echo $setting['Value']; + } + } + + // Return false if the setting was not found + return 'Could not find setting '.$codeName; +} // ------------------------------------------------------------------------------------------- @@ -419,16 +451,7 @@ function handleNull ($text, $default = "") { } -// ------------------------------------------------------------------------------------------- -// Currently unused - should be source of truth for network types (or define somewhere else?) -function getNetworkTypes(){ - $array = array( - "AP", "Gateway", "Firewall", "Hypervisor", "Powerline", "Switch", "WLAN", "PLC", "Router","USB LAN Adapter", "USB WIFI Adapter" - ); - - return $array; -} // ------------------------------------------------------------------------------------------- function getDevicesColumns(){ diff --git a/front/php/templates/language/en_us.json b/front/php/templates/language/en_us.json index 5ae9a97f..ff1b3f1a 100755 --- a/front/php/templates/language/en_us.json +++ b/front/php/templates/language/en_us.json @@ -426,7 +426,7 @@ "HelpFAQ_Cat_Device_200_head" : "I have devices in my list that I do not know about. After deleting them, they always reappear.", "HelpFAQ_Cat_Device_200_text" : "If you use Pi-hole, please note that Pi.Alert retrieves information from Pi-hole. Pause Pi.Alert, go to the settings page in Pi-hole and delete the DHCP lease if necessary. Then, also in Pi-hole, look under Tools -> Network to see if you can find the recurring hosts there. If yes, delete them there as well. Now you can start Pi.Alert again. Now the device(s) should not show up anymore.", "HelpFAQ_Cat_Detail_300_head" : "What means ", - "HelpFAQ_Cat_Detail_300_text_a" : "means a network device (a device of the type AP, Gateway, Firewall, Hypervisor, Powerline, Switch, WLAN, PLC, Router,USB LAN Adapter, USB WIFI Adapter, or Internet).", + "HelpFAQ_Cat_Detail_300_text_a" : "means a network device (a device of the type AP, Gateway, Firewall, Hypervisor, Powerline, Switch, WLAN, PLC, Router,USB LAN Adapter, USB WIFI Adapter, or Internet). Custom types can be added via the NETWORK_DEVICE_TYPES setting.", "HelpFAQ_Cat_Detail_300_text_b" : "designates the port number where the currently edited device is connected to this network device. Read this guide for more info.", "HelpFAQ_Cat_Detail_301_head_a" : "When is scanning now? At ", "HelpFAQ_Cat_Detail_301_head_b" : " says 1min but the graph shows 5min intervals.", @@ -491,6 +491,8 @@ "REPORT_DASHBOARD_URL_description" : "This URL is used as the base for generating links in the emails. Enter full URL starting with http:// including the port number (no trailig slash /).", "DIG_GET_IP_ARG_name" : "Internet IP discovery", "DIG_GET_IP_ARG_description" : "Change the dig utility arguments if you have issues resolving your Internet IP. Arguments are added at the end of the following command: dig +short .", + "NETWORK_DEVICE_TYPES_name" : "Network device types", + "NETWORK_DEVICE_TYPES_description" : "Which device types are allowed to be used as network devices in the Network view. The device type has to match exactly the Type setting on a specific device in Device details. Do not remove existing types, only add new ones.", "UI_LANG_name" : "UI Language", "UI_LANG_description" : "Select the preferred UI language.", "UI_PRESENCE_name" : "Show in presence chart", diff --git a/pialert/initialise.py b/pialert/initialise.py index d253f6cd..e2e7f9b3 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -91,6 +91,8 @@ def importConfigs (db): # Import setting if found in the dictionary # General + # ---------------------------------------- + conf.LOG_LEVEL = ccd('LOG_LEVEL', 'verbose' , c_d, 'Log verboseness', 'text.select', "['none', 'minimal', 'verbose', 'debug']", 'General') conf.TIMEZONE = ccd('TIMEZONE', 'Europe/Berlin' , c_d, 'Time zone', 'text', '', 'General') conf.PLUGINS_KEEP_HIST = ccd('PLUGINS_KEEP_HIST', 250 , c_d, 'Keep history entries', 'integer', '', 'General') @@ -104,10 +106,14 @@ def importConfigs (db): conf.DAYS_TO_KEEP_EVENTS = ccd('DAYS_TO_KEEP_EVENTS', 90 , c_d, 'Delete events days', 'integer', '', 'General') conf.HRS_TO_KEEP_NEWDEV = ccd('HRS_TO_KEEP_NEWDEV', 0 , c_d, 'Keep new devices for', 'integer', "0", 'General') conf.API_CUSTOM_SQL = ccd('API_CUSTOM_SQL', 'SELECT * FROM Devices WHERE dev_PresentLastScan = 0' , c_d, 'Custom endpoint', 'text', '', 'General') + conf.NETWORK_DEVICE_TYPES = ccd('NETWORK_DEVICE_TYPES', ['AP', 'Gateway', 'Firewall', 'Hypervisor', 'Powerline', 'Switch', 'WLAN', 'PLC', 'Router','USB LAN Adapter', 'USB WIFI Adapter', 'Internet'] , c_d, 'Network device types', 'list', '', 'General') - # ARPSCAN (+ other settings provided by the ARPSCAN plugin) + # ARPSCAN (+ more settings are provided by the ARPSCAN plugin) conf.SCAN_SUBNETS = ccd('SCAN_SUBNETS', ['192.168.1.0/24 --interface=eth1', '192.168.1.0/24 --interface=eth0'] , c_d, 'Subnets to scan', 'subnets', '', 'ARPSCAN') + # Notification gateways + # ---------------------------------------- + # Email conf.REPORT_MAIL = ccd('REPORT_MAIL', False , c_d, 'Enable email', 'boolean', '', 'Email', ['test']) conf.SMTP_SERVER = ccd('SMTP_SERVER', '' , c_d,'SMTP server URL', 'text', '', 'Email')