Skip to content

Commit

Permalink
Merge pull request #378 from leiweibau/next_update
Browse files Browse the repository at this point in the history
Next update
  • Loading branch information
leiweibau authored Sep 1, 2024
2 parents 8dd991e + b500f0e commit 2dd449d
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 52 deletions.
3 changes: 3 additions & 0 deletions back/pialert-cli
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ EOM
echo "Configured Pi.Alert scans are disabled"
if [ "$CURRENT_USER" != "www-data" ]; then
sqlite3 $PIA_DB_FILE "INSERT INTO 'pialert_journal' ('Journal_DateTime', 'LogClass', 'Trigger', 'LogString', 'Hash', 'Additional_Info') VALUES('$(date '+%Y-%m-%d %H:%M:%S')', 'b_002', 'pialert-cli', 'LogStr_0511', '', '${STOPTIMER} min');"
if [ $? -ne 0 ]; then
echo "Failed to write to the journal, but continuing."
fi
fi

;;
Expand Down
47 changes: 33 additions & 14 deletions back/pialert.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ def scan_network():
arpscan_devices = execute_arpscan()
print_log ('arp-scan ends')
# Pi-hole
print(' Pi-hole Method...')
print(f" Pi-hole {PIHOLE_VERSION} Method...")
openDB()
print_log ('Pi-hole copy starts...')
copy_pihole_network()
Expand Down Expand Up @@ -947,6 +947,9 @@ def copy_pihole_network():
print(' ...Skipped (Config Error)')
return

if not PIHOLE6_URL.endswith('/'):
PIHOLE6_URL += '/'

headers = {
"accept": "application/json",
"content-type": "application/json"
Expand All @@ -955,7 +958,7 @@ def copy_pihole_network():
"password": PIHOLE6_PASSWORD
}
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
response = requests.post(PIHOLE6_URL+'/api/auth', headers=headers, json=data, verify=False)
response = requests.post(PIHOLE6_URL+'api/auth', headers=headers, json=data, verify=False)
# print(response.json())
response_json = response.json()

Expand All @@ -964,7 +967,7 @@ def copy_pihole_network():
"X-FTL-SID": response_json['session']['sid'],
"X-FTL-CSRF": response_json['session']['csrf']
}
raw_deviceslist = requests.get(PIHOLE6_URL+'/api/network/devices?max_devices=10&max_addresses=2', headers=headers, json=data, verify=False)
raw_deviceslist = requests.get(PIHOLE6_URL+'api/network/devices?max_devices=10&max_addresses=2', headers=headers, json=data, verify=False)

result = {}
deviceslist = raw_deviceslist.json()
Expand Down Expand Up @@ -2992,6 +2995,17 @@ def email_reporting():
global mail_text
global mail_html

# Get Notification Preset Configuration from config file
try:
preset_events = NEW_DEVICE_PRESET_EVENTS
except NameError:
preset_events = True

try:
preset_down = NEW_DEVICE_PRESET_DOWN
except NameError:
preset_down = False

# Reporting section
print('\nReporting...')
openDB()
Expand Down Expand Up @@ -3094,9 +3108,9 @@ def email_reporting():
'Source: ', sat_name,
'More Info: ', eventAlert['eve_AdditionalInfo'])
mail_html_new_devices += html_line_template.format (
REPORT_DEVICE_URL, eventAlert['eve_MAC'], eventAlert['eve_MAC'],
REPORT_DEVICE_URL, eventAlert['eve_MAC'], eventAlert['dev_Name'],
eventAlert['eve_DateTime'], eventAlert['eve_IP'],
eventAlert['dev_Name'], eventAlert['eve_AdditionalInfo'], sat_name)
eventAlert['eve_MAC'], eventAlert['eve_AdditionalInfo'], sat_name)

format_report_section (mail_section_new_devices, 'SECTION_NEW_DEVICES',
'TABLE_NEW_DEVICES', mail_text_new_devices, mail_html_new_devices)
Expand Down Expand Up @@ -3134,9 +3148,9 @@ def email_reporting():
'Source: ', sat_name,
'IP: ', eventAlert['eve_IP'])
mail_html_devices_down += html_line_template.format (
REPORT_DEVICE_URL, eventAlert['eve_MAC'], eventAlert['eve_MAC'],
REPORT_DEVICE_URL, eventAlert['eve_MAC'], eventAlert['dev_Name'],
eventAlert['eve_DateTime'], eventAlert['eve_IP'],
eventAlert['dev_Name'], sat_name)
eventAlert['eve_MAC'], sat_name)

