Skip to content

Commit

Permalink
Integration of "shoutrrr"
Browse files Browse the repository at this point in the history
  • Loading branch information
leiweibau committed Oct 1, 2022
1 parent 17abf72 commit 19ffafb
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ JavaScript)*

## Modifications within this Fork
- Only one scan cycle
- Modified scanmethod. If you want to go back to the original method comment line 505 and uncomment line 508 in ~/pialert/back/pialert.py
- Modified scanmethod. If you want to go back to the original method comment line 507 and uncomment line 510 in ~/pialert/back/pialert.py
- Because of the modified scan, the extended scan parameters in the configuration file do not work. For this reason they were removed.
- The Backend has the additional option "cleanup"
- "pialert-cli" that helps to configure login, password and DB migration
Expand Down
96 changes: 57 additions & 39 deletions back/pialert.py
Original file line number Diff line number Diff line change
Expand Up @@ -1249,9 +1249,6 @@ def email_reporting ():
SELECT dev_MAC FROM Devices WHERE dev_AlertEvents = 0
)""")

# Open text Template


# Open text Template
template_file = open(PIALERT_BACK_PATH + '/report_template.txt', 'r')
mail_text = template_file.read()
Expand Down Expand Up @@ -1413,6 +1410,11 @@ def email_reporting ():
send_pushsafer (mail_text)
else :
print (' Skip PUSHSAFER...')
if REPORT_TELEGRAM :
print (' Sending report by Telegram...')
send_telegram (mail_text)
else :
print (' Skip Telegram...')
if REPORT_NTFY :
print (' Sending report by NTFY...')
send_ntfy (mail_text)
Expand All @@ -1421,8 +1423,6 @@ def email_reporting ():
else :
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
Expand All @@ -1442,33 +1442,14 @@ def email_reporting ():

def send_pushsafer (_Text):
# Remove one linebrake between "Server" and the headline of the event type
_Text = _Text.replace('\n\n\n', '\n\n')
_pushsafer_Text = _Text.replace('\n\n\n', '\n\n')
# extract event type headline to use it in the notification headline
findsubheadline = _Text.split('\n')
findsubheadline = _pushsafer_Text.split('\n')
subheadline = findsubheadline[3]
url = 'https://www.pushsafer.com/api'
post_fields = {
"t" : 'Pi.Alert Message - '+subheadline,
"m" : _Text,
"s" : 11,
"v" : 3,
"i" : 148,
"c" : '#ef7f7f',
"d" : 'a',
"u" : REPORT_DASHBOARD_URL,
"ut" : 'Open Pi.Alert',
"k" : PUSHSAFER_TOKEN,
}

requests.post(url, data=post_fields)

#-------------------------------------------------------------------------------

def send_pushsafer_test ():
url = 'https://www.pushsafer.com/api'
post_fields = {
"t" : 'Pi.Alert Message - Test',
"m" : 'Test',
"m" : _pushsafer_Text,
"s" : 11,
"v" : 3,
"i" : 148,
Expand All @@ -1481,7 +1462,6 @@ def send_pushsafer_test ():

requests.post(url, data=post_fields)


#-------------------------------------------------------------------------------

def send_ntfy (_Text):
Expand All @@ -1496,16 +1476,13 @@ def send_ntfy (_Text):

#-------------------------------------------------------------------------------

def send_ntfy_test ():
requests.post("https://ntfy.sh/{}".format(NTFY_TOPIC),
data="Test",
headers={
"Title": "Pi.Alert Notification",
"Click": REPORT_DASHBOARD_URL,
"Priority": "urgent",
"Tags": "warning"
})

def send_telegram (_Text):
# Remove one linebrake between "Server" and the headline of the event type
_telegram_Text = _Text.replace('\n\n\n', '\n\n')
# extract event type headline to use it in the notification headline
findsubheadline = _telegram_Text.split('\n')
subheadline = findsubheadline[3]
stream = os.popen('~/pialert/back/shoutrrr/'+SHOUTRRR_BINARY+'/shoutrrr send --url "'+TELEGRAM_BOT_TOKEN_URL+'" --message "'+_telegram_Text+'" --title "Pi.Alert - '+subheadline+'"')

#===============================================================================
# Test REPORTING
Expand All @@ -1514,7 +1491,6 @@ def email_reporting_test ():
global mail_text
global mail_html


# Reporting section
print ('\nTest Reporting...')
# Open text Template
Expand All @@ -1537,9 +1513,51 @@ def email_reporting_test ():
send_ntfy_test ("mail_text")
else :
print (' Skip NTFY...')

if REPORT_TELEGRAM :
print (' Sending report by Telegram...')
send_telegram_test ()
else :
print (' Skip Telegram...')

return 0

#-------------------------------------------------------------------------------

def send_ntfy_test ():
requests.post("https://ntfy.sh/{}".format(NTFY_TOPIC),
data="Test",
headers={
"Title": "Pi.Alert Notification",
"Click": REPORT_DASHBOARD_URL,
"Priority": "urgent",
"Tags": "warning"
})

#-------------------------------------------------------------------------------

def send_pushsafer_test ():
url = 'https://www.pushsafer.com/api'
post_fields = {
"t" : 'Pi.Alert Message - Test',
"m" : 'Test',
"s" : 11,
"v" : 3,
"i" : 148,
"c" : '#ef7f7f',
"d" : 'a',
"u" : REPORT_DASHBOARD_URL,
"ut" : 'Open Pi.Alert',
"k" : PUSHSAFER_TOKEN,
}

requests.post(url, data=post_fields)

#-------------------------------------------------------------------------------

def send_telegram_test ():
stream = os.popen('../back/shoutrrr/'+SHOUTRRR_BINARY+'/shoutrrr send --url "'+TELEGRAM_BOT_TOKEN_URL+'" --message "Pi.Alert Test" --title "Pi.Alert"')

#-------------------------------------------------------------------------------
def format_report_section (pActive, pSection, pTable, pText, pHTML):
global mail_text
Expand Down
Binary file added back/shoutrrr/arm64/shoutrrr
Binary file not shown.
Binary file added back/shoutrrr/armhf/shoutrrr
Binary file not shown.
Binary file added back/shoutrrr/x64/shoutrrr
Binary file not shown.
Binary file added back/shoutrrr/x86/shoutrrr
Binary file not shown.
9 changes: 9 additions & 0 deletions config/pialert.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ PUSHSAFER_TOKEN = 'ApiKey'
REPORT_NTFY = False
NTFY_TOPIC = 'replace_my_secure_topicname_91h889f28'

# Shoutrrr
# ----------------------
SHOUTRRR_BINARY = 'armhf'

# Telegram via Shoutrrr
# ----------------------
REPORT_TELEGRAM = False
TELEGRAM_BOT_TOKEN_URL = '<Your generated servive URL for telegram - use ~/pialert/back/shoutrrr/<your Systemtyp>/shoutrrr generate telegram>'

# DynDNS
# ----------------------
QUERY_MYIP_SERVER = 'https://myipv4.p1.opendns.com/get_my_ip'
Expand Down
4 changes: 2 additions & 2 deletions config/version.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = '3.7.2_leiweibau'
VERSION = '3.7.3_leiweibau'
VERSION_YEAR = '2022'
VERSION_DATE = '2022-09-25'
VERSION_DATE = '2022-10-01'
4 changes: 4 additions & 0 deletions install/pialert_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@ publish_pialert() {
chmod 770 $PIALERT_HOME/db 2>&1 >> "$LOG"
chmod 770 $PIALERT_HOME/config 2>&1 >> "$LOG"
chmod g+rw $PIALERT_HOME/config/pialert.conf 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/arm64/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/armhf/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/x64/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/x86/shoutrrr 2>&1 >> "$LOG"

print_msg "- Publishing Pi.Alert web..."
sudo ln -s "$PIALERT_HOME/front" "$WEBROOT/pialert" 2>&1 >> "$LOG"
Expand Down
40 changes: 27 additions & 13 deletions install/pialert_update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ main() {

check_packages
download_pialert
# update_config
update_config
# update_db

test_pialert
test_pialert

print_header "Update process finished"
# test_pialert
print_msg ""

move_logfile
Expand Down Expand Up @@ -136,12 +135,23 @@ update_config() {
cp "$PIALERT_HOME/config/pialert.conf" "$PIALERT_HOME/config/pialert.conf.back" 2>&1 >> "$LOG"

print_msg "- Updating config file..."
sed -i '/VERSION/d' "$PIALERT_HOME/config/pialert.conf" 2>&1 >> "$LOG"
sed -i 's/PA_FRONT_URL/REPORT_DEVICE_URL/g' "$PIALERT_HOME/config/pialert.conf" 2>&1 >> "$LOG"

if ! grep -Fq PIALERT_PATH "$PIALERT_HOME/config/pialert.conf" ; then
echo "PIALERT_PATH = '$PIALERT_HOME'" >> "$PIALERT_HOME/config/pialert.conf"
fi
# sed -i '/VERSION/d' "$PIALERT_HOME/config/pialert.conf" 2>&1 >> "$LOG"
# sed -i 's/PA_FRONT_URL/REPORT_DEVICE_URL/g' "$PIALERT_HOME/config/pialert.conf" 2>&1 >> "$LOG"

if ! grep -Fq SHOUTRRR_BINARY "$PIALERT_HOME/config/pialert.conf" ; then
cat << EOF >> "$PIALERT_HOME/config/pialert.conf"
# Shoutrrr
# ----------------------
SHOUTRRR_BINARY = 'armhf'
# Telegram via Shoutrrr
# ----------------------
REPORT_TELEGRAM = False
TELEGRAM_BOT_TOKEN_URL = '<Your generated servive URL for telegram - use ~/pialert/back/shoutrrr/<your Systemtyp>/shoutrrr generate telegram>'
EOF

fi

# if ! grep -Fq QUERY_MYIP_SERVER "$PIALERT_HOME/config/pialert.conf" ; then
# echo "QUERY_MYIP_SERVER = 'http://ipv4.icanhazip.com'" >> "$PIALERT_HOME/config/pialert.conf"
Expand All @@ -159,10 +169,6 @@ update_db() {
print_msg "- Updating DB permissions..."
sudo chgrp -R www-data $PIALERT_HOME/db 2>&1 >> "$LOG"
chmod -R 770 $PIALERT_HOME/db 2>&1 >> "$LOG"
sudo chgrp www-data $PIALERT_HOME/config 2>&1 >> "$LOG"
sudo chgrp www-data $PIALERT_HOME/config/pialert.conf 2>&1 >> "$LOG"
chmod 770 $PIALERT_HOME/config 2>&1 >> "$LOG"
chmod g+rw $PIALERT_HOME/config/pialert.conf 2>&1 >> "$LOG"

print_msg "- Installing sqlite3..."
sudo apt-get install sqlite3 -y 2>&1 >> "$LOG"
Expand Down Expand Up @@ -204,6 +210,14 @@ update_db() {
# Test Pi.Alert
# ------------------------------------------------------------------------------
test_pialert() {
print_msg "- Set Permissions..."
chmod +x $PIALERT_HOME/back/shoutrrr/arm64/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/armhf/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/x64/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/shoutrrr/x86/shoutrrr 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/speedtest-cli 2>&1 >> "$LOG"
chmod +x $PIALERT_HOME/back/pialert-cli 2>&1 >> "$LOG"

print_msg "- Testing Pi.Alert HW vendors database update process..."
print_msg "*** PLEASE WAIT A COUPLE OF MINUTES..."
stdbuf -i0 -o0 -e0 $PYTHON_BIN $PIALERT_HOME/back/pialert.py update_vendors_silent 2>&1 | tee -ai "$LOG"
Expand Down

0 comments on commit 19ffafb

Please sign in to comment.