Sunday, August 27, 2017

Email Notification scripts


Using mailx client:


Step1:

sudo yum install -y mailx
sudo yum install -y  shareutils (this is to install uuencode).


Step2:

Make sure, you check the status:
 sudo service postfix status



Step3:

Example:

#!/bin/bash

# Exited Status of the containernameapp
ping=$(docker ps -aq -f name=containernameapp -f status=exited)
#echo $ping

#Logs of the exited container
#docker logs "$ping" 2>&1 |tee /tmp/upo.txt  ---> T line
docker logs "$ping > /tmp/you.txt  2>&1   ----> N line 


#Email notification
if [ "$status" = "Up" ]; then echo "test mail" | mailx -s "container is running"abcs@gmail.com; else uuencode /tmp/you.txt /tmp/you.txt | cat "/tmp/you.txt"| mailx -s "docker status failed" -a "/tmp/you.txt"abcs@gmail.com ; fi

(or) 

When multiple containers are exited status:

#!/bin/bash

# Exited Status of the
containernameapp
#estatus=$(docker ps -aq -f name=
containernameapp -f status=exited)
docker ps -aq -f name=
containernameapp -f status=exited >/tmp/you.txt
for docID in `cat /tmp/you.txt`;
do

echo "docId : $docID"
#Logs of the exited container

docker logs "$docID"  > /tmp/you.txt 2>&1

#Email notification
if [ "$status" = "Up" ]; then echo "test mail" | mailx -s "
containernameapp is up and running"abcs@gmail.com; else uuencode /tmp/you.txt /tmp/you.txt | cat "/tmp/you.txt"| mailx -s "App Name: containernameapp , dockerID : $docID status exited" -a "/tmp/you.txt" abcs@gmail.com; fi
done


Note:
If you use T(using tee) line, we have to start script using:
"Sudo sh testlog.sh >/dev/null 2>&1"

If you use N (Normal line),we have to start normally:
"sudo ./testlog.sh"
Using sendmail client:

we have to install the sendmail client and start the process.

Example:
 nodes that have not checked-in in over 24 hours. 


#!/bin/bash

#Subject of the mail
echo "Subject:not checking into node“ > output.(html, txt)

#Add nodes to the body
echo -e "Servers on this list have gone 24 hours without checking in.\n" >> output.(html, txt)
echo " " >> output.(html, txt)
knife status --hide-by-mins 40 >> output.(html, txt)

#exclude unnecessary nodes
sed -n -i ‘/pinknodes!p’ output.(html, txt)
sed -n -i ‘/testnode/!p’ output.(html, txt)

#send email

/usr/sbin/sendmail -f “abcs@gmail.com” < output.(html, txt) 



















No comments:

Post a Comment