GroupWise 8 SP1 Linux - Script Checking Services w/auto restart/logging

0 Likes

This is a script I wrote to automate the restart of GroupWise 8 sp1 services on our SLES11 server. Frequently the grpwise and gwmta services would fail on the server. Restarting the services manually in the middle of the night got old fast. This script will check the grpwise and gwmta services to see if they are running or not. If not it will be logged, restarted, checked for completion, logged and if it fails, logged again.



This script could be easily modified to include other services like webaccess, apache, tomcat, and the gwia. This script uses two methods for finding the running service/process. grpwise services does not run with a PID so the method for finding if the service is running is different than finding the gwmta process, take note.



You can download the script as well. Rename file to have .sh extension. Upload to server the as root then chmod u x <script_name>.sh to make the script executable. Edit crontab and insert 10,20,30,40,50,00 * * * * /opt/novell/groupwise/<script_name>.sh to run the script every 10 minutes



Code:

#!/bin/bash
#
#------------------Change LOG------------------
#v9.5-Added hostname to email notifications
#v9.5.1-Removed $(date) from mail string. Mail was processing $(date) as an email address.
#
#----------------------------------------------
#
#Edit crontab as root to modify the schedule of this script.
#10,20,30,40,50,00 * * * * /opt/novell/groupwise/<script_name>.sh
echo "This script will check to see if $SERVICE1 and $SERVICE2 are running. If one or both are not running this script will start the processe(s). This script will also log the event and email $EMAIL1,$EMAIL2."

#Defined variables
LOGDATE=$(date "%m%d%Y")
LOG="/var/log/novell/groupwise/gwscript_logs/gw_services_$LOGDATE.log"
SERVICE1='gwmta'
SERVICE2='grpwise'
HOST=gwmail
EMAIL1=root
EMAIL2=email@domain.com
VERSION=v9.5.1

#Check to see if log file exists already, if not create log.
if [ -e $LOG ]
then echo "Log exists"
else touch $LOG ; echo "Log file $LOG created"
fi

#Write header to log.
echo -e "-------------------------$SERVICE1/$SERVICE2 Service Check Log $VERSION-------------------------">>$LOG

#Check to see that gwmta process is running.
if ps ax | grep -v grep | grep 'gwmta --home /opt/novell/groupwise/<insert domain here>' > /dev/null 2>&1
then
echo -e $(date) "$SERVICE1 service is running.">>$LOG
elif
#If service is not running try to restart and send email notification.
echo -e $(date) "Sending Email Notice that $SERVICE1 is not running to $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$SERVICE1 is not running, attempting to restart $SERVICE1 now....">>$LOG
echo -e $(date) "$HOST $SERVICE1 is not running! attempting to restart" | mail -s "$HOST $SERVICE1 is down." $EMAIL1,$EMAIL2

/opt/novell/groupwise/agents/bin/gwmta --home /opt/novell/groupwise/<insert domain here> &
#for script testing only: /etc/init.d/apache2 start
#Import last 15 lines from /var/log/messages to help with troubleshooting.
echo -e $(date) "Importing last 15 lines from /var/log/messages to $LOG now.... \r \r From /var/log/messages:">>$LOG
tail -15 /var/log/messages>>$LOG
echo -e "End /var/log/messages">>$LOG
echo -e "\r">>$LOG
then
#Check to see that service started correctly and send email notification.
ps ax | grep -v grep | grep 'gwmta --home /opt/novell/groupwise/<insert domain here>' > /dev/null 2>&1
echo -e $(date) "$SERVICE1 started successfully">>$LOG
echo -e $(date) "Sending Email Notice that $SERVICE1 restarted successfully $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$HOST $SERVICE1 has been restarted successfully" | mail -s "$HOST $SERVICE1 restarted" $EMAIL1,$EMAIL2
else
#If service failed to start send email notification.
echo -e $(date) "$SERVICE1 failed to restart">>$LOG
echo -e $(date) "Sending Email Notice for $SERVICE1 restart failed $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$HOST $SERVICE1 restart failed!" | mail -s "$HOST $SERVICE1 restart failed" $EMAIL1,$EMAIL2
#Import last 15 lines from /var/log/messages to help with troubleshooting.
echo -e $(date) "Importing last 15 lines from /var/log/messages to $LOG now.... \r \r From /var/log/messages:">>$LOG
tail -15 /var/log/messages>>$LOG
echo -e "End /var/log/messages">>$LOG
echo -e "\r">>$LOG
fi

#Check to see that grpwise service is running.
if /sbin/service $SERVICE2 status |grep running > /dev/null 2>&1
then
echo -e $(date) "$SERVICE2 service is running.">>$LOG
echo -e "\r">>$LOG
elif
#If service is not running try to restart and send email notification.
echo -e $(date) "Sending Email Notice for $SERVICE2 is not running to $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$SERVICE2 is not running, attempting to restart $SERVICE2 now....">>$LOG
echo -e $(date) "$HOST $SERVICE2 is not running! attempting to restart" | mail -s "$HOST $SERVICE2 is down." $EMAIL1,$EMAIL2
/etc/init.d/$SERVICE2 start
#Import last 15 lines from /var/log/messages to help with troubleshooting.
echo -e $(date) "Importing last 15 lines from /var/log/messages to $LOG now.... \r \r From /var/log/messages:">>$LOG
tail -15 /var/log/messages>>$LOG
echo -e "End /var/log/messages">>$LOG
echo -e "\r">>$LOG
then
#Check to see that service started correctly and send email notification.
service $SERVICE2 status |grep running > /dev/null 2>&1
echo -e $(date) "$SERVICE2 started successfully">>$LOG
echo -e $(date) "Sending Email Notice for $SERVICE2 restarted successfully $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$HOST $SERVICE2 has been restarted successfully." | mail -s "$HOST $SERVICE2 restarted" $EMAIL1,$EMAIL2
echo -e "\r">>$LOG
else
#If service failed to start send email notification.
echo -e $(date) "$SERVICE2 Failed to restart">>$LOG
echo -e $(date) "Sending Email Notice for $SERVICE2 Failed to restart to $EMAIL1,$EMAIL2">>$LOG
echo -e $(date) "$HOST $SERVICE2 failed to restart." | mail -s "$HOST $SERVICE2 failed to restart." $EMAIL1,$EMAIL2
#Import last 15 lines from /var/log/messages to help with troubleshooting.
echo -e $(date) "Importing last 15 lines from /var/log/messages to $LOG now.... \r \r From /var/log/messages:">>$LOG
tail -15 /var/log/messages>>$LOG
echo -e "End /var/log/messages">>$LOG
echo -e "\r">>$LOG
fi

#end script


If you have ideas on how to improve the script please share them.

Tags:

Labels:

How To-Best Practice
Comment List
Anonymous
Parents Comment Children
No Data
Related Discussions
Recommended