From cd9e244efd1db1a53b197b1db99c8dfcd8069da7 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Sun, 2 Jul 2023 10:37:21 +1000 Subject: [PATCH] New dev defaults #274 work --- docs/DATABASE.md | 2 +- front/php/templates/language/en_us.php | 16 ++++++++++++++++ front/settings.php | 2 +- pialert/device.py | 25 ++++++++++++------------- pialert/initialise.py | 8 ++++++++ pialert/networkscan.py | 2 +- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/docs/DATABASE.md b/docs/DATABASE.md index bfb8a620..a6bce4a5 100755 --- a/docs/DATABASE.md +++ b/docs/DATABASE.md @@ -10,7 +10,7 @@ | Table name | Description | Sample data | |----------------------|----------------------| ----------------------| | CurrentScan | Result of the current scan | ![Screen1][screen1] | - | Devices | The main devices database that also contains the Network tree mappings. | ![Screen2][screen2] | + | Devices | The main devices database that also contains the Network tree mappings. If `ScanCycle` is set to `0` device is not scanned. | ![Screen2][screen2] | | DHCP_Leases | Used for importing devices from DHCP_Leases files. Also leveraged by some plugins. | ![Screen3][screen3] | | Events | Used to collect connection/disconnection events. | ![Screen4][screen4] | | Nmap_Scan | Contains results of the scheduled Nmap scan, taht is also displayed in the Nmap tab on each device. | ![Screen5][screen5] | diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index ccd5e31a..7f6ca189 100755 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -545,6 +545,22 @@ The arp-scan time itself depends on the number of IP addresses to check so set t 'UI_PRESENCE_name' => 'Show in presence chart', 'UI_PRESENCE_description' => 'Select what statuses should be shown in the Device presence over time chart in the Devices page. (CTRL + Click to select/deselect)', +// New device defaults +'NewDeviceDefaults_display_name' => 'New device defaults', +'NewDeviceDefaults_icon' => '', +'NEWDEV_SCAN_name' => 'Scan device', +'NEWDEV_SCAN_description' => 'The default value of the Scan device dropdown. Select 1 if newly discovered devices should be scanned (this will result in Yes being selected in the dropdown) or 0, if not.', +'NEWDEV_ALERT_ALL_name' => 'Alert All Events', +'NEWDEV_ALERT_ALL_description' => 'The default value of the Alert All Events checkbox. Select 1 if newly discovered devices should have this checkbox selected or 0, if not.', +'NEWDEV_ALERT_DWN_name' => 'Alert Down', +'NEWDEV_ALERT_DWN_description' => 'The default value of the Alert Down checkbox. Select 1 if newly discovered devices should have this checkbox selected or 0, if not.', +'NEWDEV_NEWDEV_name' => 'New Device', +'NEWDEV_NEWDEV_description' => 'The default value of the New Device checkbox. Select 1 if newly discovered devices should have this checkbox selected or 0, if not.', +'NEWDEV_ARCHIVED_name' => 'Archived', +'NEWDEV_ARCHIVED_description' => 'The default value of the Archived checkbox. Select 1 if newly discovered devices should have this checkbox selected or 0, if not.', +'NEWDEV_SKIPNTF_name' => 'Skip repeated notifications for', +'NEWDEV_SKIPNTF_description' => 'The default value of the Skip repeated notifications for dropdown. Select the number of hours for which repeated notifications should be ignored for.', + //Email 'Email_display_name' => 'Email', 'Email_icon' => '', diff --git a/front/settings.php b/front/settings.php index b38359b6..177d52a4 100755 --- a/front/settings.php +++ b/front/settings.php @@ -32,7 +32,7 @@ $result = $db->query("SELECT * FROM Settings"); // array $settingKeyOfLists = array(); -$settingCoreGroups = array('General', 'Email', 'Webhooks', 'Apprise', 'NTFY', 'PUSHSAFER', 'MQTT', 'DynDNS', 'PiHole', 'Pholus', 'Nmap', 'API'); +$settingCoreGroups = array('General', 'NewDeviceDefaults', 'Email', 'Webhooks', 'Apprise', 'NTFY', 'PUSHSAFER', 'MQTT', 'DynDNS', 'PiHole', 'Pholus', 'Nmap', 'API'); $settings = array(); while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { // Push row data diff --git a/pialert/device.py b/pialert/device.py index f712deed..9f638b3e 100755 --- a/pialert/device.py +++ b/pialert/device.py @@ -192,12 +192,11 @@ def create_new_devices (db): # arpscan - Create new devices mylog('debug','[New Devices] 2 Create devices') - sql.execute ("""INSERT INTO Devices (dev_MAC, dev_name, dev_Vendor, + sql.execute (f"""INSERT INTO Devices (dev_MAC, dev_name, dev_Vendor, dev_LastIP, dev_FirstConnection, dev_LastConnection, - dev_ScanCycle, dev_AlertEvents, dev_AlertDeviceDown, - dev_PresentLastScan) + dev_AlertEvents, dev_AlertDeviceDown, dev_PresentLastScan, dev_Archived, dev_NewDevice, dev_SkipRepeated, dev_ScanCycle) SELECT cur_MAC, '(unknown)', cur_Vendor, cur_IP, ?, ?, - 1, 1, 0, 1 + {conf.NEWDEV_ALERT_ALL}, {conf.NEWDEV_ALERT_DWN}, 1, {conf.NEWDEV_ARCHIVED}, {conf.NEWDEV_NEWDEV}, {conf.NEWDEV_SKIPNTF}, {conf.NEWDEV_SCAN} FROM CurrentScan WHERE cur_ScanCycle = ? AND NOT EXISTS (SELECT 1 FROM Devices @@ -221,12 +220,12 @@ def create_new_devices (db): # Pi-hole - Create New Devices # Bugfix #2 - Pi-hole devices w/o IP mylog('debug','[New Devices] 4 Pi-hole Create devices') - sql.execute ("""INSERT INTO Devices (dev_MAC, dev_name, dev_Vendor, + sql.execute (f"""INSERT INTO Devices (dev_MAC, dev_name, dev_Vendor, dev_LastIP, dev_FirstConnection, dev_LastConnection, - dev_ScanCycle, dev_AlertEvents, dev_AlertDeviceDown, - dev_PresentLastScan) + dev_AlertEvents, dev_AlertDeviceDown, dev_PresentLastScan, dev_Archived, dev_NewDevice, dev_SkipRepeated, dev_ScanCycle) SELECT PH_MAC, PH_Name, PH_Vendor, IFNULL (PH_IP,'-'), - ?, ?, 1, 1, 0, 1 + ?, ?, + {conf.NEWDEV_ALERT_ALL}, {conf.NEWDEV_ALERT_DWN}, 1, {conf.NEWDEV_ARCHIVED}, {conf.NEWDEV_NEWDEV}, {conf.NEWDEV_SKIPNTF}, {conf.NEWDEV_SCAN} FROM PiHole_Network WHERE NOT EXISTS (SELECT 1 FROM Devices WHERE dev_MAC = PH_MAC) """, @@ -251,10 +250,9 @@ def create_new_devices (db): # (1610700000, 'TEST1', '10.10.10.1', 'Test 1', '*')""") # sql.execute ("""INSERT INTO DHCP_Leases VALUES # (1610700000, 'TEST2', '10.10.10.2', 'Test 2', '*')""") - sql.execute ("""INSERT INTO Devices (dev_MAC, dev_name, dev_LastIP, - dev_Vendor, dev_FirstConnection, dev_LastConnection, - dev_ScanCycle, dev_AlertEvents, dev_AlertDeviceDown, - dev_PresentLastScan) + sql.execute (f"""INSERT INTO Devices (dev_MAC, dev_name, dev_LastIP, + dev_Vendor, dev_FirstConnection, dev_LastConnection, + dev_AlertEvents, dev_AlertDeviceDown, dev_PresentLastScan, dev_Archived, dev_NewDevice, dev_SkipRepeated, dev_ScanCycle) SELECT DISTINCT DHCP_MAC, (SELECT DHCP_Name FROM DHCP_Leases AS D2 WHERE D2.DHCP_MAC = D1.DHCP_MAC @@ -262,7 +260,8 @@ def create_new_devices (db): (SELECT DHCP_IP FROM DHCP_Leases AS D2 WHERE D2.DHCP_MAC = D1.DHCP_MAC ORDER BY DHCP_DateTime DESC LIMIT 1), - '(unknown)', ?, ?, 1, 1, 0, 1 + '(unknown)', ?, ?, + {conf.NEWDEV_ALERT_ALL}, {conf.NEWDEV_ALERT_DWN}, 1, {conf.NEWDEV_ARCHIVED}, {conf.NEWDEV_NEWDEV}, {conf.NEWDEV_SKIPNTF}, {conf.NEWDEV_SCAN} FROM DHCP_Leases AS D1 WHERE NOT EXISTS (SELECT 1 FROM Devices WHERE dev_MAC = DHCP_MAC) """, diff --git a/pialert/initialise.py b/pialert/initialise.py index 29ad1a71..93774048 100755 --- a/pialert/initialise.py +++ b/pialert/initialise.py @@ -85,6 +85,14 @@ def importConfigs (db): conf.UI_LANG = ccd('UI_LANG', 'English' , c_d, 'Language Interface', 'selecttext', "['English', 'German', 'Spanish']", 'General') conf.UI_PRESENCE = ccd('UI_PRESENCE', ['online', 'offline', 'archived'] , c_d, 'Include in presence', 'multiselect', "['online', 'offline', 'archived']", 'General') + # New device defaults + conf.NEWDEV_SCAN = ccd('NEWDEV_SCAN', 1 , c_d, 'Scan Device', 'selectinteger', "['0', '1']", 'NewDeviceDefaults') + conf.NEWDEV_ALERT_ALL = ccd('NEWDEV_ALERT_ALL', 0 , c_d, 'Alert All Events', 'selectinteger', "['0', '1']", 'NewDeviceDefaults') + conf.NEWDEV_ALERT_DWN = ccd('NEWDEV_ALERT_DWN', 0 , c_d, 'Alert Down', 'selectinteger', "['0', '1']", 'NewDeviceDefaults') + conf.NEWDEV_NEWDEV = ccd('NEWDEV_NEWDEV', 1 , c_d, 'New Device', 'selectinteger', "['0', '1']", 'NewDeviceDefaults') + conf.NEWDEV_ARCHIVED = ccd('NEWDEV_ARCHIVED', 0 , c_d, 'Archived', 'selectinteger', "['0', '1']", 'NewDeviceDefaults') + conf.NEWDEV_SKIPNTF = ccd('NEWDEV_SKIPNTF', 0 , c_d, 'Skip repeated notifications for', 'selectinteger', "['0', '1', '8', '24', '168']", 'NewDeviceDefaults') + # 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') diff --git a/pialert/networkscan.py b/pialert/networkscan.py index 1f07b692..b8bb7f19 100755 --- a/pialert/networkscan.py +++ b/pialert/networkscan.py @@ -277,7 +277,7 @@ def insert_events (db): eve_EventType, eve_AdditionalInfo, eve_PendingAlertEmail) SELECT dev_MAC, dev_LastIP, ?, 'Device Down', '', 1 - FROM Devices + FROM Devices WHERE dev_AlertDeviceDown = 1 AND dev_PresentLastScan = 1 AND dev_ScanCycle = ?