From f87ea210c74c582f1bd2059a01098ed4be42156b Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Thu, 12 Oct 2023 21:45:05 +1100 Subject: [PATCH] =?UTF-8?q?Docs=20+=20Device=20list=20rework=20v0.2=20?= =?UTF-8?q?=F0=9F=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/devices.php | 510 ++++++++++++++++++++++------------------------ pialert/const.py | 11 +- 2 files changed, 248 insertions(+), 273 deletions(-) diff --git a/front/devices.php b/front/devices.php index 0bed1829..07ed2e51 100755 --- a/front/devices.php +++ b/front/devices.php @@ -42,7 +42,7 @@
- +

--

@@ -54,7 +54,7 @@
- +

--

@@ -66,7 +66,7 @@
- +

--

@@ -78,7 +78,7 @@
- +

--

@@ -90,7 +90,7 @@
- +

--

@@ -102,7 +102,7 @@
- +

--

@@ -236,24 +236,24 @@ function main () { //initialize the table headers in the correct order - var headersDefaultOrder = [ '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '', - '' + var headersDefaultOrder = [ getString('Device_TableHead_Name'), + getString('Device_TableHead_Owner'), + getString('Device_TableHead_Type'), + getString('Device_TableHead_Icon'), + getString('Device_TableHead_Favorite'), + getString('Device_TableHead_Group'), + getString('Device_TableHead_FirstSession'), + getString('Device_TableHead_LastSession'), + getString('Device_TableHead_LastIP'), + getString('Device_TableHead_MAC'), + getString('Device_TableHead_Status'), + getString('Device_TableHead_MAC_full'), + getString('Device_TableHead_LastIPOrder'), + getString('Device_TableHead_Rowid'), + getString('Device_TableHead_Parent_MAC'), + getString('Device_TableHead_Connected_Devices'), + getString('Device_TableHead_Location'), + getString('Device_TableHead_Vendor') ]; html = ''; @@ -290,8 +290,7 @@ function main () { initializeDatatable(); // query data - getDevicesTotals(); - getDevicesList (deviceStatus); + getDevicesTotals(); }); }); }); @@ -313,9 +312,53 @@ function mapIndx(oldIndex) } } +// ----------------------------------------------------------------------------- +// Define a function to filter data based on deviceStatus +function filterDataByStatus(data, status) { + return data.filter(function(item) { + switch (status) { + case 'all': + return true; // Include all items for 'all' status + case 'connected': + return item.dev_PresentLastScan === 1; + case 'favorites': + return item.dev_Favorite === 1; + case 'new': + return item.dev_NewDevice === 1; + case 'down': + return item.dev_PresentLastScan === 0 && item.dev_AlertDeviceDown === 1; + case 'archived': + return item.dev_Archived === 1; + default: + return true; // Include all items for unknown statuses + } + }); +} // ----------------------------------------------------------------------------- -function initializeDatatable () { +function initializeDatatable (status) { + + // Save status selected + deviceStatus = status; + + // Define color & title for the status selected + switch (deviceStatus) { + case 'all': tableTitle = getString('Device_Shortcut_AllDevices'); color = 'aqua'; break; + case 'connected': tableTitle = getString('Device_Shortcut_Connected'); color = 'green'; break; + case 'favorites': tableTitle = getString('Device_Shortcut_Favorites'); color = 'yellow'; break; + case 'new': tableTitle = getString('Device_Shortcut_NewDevices'); color = 'yellow'; break; + case 'down': tableTitle = getString('Device_Shortcut_DownAlerts'); color = 'red'; break; + case 'archived': tableTitle = getString('Device_Shortcut_Archived'); color = 'gray'; break; + default: tableTitle = getString('Device_Shortcut_Devices'); color = 'gray'; break; + } + + // Set title and color + $('#tableDevicesTitle')[0].className = 'box-title text-'+ color; + $('#tableDevicesBox')[0].className = 'box box-'+ color; + $('#tableDevicesTitle').html (tableTitle); + + + for(i = 0; i < tableColumnOrder.length; i++) { // hide this column if not in the tableColumnVisible variable (we need to keep the MAC address (index 11) for functionality reasons) @@ -325,188 +368,195 @@ function initializeDatatable () { } } - $.get('api/table_devices.json', function(result) { - // var list = data["data"]; + console.log(tableColumnOrder) + console.log(tableColumnVisible) + console.log(tableColumnHide) - // Convert JSON data into the desired format - var dataArray = { - data: result.data.map(function(item) { - return [ - item.dev_Icon || "", - item.dev_Name || "", - item.dev_Status || "", - item.dev_MAC || "", - item.dev_LastIP || "", - item.dev_Group || "", - item.dev_DeviceType || "", - item.dev_StaticIP || 0, - item.dev_Favorite || "", - item.dev_FirstConnection || "", - item.dev_LastConnection || "", - item.dev_NewDevice || 0, - item.dev_MAC_full || "", - item.connected_devices || 0, - item.dev_Network_Node_MAC_ADDR || "", - item.dev_Network_Node_port || 0, - item.dev_Network_Node_MAC_ADDR || "", - item.dev_Vendor || "", - item.dev_Network_Node_port || "" - ]; - }) - }; + $.get('api/table_devices.json', function(result) { + + // Filter the data based on deviceStatus + var filteredData = filterDataByStatus(result.data, deviceStatus); - console.log("aaaaa") - console.log(JSON.stringify(dataArray)) - console.log(dataArray) - console.log() + // Convert JSON data into the desired format + var dataArray = { + data: filteredData.map(function(item) { + return [ + item.dev_Name || "", + item.dev_Owner || "", + item.dev_DeviceType || "", + item.dev_Icon || "", + item.dev_Favorite || "", + item.dev_Group || "", + item.dev_FirstConnection || "", + item.dev_LastConnection || "", + item.dev_LastIP || "", + item.dev_MAC || "", // TODO handle internet node mac + "status", + item.dev_MAC || "", // hidden + item.dev_StaticIP || 0, + item.dev_LastIP || "", // IP orderable + item.rowid || "", + item.dev_Network_Node_MAC_ADDR || "", + item.connected_devices || 0, + item.dev_Location || "", + item.dev_Vendor || "", + item.dev_Network_Node_port || 0 + ]; + }) + }; + + // TODO displayed columns - // TODO Filter - // TODO displayed columns - // TODO columns order - var table= - $('#tableDevices').DataTable({ - 'data' : dataArray["data"], - 'paging' : true, - 'lengthChange' : true, - 'lengthMenu' : [[10, 25, 50, 100, 500, -1], [10, 25, 50, 100, 500, '']], - 'searching' : true, + // Check if the DataTable already exists + if ($.fn.dataTable.isDataTable('#tableDevices')) { + // The DataTable exists, so destroy it + var table = $('#tableDevices').DataTable(); + table.clear().destroy(); + } - 'ordering' : true, - 'info' : true, - 'autoWidth' : false, + var table= + $('#tableDevices').DataTable({ + 'data' : dataArray["data"], + 'paging' : true, + 'lengthChange' : true, + 'lengthMenu' : [[10, 25, 50, 100, 500, -1], [10, 25, 50, 100, 500, getString('Device_Tablelenght_all')]], + 'searching' : true, - // Parameters - 'pageLength' : tableRows, - 'order' : tableOrder, - // 'order' : [[3,'desc'], [0,'asc']], + 'ordering' : true, + 'info' : true, + 'autoWidth' : false, - 'columnDefs' : [ - {visible: false, targets: tableColumnHide }, - {className: 'text-center', targets: [mapIndx(3), mapIndx(4), mapIndx(9), mapIndx(10), mapIndx(15)] }, - {width: '80px', targets: [mapIndx(6), mapIndx(7), mapIndx(15)] }, - {width: '30px', targets: [mapIndx(10), mapIndx(13)] }, - {orderData: [mapIndx(12)], targets: mapIndx(8) }, + // Parameters + 'pageLength' : tableRows, + 'order' : tableOrder, - // Device Name - {targets: [mapIndx(0)], - 'createdCell': function (td, cellData, rowData, row, col) { - - console.log(cellData) - $(td).html ('
'+ cellData +''); - } }, + 'columnDefs' : [ + {visible: false, targets: tableColumnHide }, + {className: 'text-center', targets: [mapIndx(3), mapIndx(4), mapIndx(9), mapIndx(10), mapIndx(15)] }, + {width: '80px', targets: [mapIndx(6), mapIndx(7), mapIndx(15)] }, + {width: '30px', targets: [mapIndx(10), mapIndx(13)] }, + {orderData: [mapIndx(12)], targets: mapIndx(8) }, - // Connected Devices - {targets: [mapIndx(15)], - 'createdCell': function (td, cellData, rowData, row, col) { - $(td).html (''+ cellData +''); - } }, - - // Icon - {targets: [mapIndx(3)], - 'createdCell': function (td, cellData, rowData, row, col) { - if (!emptyArr.includes(cellData)){ - $(td).html (''); - } else { - $(td).html (''); - } - } }, - - // Full MAC - {targets: [mapIndx(11)], - 'createdCell': function (td, cellData, rowData, row, col) { - if (!emptyArr.includes(cellData)){ - $(td).html (''+cellData+''); - } else { - $(td).html (''); - } - } }, - - // IP address - {targets: [mapIndx(12)], - 'createdCell': function (td, cellData, rowData, row, col) { - if (!emptyArr.includes(cellData)){ - $(td).html (''+cellData+''); - } else { - $(td).html (''); - } - } }, - - // Favorite - {targets: [mapIndx(4)], - 'createdCell': function (td, cellData, rowData, row, col) { - if (cellData == 1){ - $(td).html (''); - } else { - $(td).html (''); - } - } }, + // Device Name + {targets: [mapIndx(0)], + 'createdCell': function (td, cellData, rowData, row, col) { - // Dates - {targets: [mapIndx(6), mapIndx(7)], - 'createdCell': function (td, cellData, rowData, row, col) { - $(td).html (translateHTMLcodes (cellData)); - } }, + console.log(cellData) + $(td).html (''+ cellData +''); + } }, - // Random MAC - {targets: [mapIndx(9)], - 'createdCell': function (td, cellData, rowData, row, col) { - if (cellData == 1){ - $(td).html (''); - } else { - $(td).html (''); - } - } }, + // Connected Devices + {targets: [mapIndx(15)], + 'createdCell': function (td, cellData, rowData, row, col) { + $(td).html (''+ cellData +''); + } }, - // Status color - {targets: [mapIndx(10)], - 'createdCell': function (td, cellData, rowData, row, col) { - switch (cellData) { - case 'Down': color='red'; break; - case 'New': color='yellow'; break; - case 'On-line': color='green'; break; - case 'Off-line': color='gray text-white'; break; - case 'Archived': color='gray text-white'; break; - default: color='aqua'; break; - }; - - $(td).html (''+ cellData.replace('-', '') +''); - } }, - ], + // Icon + {targets: [mapIndx(3)], + 'createdCell': function (td, cellData, rowData, row, col) { + if (!emptyArr.includes(cellData)){ + $(td).html (''); + } else { + $(td).html (''); + } + } }, + + // Full MAC + {targets: [mapIndx(11)], + 'createdCell': function (td, cellData, rowData, row, col) { + if (!emptyArr.includes(cellData)){ + $(td).html (''+cellData+''); + } else { + $(td).html (''); + } + } }, + + // IP address + {targets: [mapIndx(12)], + 'createdCell': function (td, cellData, rowData, row, col) { + if (!emptyArr.includes(cellData)){ + $(td).html (''+cellData+''); + } else { + $(td).html (''); + } + } }, + + // Favorite + {targets: [mapIndx(4)], + 'createdCell': function (td, cellData, rowData, row, col) { + if (cellData == 1){ + $(td).html (''); + } else { + $(td).html (''); + } + } }, - // Processing - 'processing' : true, - 'language' : { - processing: '
Loading...
', - emptyTable: 'No data', - "lengthMenu": "", - "search": ": ", - "paginate": { - "next": "", - "previous": "" - }, - "info": "", - } - }); + // Dates + {targets: [mapIndx(6), mapIndx(7)], + 'createdCell': function (td, cellData, rowData, row, col) { + $(td).html (translateHTMLcodes (cellData)); + } }, - // Save cookie Rows displayed, and Parameters rows & order - $('#tableDevices').on( 'length.dt', function ( e, settings, len ) { - setParameter (parTableRows, len); - } ); - - $('#tableDevices').on( 'order.dt', function () { - setParameter (parTableOrder, JSON.stringify (table.order()) ); - setCache ('devicesList', getDevicesFromTable(table) ); - } ); + // Random MAC + {targets: [mapIndx(9)], + 'createdCell': function (td, cellData, rowData, row, col) { + if (cellData == 1){ + $(td).html (''); + } else { + $(td).html (''); + } + } }, - $('#tableDevices').on( 'search.dt', function () { - setCache ('devicesList', getDevicesFromTable(table) ); - } ); + // Status color + {targets: [mapIndx(10)], + 'createdCell': function (td, cellData, rowData, row, col) { + + console.log(cellData) + switch (cellData) { + case 'Down': color='red'; break; + case 'New': color='yellow'; break; + case 'On-line': color='green'; break; + case 'Off-line': color='gray text-white'; break; + case 'Archived': color='gray text-white'; break; + default: color='aqua'; break; + }; + + // $(td).html (''+ cellData.replace('-', '') +''); + } }, + ], + + // Processing + 'processing' : true, + 'language' : { + processing: '
Loading...
', + emptyTable: 'No data', + "lengthMenu": "", + "search": ": ", + "paginate": { + "next": "", + "previous": "" + }, + "info": "", + } + }); + + // Save cookie Rows displayed, and Parameters rows & order + $('#tableDevices').on( 'length.dt', function ( e, settings, len ) { + setParameter (parTableRows, len); + } ); + + $('#tableDevices').on( 'order.dt', function () { + setParameter (parTableOrder, JSON.stringify (table.order()) ); + setCache ('devicesList', getDevicesFromTable(table) ); + } ); + + $('#tableDevices').on( 'search.dt', function () { + setCache ('devicesList', getDevicesFromTable(table) ); + } ); - }); - - + }); }; @@ -560,82 +610,6 @@ function getDevicesTotals () { } // ----------------------------------------------------------------------------- -function getDevicesList (status) { - // Save status selected - deviceStatus = status; - - // Define color & title for the status selected - switch (deviceStatus) { - case 'all': tableTitle = ''; color = 'aqua'; break; - case 'connected': tableTitle = ''; color = 'green'; break; - case 'favorites': tableTitle = ''; color = 'yellow'; break; - case 'new': tableTitle = ''; color = 'yellow'; break; - case 'down': tableTitle = ''; color = 'red'; break; - case 'archived': tableTitle = ''; color = 'gray'; break; - default: tableTitle = ''; color = 'gray'; break; - } - - // Set title and color - $('#tableDevicesTitle')[0].className = 'box-title text-'+ color; - $('#tableDevicesBox')[0].className = 'box box-'+ color; - $('#tableDevicesTitle').html (tableTitle); - - - - $.get('api/table_devices.json', function(result_1) { - // var list = data["data"]; - - // Convert JSON data into the desired format - var dataArray = { - data: result_1.data.map(function(item) { - return [ - item.dev_Icon || "", - item.dev_Name || "", - item.dev_Status || "", - item.dev_MAC || "", - item.dev_LastIP || "", - item.dev_Group || "", - item.dev_DeviceType || "", - item.dev_StaticIP || 0, - item.dev_Favorite || "", - item.dev_FirstConnection || "", - item.dev_LastConnection || "", - item.dev_NewDevice || 0, - item.dev_MAC_full || "", - item.connected_devices || 0, - item.dev_Network_Node_MAC_ADDR || "", - item.dev_Network_Node_port || 0, - item.dev_Network_Node_MAC_ADDR || "", - item.dev_Vendor || "", - item.dev_Network_Node_port || "" - ]; - }) - }; - - console.log("aaaaa") - console.log(JSON.stringify(dataArray)) - console.log(dataArray) - console.log(dataArray["data"]) - // console.log(result_2) - - // $('#tableDevices').DataTable(); - - // TODO Filter - // TODO displayed columns - // TODO columns order - - // Initialize DataTable with your data - // $('#tableDevices').DataTable(dataArray); - - - }); - - // Define new datasource URL and reload - // $('#tableDevices').DataTable().ajax.url( - // 'php/server/devices.php?action=getDevicesList&status=' + deviceStatus - // ).load(); -}; - function handleLoadingDialog() { $.get('api/app_state.json?nocache=' + Date.now(), function(appState) { diff --git a/pialert/const.py b/pialert/const.py index cd6daef5..67a98349 100755 --- a/pialert/const.py +++ b/pialert/const.py @@ -23,11 +23,12 @@ vendorsPath9 = '/usr/share/arp-scan/ieee-iab.txt' #=============================================================================== # SQL queries #=============================================================================== -sql_devices_all = """select dev_MAC, dev_Name, dev_DeviceType, dev_Vendor, dev_Group, - dev_FirstConnection, dev_LastConnection, dev_LastIP, dev_StaticIP, - dev_PresentLastScan, dev_LastNotification, dev_NewDevice, - dev_Network_Node_MAC_ADDR, dev_Network_Node_port, - dev_Icon from Devices""" +# sql_devices_all = """select dev_MAC, dev_Name, dev_DeviceType, dev_Vendor, dev_Group, +# dev_FirstConnection, dev_LastConnection, dev_LastIP, dev_StaticIP, +# dev_PresentLastScan, dev_LastNotification, dev_NewDevice, +# dev_Network_Node_MAC_ADDR, dev_Network_Node_port, +# dev_Icon from Devices""" +sql_devices_all = """select rowid, * from Devices""" sql_devices_stats = """SELECT Online_Devices as online, Down_Devices as down, All_Devices as 'all', Archived_Devices as archived, (select count(*) from Devices a where dev_NewDevice = 1 ) as new, (select count(*) from Devices a where dev_Name = '(unknown)' or dev_Name = '(name not found)' ) as unknown