mercredi 23 août 2017

Monitoring application with a simple bash under linux



 Edit a cron file

 vi /etc/cron.hourly/monitorhttpd.sh


#!/bin/bash
ps -ef |grep `cat /var/run/httpd/httpd.pid`|grep httpd>/tmp/httpjenkinscount
NBPCS=`wc -l

if [ "$NBPCS" -eq "0" ]; then
    service httpd start
fi



#!/bin/bash
ps -ef |grep `cat /var/run/tomcat-jenkins/tomcat.pid`|grep java>/tmp/tomcatjenkinscount
NBPCS=`wc -l

if [ "$NBPCS" -eq "0" ]; then
    sudo -ujenkins -- "/local/home/jenkins/tomcat-7/bin/shutdown.sh"
    sleep 30
    rm -f /var/run/tomcat-jenkins/tomcat.pid
    sudo -ujenkins -- "/local/home/jenkins/tomcat-7/bin/startup.sh"
fi 


But you can also perform some more complex monitoring by testing call to url (end to end test)
vi ./autorestartscript.sh

#!/bin/sh
rm -f /tmp/status
wget https://myserver/myapp/status --no-check-certificate -O /tmp/status
RETOUR=$?
KOCOUNT=`cat /tmp/status|grep KO|wc -w`
if [ $KOCOUNT -ge 1 ] || [ $RETOUR -ge 1 ];
then
service tomcat6_myapp stop;
sleep 30
service tomcat6_myapp start;
service httpd_myapp restart;
fi