Version: 1.4
Hallo zusammen,
Ich hab mich mal wieder an ein kleines Script gemacht.
Etwas verbesserte Version mit Kommentaren und Anpassungsmöglichkeit... wer Verbesserungen hat, immer her damit
Außerdem ist in der neuen Version Logrotation aktiviert. Aktuell ohne Kompression oder Archivierung.
Und zwar sendet das Script eine E-Mail wenn entweder im Homebridge-Log ein "Hit" auftritt oder der Homebridge-Service nicht läuft. Das ganze funktioniert natürlich unabhängig von der Homebridge!
Ich nutze dafür die Funktion sendEmail - für die Installation bitte dieser Anleitung folgen (ACHTUNG! Die Anleitung ist nicht mehr aktuell, bitte meine Anweisungen im Spoiler nutzen): Raspbian Wheezy: E-Mails vom Raspberry Pi mit sendEmail verschicken
Die Kommands einmal zusammengefasst:
sudo apt-get update
sudo apt-get install sendemail libio-socket-ssl-perl libnet-ssleay-perl
sudo vim /usr/local/bin/mailnotify.sh
####################################################################################################
###########################################################################
## ##
## Sending E-Mail notification with sendEmail ##
## ##
## Creation: 15.08.2013 ##
## Last Update: 20.10.2013 ##
## ##
## Copyright (c) 2013 by Georg Kainzbauer <http://www.gtkdb.de> ##
## ##
## This program is free software; you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation; either version 2 of the License, or ##
## (at your option) any later version. ##
## ##
###########################################################################
#!/bin/bash
# Sender of the mail
SENDER="[email protected]"
# Recipient of the mail
RECIPIENT="[email protected]"
# SMTP server (Port kann je nach Anbieter variieren
SMTPSERVER="smtp.gmail.com:587"
# User name on the SMTP server
SMTPUSERNAME="[email protected]"
# Password on the SMTP server (Ggf. muss hier ein separates App-Passwort generiert werden)
SMTPPASSWORD="abcdefghijkl"
# Enable TLS for the SMTP connection
USETLS=1
###################################################################
# NORMALLY THERE IS NO NEED TO CHANGE ANYTHING BELOW THIS COMMENT #
###################################################################
# Use first argument as mail subject
if [ -n "$1" ]; then
SUBJECT="$1"
else
# No subject specified
SUBJECT=""
fi
# Use second argument as mail body
if [ -n "$2" ]; then
BODY="$2"
else
# No mail body specified
BODY=""
fi
# Generate the options list for sendEmail
OPTIONS=""
if [ -n "${SMTPSERVER}" ]; then
OPTIONS="${OPTIONS} -s ${SMTPSERVER}"
fi
if [ -n "${SMTPUSERNAME}" ]; then
OPTIONS="${OPTIONS} -xu ${SMTPUSERNAME}"
fi
if [ -n "${SMTPPASSWORD}" ]; then
OPTIONS="${OPTIONS} -xp ${SMTPPASSWORD}"
fi
if [ -n "${USETLS}" ]; then
if [ ${USETLS} == 1 ]; then
OPTIONS="${OPTIONS} -o tls=yes"
else
OPTIONS="${OPTIONS} -o tls=no"
fi
fi
# Send the mail with sendEmail
sendEmail -f ${SENDER} -t ${RECIPIENT} -u "${SUBJECT}" -m "${BODY}" ${OPTIONS}
exit 0
####################################################################################################
Alles anzeigen
sudo chmod +x /usr/local/bin/mailnotify.sh
sudo sed -i 's/))$}i/))}i/g' /usr/share/perl5/IO/Socket/SSL.pmsudo sed -i 's/SSLv3 TLSv1/TLSv1_2/g' /usr/bin/sendEmail
Ggf. auch noch dieser Befehle - ich bin mir nicht 100% sicher, in welcher Version es wie in sendEmail geschrieben wird
sudo sed -i 's/SSLv23:!SSLv2/TLSv1_2/g' /usr/bin/sendEmail
ACHTUNG: Bei gmail muss ein "App-Passwort" generiert werden.
Wer Fragen zur Einrichtung hat - ich gebe mein Bestes
Nun zu meinem Script. Der Ordnung wegen habe ich in meinem Home einen Ordner scripts angelegt
Wie auch bei meinen anderen Scripts arbeite ich mit vim anstelle von nano! Bitte etwaige Unterschiede beachten!
vim scripts/check_homebridge.sh
#!/bin/bash
###################################################################
# Version 1.3 #
# Written by Daniel Kaiser #
# Date 26.10.2017 #
# Based on sendEmail - http://www.gtkdb.de/index_36_2296.html #
# Changelog: 26.10.2017 - 1.2: Added Configpart #
# Added Logrotation #
# Mail now contains error #
# 1.3: Logfile wurde nicht geschrieben #
# 1.4: Logrotation ausgebaut #
###################################################################
# Add your Search-String to Variable search - Multiple Strings separated by '\|'
search='Error\|Warning'
# Add your Homebridge-Logpath to Variable log
log=/var/log/homebridge.log
# The logpath-Variable is the log for this script
logpath=/var/log/check_homebridge.log
# The cntfile-Variable contains the counter for hits - to prevent multiple Mails without any new hits
cntfile=/home/pi/scripts/last_cnt.log
# If your Homebridge is running as a Service change this variable to 1 else 0
HBisService=1
# If your Service is not named as "Homebridge" change here
SERVICE=homebridge
# Error in Log - Mailheader
errHeader="Error in Homebridge-Log"
# Homebridge not running - Mailheader
HBHeader="$SERVICE service is not running!"
# Define Logfile-Size
#MaxFileSize=2048
##############################################
# No changes need to be done after this line #
##############################################
# Count $search Hits in $log
num_errors="$( grep -ic $search $log )"
# If Countfile not exist, create with "-1"
if [ ! -s $cntfile ]; then
echo -1 > $cntfile
fi
# If Hits greater 0 enter sendMail-Process
if [ $num_errors != 0 ]; then
cnt=$( cat $cntfile )
# Count new Hits since last run
diff=`expr $num_errors - $cnt `
# If hits differ since last run - send Mail
if [ $num_errors != $cnt ]; then
/usr/local/bin/mailnotify.sh "$errHeader" "$( grep -i $search $log | tail -n $diff )" >> $logpath
echo $num_errors > $cntfile
else
echo "$( date ): No new errors in $log" >> $logpath
fi
fi
# If HBisService is set to 1 enter process to check if Homebridge is running
comp=1
if [ "$HBisService" -eq "$comp" ]; then
# List Processes | ignore grep | grep for Service
if systemctl status $SERVICE | grep running > /dev/null
then
echo "$( date ): $SERVICE service running, everything is fine" >> $logpath
# If no process found, send Mail
else
/usr/local/bin/mailnotify.sh "$HBHeader" "$( grep $log | tail -n 30 )" >> $logpath
fi
fi
###### Logrotation can be done using logrotate.d
################################
# Logfilerotation #
################################
# Get size in bytes**
#file_size=`du -b $logpath | tr -s '\t' ' ' | cut -d' ' -f1`
# If file-size greater than maxfile size enter process
#if [ $file_size -gt $MaxFileSize ];then
# timestamp=`date +%s`
# echo "Logfile moved to $logpath.$timestamp for Logrotation" >> $logpath
# # Move Logfile
# mv $logpath $logpath.$timestamp
# # Create empty logfile
# touch $logpath
#fi
Alles anzeigen
Variablen:
log = Das Log das nach dem Begriff "Error" gescannt wird
cntfile müssen in die gleiche Verzeichnis zeigen, in dem das check_homebridge.sh Script liegt.
Ich habe das Script dann noch in die Crontab von pi eingetragen, damit alle 15 Minuten gecheckt wird
crontab -e
0,15,30,45 * * * * . /home/pi/scripts/check_homebridge.sh > /dev/null # Wichtig ist der Punkt nach den vier Sternen! Sonst funktioniert der Mailversand nicht
Im /home/pi/scripts/check_homebridge.log wird der Output dieses Scripts protokolliert.
Um zu verhindern, dass das log riesig wird, kann man das dem logrotation-daemon bekannt machen
sudo vim /etc/logrotate.d/check_homebridge
/home/pi/scripts/check_homebridge.log
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
invoke-rc.d rsyslog rotate > /dev/null
endscript
}
Alles anzeigen
Liebe Grüße
Daniel
PS: Ich freue mich über Feedback ob das hilfreich ist