Wednesday, September 3, 2014

Script to monitor any service running or not

There are cases where we want to monitor running services in our servers and if in case they fails take a action according to that..

In the example script we are monitoring PHP, so if your monitoring any other service please adjust your at least "count"parameter accordingly..

Place the script in cron to check the required service..

#!/bin/bash

#To count the no of process running
count=$(ps -ef | grep -c php)
#To show count no for testing
echo $count

#Comparing the count variable with min required by us
if [ $count -lt 5 ]; then
echo -e "\nThe php seems to be not running starting it..."

#Auto starting the required service if not running, will differ according to your need
killall php-fpm
/etc/init.d/php-fpm start
/etc/init.d/nginx restart


#Sending a mail to desired user about the start of service with fetching hostname from AWS
echo -e "The PHP was stopped somehow but now it has been restarted in `wget -q -O - http://169.254.169.254/latest/meta-data/public-hostname`..\nThe PHP service is running fine now..\n\n `ps -ef | grep php`\n\n If in case PHP output is empty above please run following commands via log in to server..\n\nkillall php-fpm\n/etc/init.d/php-fpm start \n/etc/init.d/nginx restart" | mailx -r "phpalert@yourdomain.com" -s "PHP was again started in following server `date`" "ravi@theteamie.com"

 else
    echo "PHP is running fine is up and running"
  fi

The output of script will be as follows :

The PHP was stopped somehow but now it has been restarted in ec2-54-XX.XX.XX.ap-southeast-1.compute.amazonaws.com..
The PHP service is running fine now..

 root      1859  1856  0 11:50 ?        00:00:00 /bin/bash /usr/scripts/php_check.sh
root      1879     1  1 11:50 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
root      1882  1879 29 11:50 ?        00:00:00 /usr/lib/appdynamics-php5/proxy/jre/bin/java -server -Xmx120m -classpath /usr/lib/appdynamics-php5/proxy/conf/logging/*:/usr/lib/appdynamics-php5/proxy/lib/*:/usr/lib/appdynamics-php5/proxy/lib/tp/*:/usr/lib/appdynamics-php5/proxy/* -Djava.library.path=/usr/lib/appdynamics-php5/proxy/lib/tp -Dappdynamics.agent.logs.dir=/usr/lib/appdynamics-php5/logs -Dcomm=/tmp/ad-bmjfkD -DagentType=PHP_APP_AGENT -Dappdynamics.agent.runtime.dir=/usr/lib/appdynamics-php5/proxy com.appdynamics.ee.agent.proxy.kernel.Proxy
apache    1883  1879  0 11:50 ?        00:00:00 php-fpm: pool www0
apache    1884  1879  0 11:50 ?        00:00:00 php-fpm: pool www0
apache    1885  1879  0 11:50 ?        00:00:00 php-fpm: pool www0
root      1911  1859  0 11:50 ?        00:00:00 /bin/bash /usr/scripts/php_check.sh
root      1912  1859  0 11:50 ?        00:00:00 /bin/bash /usr/scripts/php_check.sh
root      1916  1911  0 11:50 ?        00:00:00 /bin/bash /usr/scripts/php_check.sh
root      1918  1916  0 11:50 ?        00:00:00 grep php

 If in case PHP output is empty above please run following commands via log in to server..

killall php-fpm
/etc/init.d/php-fpm start
/etc/init.d/nginx restart




Setup fully configurable EFK Elasticsearch Fluentd Kibana setup in Kubernetes

In the following setup, we will be creating a fully configurable Elasticsearch, Flunetd, Kibana setup better known as EKF setup. There is a...