format_report_section (mail_section_devices_down, 'SECTION_DEVICES_DOWN',
'TABLE_DEVICES_DOWN', mail_text_devices_down, mail_html_devices_down)
Expand Down Expand Up @@ -3177,9 +3191,9 @@ def email_reporting():
'Source: ', sat_name,
'More Info: ', eventAlert['eve_AdditionalInfo'])
mail_html_events += html_line_template.format (
REPORT_DEVICE_URL, eventAlert['eve_MAC'], eventAlert['eve_MAC'],
REPORT_DEVICE_URL, eventAlert['eve_MAC'], eventAlert['dev_Name'],
eventAlert['eve_DateTime'], eventAlert['eve_IP'],
eventAlert['eve_EventType'], eventAlert['dev_Name'], sat_name,
eventAlert['eve_EventType'], eventAlert['eve_MAC'], sat_name,
eventAlert['eve_AdditionalInfo'])

format_report_section (mail_section_events, 'SECTION_EVENTS',
Expand All @@ -3194,12 +3208,17 @@ def email_reporting():
print(' No changes to report...')

# Clean Pending Alert Events
sql.execute ("""UPDATE Devices SET dev_LastNotification = ?
WHERE dev_MAC IN (SELECT eve_MAC FROM Events
sql.execute("""UPDATE Devices SET dev_LastNotification = ?
WHERE dev_MAC IN (SELECT eve_MAC FROM Events
WHERE eve_PendingAlertEmail = 1)
""", (datetime.datetime.now(),) )
sql.execute ("""UPDATE Events SET eve_PendingAlertEmail = 0
WHERE eve_PendingAlertEmail = 1""")
""", (datetime.datetime.now(),))
sql.execute("""UPDATE Events SET eve_PendingAlertEmail = 0
WHERE eve_PendingAlertEmail = 1""")

# Set Notification Presets
sql.execute("""UPDATE Devices SET dev_AlertEvents = ?, dev_AlertDeviceDown = ?
WHERE dev_NewDevice = 1
""",(preset_events, preset_down,))

print(' Notifications:', sql.rowcount)

Expand Down
12 changes: 6 additions & 6 deletions back/report_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@

<table width=100% border=1 bordercolor=#C0C0C0 cellpadding=3px cellspacing=0 style="border-collapse: collapse; font-size: 12px; color:#707070">
<tr bgcolor=#909090 style="color:#F0F0F0">
<th width=150> MAC </th>
<th width=150> Device Name </th>
<th width=130> Datetime </th>
<th width=100> IP </th>
<th width=140> Device Name </th>
<th width=140> MAC </th>
<th> Vendor </th>
<th> Source </th>
</tr>
Expand All @@ -87,10 +87,10 @@

<table width=100% border=1 bordercolor=#C0C0C0 cellpadding=3px cellspacing=0 style="border-collapse: collapse; font-size: 12px; color:#707070">
<tr bgcolor=#909090 style="color:#F0F0F0">
<th width=140> MAC </th>
<th width=140> Device Name </th>
<th width=130> Datetime </th>
<th width=100> IP </th>
<th> Device Name </th>
<th> MAC </th>
<th> Source </th>
</tr>

Expand All @@ -105,11 +105,11 @@

<table width=100% border=1 bordercolor=#C0C0C0 cellpadding=3px cellspacing=0 style="border-collapse: collapse; font-size: 12px; color:#707070">
<tr bgcolor=#909090 style="color:#F0F0F0">
<th width=140> MAC </th>
<th width=140> Device Name </th>
<th width=130> Datetime </th>
<th width=100> IP </th>
<th width=100> Event Type </th>
<th width=140> Device Name </th>
<th width=140> MAC </th>
<th> Source </th>
<th> Additional Info </th>
</tr>
Expand Down
28 changes: 15 additions & 13 deletions config/pialert.conf
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# General Settings
# ----------------------
PIALERT_PATH = '/home/pi/pialert'
DB_PATH = PIALERT_PATH + '/db/pialert.db'
LOG_PATH = PIALERT_PATH + '/log'
PRINT_LOG = False
VENDORS_DB = '/usr/share/arp-scan/ieee-oui.txt'
PIALERT_APIKEY = ''
PIALERT_WEB_PROTECTION = False
PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
NETWORK_DNS_SERVER = 'localhost'
AUTO_UPDATE_CHECK = True
AUTO_DB_BACKUP = False
AUTO_DB_BACKUP_KEEP = 5
REPORT_NEW_CONTINUOUS = False
PIALERT_PATH = '/home/pi/pialert'
DB_PATH = PIALERT_PATH + '/db/pialert.db'
LOG_PATH = PIALERT_PATH + '/log'
PRINT_LOG = False
VENDORS_DB = '/usr/share/arp-scan/ieee-oui.txt'
PIALERT_APIKEY = ''
PIALERT_WEB_PROTECTION = False
PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
NETWORK_DNS_SERVER = 'localhost'
AUTO_UPDATE_CHECK = True
AUTO_DB_BACKUP = False
AUTO_DB_BACKUP_KEEP = 5
REPORT_NEW_CONTINUOUS = False
NEW_DEVICE_PRESET_EVENTS = True
NEW_DEVICE_PRESET_DOWN = False

# Other Modules
# ----------------------
Expand Down
2 changes: 1 addition & 1 deletion config/version.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = ''
VERSION_YEAR = '2024'
VERSION_DATE = '2024-08-24'
VERSION_DATE = '2024-09-01'
33 changes: 20 additions & 13 deletions front/devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,10 @@ function BulkDeletion()
<input class="checkbox blue" id="chkFilterMac" type="checkbox">
<label class="control-label" style="margin-left: 5px">' . $pia_lang['Device_TableHead_MACaddress'] .'</label>
</div>
<div class="table_settings_col_box" style="width:180px;">
<input class="checkbox blue" id="chkFilterVendor" type="checkbox">
<label class="control-label" style="margin-left: 5px">' . $pia_lang['DevDetail_MainInfo_Vendor'] .'</label>
</div>
<div class="table_settings_col_box" style="width:180px;">
<input class="checkbox blue" id="chkFilterConnectionType" type="checkbox">
<label class="control-label" style="margin-left: 5px">' . $pia_lang['Device_TableHead_ConnectionType'] .'</label>
Expand Down Expand Up @@ -682,9 +686,10 @@ function BulkDeletion()
if ($table_config['LastIP'] == 0) {$devlistcol_hide .= '9, ';}
if ($table_config['MACType'] == 0) {$devlistcol_hide .= '10, ';}
if ($table_config['MACAddress'] == 0) {$devlistcol_hide .= '11, ';}
if ($table_config['WakeOnLAN'] == 0) {$devlistcol_hide .= '16, ';}
if ($table_config['MACVendor'] == 0) {$devlistcol_hide .= '12, ';}
if ($table_config['WakeOnLAN'] == 0) {$devlistcol_hide .= '17, ';}
?>
<th><?=$pia_lang['Device_TableHead_Name'];?></th>
<th><?=$pia_lang['Device_TableHead_Name'];?></th>
<th><?=$pia_lang['Device_TableHead_ConnectionType'];?></th>
<th><?=$pia_lang['Device_TableHead_Owner'];?></th>
<th><?=$pia_lang['Device_TableHead_Type'];?></th>
Expand All @@ -696,6 +701,7 @@ function BulkDeletion()
<th style="white-space: nowrap;"><?=$pia_lang['Device_TableHead_LastIP'];?></th>
<th><?=$pia_lang['Device_TableHead_MAC'];?></th>
<th style="white-space: nowrap;"><?=$pia_lang['Device_TableHead_MACaddress'];?></th>
<th><?=$pia_lang['DevDetail_MainInfo_Vendor'];?></th>
<th><?=$pia_lang['Device_TableHead_Status'];?></th>
<th><?=$pia_lang['Device_TableHead_LastIPOrder'];?></th>
<th>ScanSource</th>
Expand Down Expand Up @@ -789,12 +795,12 @@ function initializeDatatable () {
// 'order' : [[3,'desc'], [0,'asc']],

'columnDefs' : [
{visible: false, targets: [<?=$devlistcol_hide;?>13, 14, 15] },
{className: 'text-center', targets: [4, 9, 10, 11, 12, 16] },
{visible: false, targets: [<?=$devlistcol_hide;?>14, 15, 16] },
{className: 'text-center', targets: [4, 9, 10, 11, 13, 17] },
{width: '100px', targets: [7, 8] },
{width: '30px', targets: [10] },
{width: '0px', targets: [12] },
{width: '20px', targets: [16] },
{width: '0px', targets: [13] },
{width: '20px', targets: [17] },
{orderData: [13], targets: [9] },
{ "targets": [<?=$_REQUEST['filter_fields'];?>], "searchable": false },

Expand Down Expand Up @@ -836,9 +842,9 @@ function initializeDatatable () {
$(td).html (rowData[11]);
} },
// Status color
{targets: [12],
{targets: [13],
'createdCell': function (td, cellData, rowData, row, col) {
switch (rowData[12]) {
switch (rowData[13]) {
case 'Down': color='red'; statusname='Down'; break;
case 'NewON': color='grad-green-yellow'; statusname='&nbsp;&nbsp;New&nbsp;&nbsp;'; break;
case 'NewOFF': color='grad-gray-yellow'; statusname='&nbsp;&nbsp;New&nbsp;&nbsp;'; break;
Expand All @@ -858,7 +864,7 @@ function initializeDatatable () {
var includeValues = ["Mini PC", "Server", "Laptop", "NAS", "PC"];

if (includeValues.indexOf(row[3]) !== -1 && row[11] !== "Internet") {
return '<a href="#" onclick="askwakeonlan(\'' + row[11] + '\', \'' + row[9] + '\')"><i class="fa-solid fa-power-off text-red"></i></a>';
return '<a href="#" onclick="askwakeonlan(\'' + row[11] + '\',\'' + row[9] + '\', \'' + row[0] + '\')"><i class="fa-solid fa-power-off text-red"></i></a>';
} else {
return '';
}
Expand Down Expand Up @@ -888,11 +894,11 @@ function initializeDatatable () {

$('#tableDevices').on( 'order.dt', function () {
setParameter (parTableOrder, JSON.stringify (table.order()) );
setCookie ('devicesList',JSON.stringify (table.column(15, { 'search': 'applied' }).data().toArray()) );
setCookie ('devicesList',JSON.stringify (table.column(16, { 'search': 'applied' }).data().toArray()) );
} );

$('#tableDevices').on( 'search.dt', function () {
setCookie ('devicesList', JSON.stringify (table.column(15, { 'search': 'applied' }).data().toArray()) );
setCookie ('devicesList', JSON.stringify (table.column(16, { 'search': 'applied' }).data().toArray()) );
} );

};
Expand Down Expand Up @@ -945,10 +951,10 @@ function getDevicesList (status) {
};

// WakeonLAN
function askwakeonlan(fmac,fip) {
function askwakeonlan(fmac,fip,fname) {
window.global_fmac = fmac;
window.global_fip = fip;
showModalWarning('<?=$pia_lang['DevDetail_Tools_WOL_noti'];?>', '<?=$pia_lang['DevDetail_Tools_WOL_noti_text'];?>',
showModalWarning('<?=$pia_lang['DevDetail_Tools_WOL_noti'];?> (<span class="text-red">' + fname + '</span>)', '<?=$pia_lang['DevDetail_Tools_WOL_noti_text'];?>',
'<?=$pia_lang['Gen_Cancel'];?>', '<?=$pia_lang['Gen_Run'];?>', 'wakeonlan');
}
function wakeonlan() {
Expand Down Expand Up @@ -987,6 +993,7 @@ function SetDeviceFilter() {
+ '&ftype=' + ($('#chkFilterType')[0].checked * 1)
+ '&fip=' + ($('#chkFilterIP')[0].checked * 1)
+ '&fmac=' + ($('#chkFilterMac')[0].checked * 1)
+ '&fvendor=' + ($('#chkFilterVendor')[0].checked * 1)
+ '&fconnectiont=' + ($('#chkFilterConnectionType')[0].checked * 1)
, function(msg) {
showMessage (msg);
Expand Down
5 changes: 5 additions & 0 deletions front/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@
<input class="checkbox blue" id="chkMACaddress" type="checkbox" <?=$col_checkbox['MACAddress'];?>>
<label class="control-label" style="margin-left: 5px"><?=$pia_lang['Device_TableHead_MAC'];?>-Address</label>
</div>
<div class="table_settings_col_box">
<input class="checkbox blue" id="chkMACVendor" type="checkbox" <?=$col_checkbox['MACVendor'];?>>
<label class="control-label" style="margin-left: 5px">Vendor</label>
</div>
<div class="table_settings_col_box">
<input class="checkbox blue" id="chkWakeOnLAN" type="checkbox" <?=$col_checkbox['WakeOnLAN'];?>>
<label class="control-label" style="margin-left: 5px"><?=$pia_lang['Device_TableHead_WakeOnLAN'];?> (WakeOnLAN)</label>
Expand Down Expand Up @@ -1091,6 +1095,7 @@ function setDeviceListCol() {
+ '&lastip=' + ($('#chklastIP')[0].checked * 1)
+ '&mactype=' + ($('#chkMACtype')[0].checked * 1)
+ '&macaddress=' + ($('#chkMACaddress')[0].checked * 1)
+ '&macvendor=' + ($('#chkMACVendor')[0].checked * 1)
+ '&location=' + ($('#chkLocation')[0].checked * 1)
+ '&wakeonlan=' + ($('#chkWakeOnLAN')[0].checked * 1)
, function(msg) {
Expand Down
9 changes: 7 additions & 2 deletions front/php/server/devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ function SetDeviceFilter() {
if ($_REQUEST['ftype'] == 0) {array_push($colfilterarray, "3");}
if ($_REQUEST['fip'] == 0) {array_push($colfilterarray, "9");}
if ($_REQUEST['fmac'] == 0) {array_push($colfilterarray, "11");}
if ($_REQUEST['fvendor'] == 0) {array_push($colfilterarray, "12");}
if ($_REQUEST['fconnectiont'] == 0) {array_push($colfilterarray, "1");}

$newcolfilter = implode(",", $colfilterarray);
Expand Down Expand Up @@ -568,10 +569,11 @@ function getDevicesTotals() {
(SELECT COUNT(*) FROM Devices ' . getDeviceCondition('favorites',$scansource) . ') as favorites,
(SELECT COUNT(*) FROM Devices ' . getDeviceCondition('new',$scansource) . ') as new,
(SELECT COUNT(*) FROM Devices ' . getDeviceCondition('down',$scansource) . ') as down,
(SELECT COUNT(*) FROM Devices ' . getDeviceCondition('archived',$scansource) . ') as archived
(SELECT COUNT(*) FROM Devices ' . getDeviceCondition('archived',$scansource) . ') as archived,
(SELECT COUNT(*) FROM Devices ' . getDeviceCondition('presence',$scansource) . ') as presence
');
$row = $result->fetchArray(SQLITE3_NUM);
echo json_encode(array($row[0], $row[1], $row[2], $row[3], $row[4], $row[5]));
echo json_encode(array($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6]));
}

// Query the List of devices in a determined Status
Expand Down Expand Up @@ -603,6 +605,7 @@ function getDevicesList() {
$row['dev_LastIP'],
(in_array($row['dev_MAC'][1], array("2", "6", "A", "E", "a", "e")) ? 1 : 0),
$row['dev_MAC'], // MAC (hidden)
$row['dev_Vendor'],
$row['dev_Status'],
formatIPlong($row['dev_LastIP']), // IP orderable
$row['dev_ScanSource'],
Expand Down Expand Up @@ -827,6 +830,8 @@ function getDeviceCondition($deviceStatus, $scansource) {
break;
case 'archived':return 'WHERE '.$scansource_query.'dev_Archived=1';
break;
case 'presence':return 'WHERE '.$scansource_query.'dev_Archived=0 AND dev_PresencePage=1';
break;
default:return 'WHERE '.$scansource_query.'AND 1=0';
break;
}
Expand Down
Loading

0 comments on commit 2dd449d

Please sign in to comment.