Compare commits

...

2 Commits

Author SHA1 Message Date
Ze'ev Schurmann 1e68686e92 Added logwatch.sh 2024-02-05 14:53:08 +02:00
Ze'ev Schurmann 6470c0458c Added logwatch.sh 2024-02-05 14:48:46 +02:00
3 changed files with 102 additions and 0 deletions

View File

@ -27,3 +27,14 @@ Example crontab entry to check for updates every 4 hours:
````
00 */4 * * * root bash /root/scripts/nextcloud-telegram-notify/checkupdates.sh
````
## logwatch.sh
![screenshot](images/logwatch.png)
Run this as a cronjob. It will send you a Telegram every time the log file receives an update, but only at the log level you preselect.
Example crontab entry to check for updates every 15 minutes:
````
*/15 * * * * root bash /root/scripts/nextcloud-telegram-notify/logwatch.sh
````

BIN
images/logwatch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 570 KiB

91
logwatch.sh Normal file
View File

@ -0,0 +1,91 @@
#!/bin/bash
tgapi=""
tgch=""
nextcloudpath="/var/www/html"
wwwuser="www-data"
logmax="250"
logprefix="logwatch"
## Level 0 = DEBUG
## Level 1 = INFO
## Level 2 = WARNING
## Level 3 = ERROR
## Level 4 = FATAL
## Will send everything from selected log level and up.
loglevel=3
errorstring=('"level":0,' '"level":1,' '"level":2,' '"level":3,' '"level":4,')
errorname=('DEBUG' 'INFO' 'WARNING' 'ERROR' 'FATAL')
function sendtelegram {
echo "Nextcloud has new log entries..." > /tmp/nclogentryheader.txt
echo "Server: $(hostname)" >> /tmp/nclogentryheader.txt
echo "Path: $nextcloudpath" >> /tmp/nclogentryheader.txt
echo "Date: $(date)" >> /tmp/nclogentryheader.txt
echo "" >> /tmp/nclogentryheader.txt
string=$(cat /tmp/nclogentryheader.txt /tmp/nclogentry.txt)
wget -qO- "https://api.telegram.org/bot$tgapi/sendMessage?chat_id=$tgch&text=$string" &> /dev/null
errorcode=$?
updatelog $errorcode
}
function updatelog {
logdata="Telegram Sent"
if [[ ! -z $1 ]]; then
if [[ $1 == "0" ]]; then
logerror="Successful!"
else
logerror="Sending Failed: Exit Code $1"
fi
fi
echo "$(date +%y%m%d).$(date +%H%M) - $logprefix - $logdata - $logerror" >> /var/log/nextcloud-telegram-notify.log
logsize=$(cat /var/log/nextcloud-telegram-notify.log | wc -l)
if [[ $logsize -gt $logmax ]]; then
mv /var/log/nextcloud-telegram-notify.log /tmp/nextcloud-telegram-notify.log
tail -n $logmax /tmp/nextcloud-telegram-notify.log > /var/log/nextcloud-telegram-notify.log
rm /tmp/nextcloud-telegram-notify.log
fi
}
sudo -u $wwwuser php "$nextcloudpath/occ" user:list > /tmp/ncchecknewusers.txt
array=($(cat $nextcloudpath/config/config.php | grep 'datadirectory'))
nextcloudfilespath=${array[2]:1:-2}
cp $nextcloudfilespath/nextcloud.log /tmp/nclog.tmp
if [[ -f /tmp/nclog.old ]]; then
rm -f /tmp/nclog.changes.tmp
for ((lvl=4; lvl>$((loglevel-1)); lvl--)); do
cat /tmp/nclog.old /tmp/nclog.old /tmp/nclog.tmp | grep "${errorstring[$lvl]}" | sort | uniq -u >> /tmp/nclog.changes.tmp
done
rm -f /tmp/nclog.new.*.tmp
size=$(cat /tmp/nclog.changes.tmp | wc -l)
if [[ $size -gt 0 ]]; then
n=0
while read -r entry; do
echo "$n" "/tmp/nclog.new.$n.tmp"
echo $entry > "/tmp/nclog.new.$((n++)).tmp"
done < /tmp/nclog.changes.tmp
rm -f /tmp/nclogentry.txt
for ((c=0; c<$n; c++)); do
log_level=$(cat "/tmp/nclog.new.$c.tmp" | jq -r .level)
echo "Log Entry: ${errorname[$log_level]}" >> /tmp/nclogentry.txt
echo "Time: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .time)" >> /tmp/nclogentry.txt
echo "ReqID: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .reqId)" >> /tmp/nclogentry.txt
echo "Remote Address: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .remoteAddr)" >> /tmp/nclogentry.txt
echo "Username: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .user)" >> /tmp/nclogentry.txt
echo "App: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .app)" >> /tmp/nclogentry.txt
echo "Message: $(cat "/tmp/nclog.new.$c.tmp" | jq -r .message)" >> /tmp/nclogentry.txt
echo "" >> /tmp/nclogentry.txt
done
sendtelegram
else
echo "There are no log entries to send..."
fi
else
echo "First run... saving current log..."
fi
mv /tmp/nclog.tmp /tmp/nclog.old