This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Author: Igor Andrade | |
# 08/01/2021 | |
# Send alerts for google chat if LDAP server are currently offline, protects agains repeated alerts. | |
# I was used this for a log that are stored on messages from LDAP but can be used for other things. | |
# | |
# | |
######################################################################### | |
# | |
# Constant Vars | |
datetoday=$(date -I | cut -d- -f3 | sed 's/^0//g') | |
catchlogs=$(egrep "(failed to bind|no available LDAP server found)" /var/log/messages | tail -1 | awk {'print $2'}) | |
alerta="[LDAP] Erro: $(hostname) em $(date -I) Cannot Hit LDAP Server" | |
minutes=61 | |
LOCKFILE=/home/LDAP-Alert/lo.lock | |
recents=/home/LDAP-Alert/recents.txt | |
messages=/var/log/messages | |
nduplic=$(egrep "(failed to bind|no available LDAP server found)" $messages | tail -1 | awk {'print $1,$2,$3'}) | |
matchfile=$(egrep "(failed to bind|no available LDAP server found)" $recents | tail -1 | awk {'print $1,$2,$3'}) | |
URL_GCHAT="\ | |
https://chat.googleapis.com/v1/spaces/YOURURLGCHAT/messages?key=AIzaSyDdI0hCZtE6vySjMm\ | |
-WYOURURLGCHATsHI&token=YOURURLGCHAT_4YOURURLGCHAT-YOURURLGCHAT-pYOURURLGCHATzdY7dYOURURLGCHAT" | |
envia_alerta() | |
{ | |
curl -d '{"text": "'"$alerta"'" }' -H 'Content-Type: application/json; charset=UTF-8' "$URL_GCHAT" | |
egrep "(failed to bind|no available LDAP server found)" $messages | tail -1 | tee -a $recents | |
} | |
alerta_dispara() | |
{ | |
if [ $datetoday -eq $catchlogs ] ; then | |
if [ "$nduplic" == "$matchfile" ] ; then | |
echo "This alert alredy sended" | |
else | |
envia_alerta | |
fi | |
else | |
echo "Nothing Happens" | |
exit 1 | |
fi | |
} | |
lockrulez(){ | |
if [ -f $LOCKFILE ]; then | |
echo "Lockfile Exists" | |
filestr=`find $LOCKFILE -mmin +$minutes -print` | |
if [ "$filestr" = "" ]; then | |
echo "Lockfile is $minutes minutes older, script will not proceed" | |
exit 1 | |
else | |
echo "Lockfile is older $minutes minutes..." | |
rm $LOCKFILE | |
fi | |
fi | |
touch $LOCKFILE | |
alerta_dispara | |
} | |
lockrulez |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment