Total Hit Counter

Monday, October 22, 2012

Shell scripts






Check multiple websites' status.
[ Note: First copy the check_http plugin in /usr/lib/nagios/plugins/ path ]

#!/bin/bash

#####
#Script will check all given websites and give status by the help of check_http plugin
#Created by Shankar Patel
#Create date :10-10-2012
#######################################################################

F_P=""
msg_ok=""
exit_status=0
#also you can create a file with the list of websites and 
#SITES_LIST="file_name_of_list_websites"
#for WEB in $(cat $SITES_LIST)
for WEB in {staging.sabsebolo.com,staging.sabsetalk.com,google.com,yyyyyyyyyyyyyyyyahoo.com}
do
/usr/lib/nagios/plugins/check_http $WEB > /dev/null
result_check=$?
if [ "$result_check" -ne 0 ] ; then
       F_P="$F_P $WEB"
       exit_status=2
else
       msg_ok="$msg_ok $WEB"
       exit_status=0
fi
done
if [ "$F_P" = "" ]
then
      echo "OK. All Websites are OK."
else
      echo "CRITICAL. $F_P websites showing down. and $msg_ok are up."
      exit $exit_status;
fi




Script Daily Backup To remote location

#!/bin/bash
SSHKEY=/backup/id_rsa
TOBACKUP=$1
HOSTNAME=$(hostname)
TODAY=`date +%Y%m%d`
check_time=0
tar_done=1
tar_done=1
#create tar file
tak_bkp()
{
 if [ "$check_bkp" -lt 4 ]
 then
        scp -o StrictHostKeyChecking=no -i $SSHKEY /backup/${HOSTNAME}_${TODAY}.tar.gz incoming-backup@heimdall:/mnt/datastore1/incoming-backup/ > /backup/backup_done
        [ "$?" -eq 0 ] && rm /backup/${HOSTNAME}_${TODAY}.tar.gz && exit 0 || check_bkp=$(($check_bkp+1)) && tak_bkp ;
 else
        echo -e "Can not create BKP file! \n ackup not done properly" > /backup/backup_done
        exit 2;
 fi

}
create_tar()
{
 if [ "$check_tar" -lt 4 ]
 then
   tar -cpzf /backup/${HOSTNAME}_${TODAY}.tar.gz --exclude="${HOSTNAME}_${TODAY}.tar.gz" --one-file-system  $TOBACKUP
   [ "$?" -eq 0 ] && tak_bkp
   check_time=$(($check_time+1))
 else
        echo -e "Can not create tar file! \nbackup not done properly" > /backup/backup_done
        exit 2;
 fi
}

create_tar


Script to check backup done or not 


#!/bin/bash
#####
#Script will check backup copied or not on remote location.
#Created by Shankar Patel
#Create date :10-10-2012
#######################################################################
TODAY=`date +%Y%m%d --date="1 day ago"`
bkp_exist=`ssh -o StrictHostKeyChecking=no -i /backup/id_rsa incoming-backup@heimdall "ls /mnt/datastore1/incoming-backup/${HOSTNAME}_${TODAY}.tar.gz &>/dev/null && echo -e 'OK' || echo -e 'CRITICAL'" 2>/dev/null`
[ "$bkp_exist" == "OK" ] && echo "OK. Yesterday's backup done."  && exit 0
[ "$bkp_exist" == "CRITICAL" ] &&  echo "CRITICAL. Yesterday's backup done." && exit 2




Nagios plugin to check Openfiles
check_openfiles.sh 

if it is greater then 85% then it will show warning
if it is greater then 90% then it will show warning
otherwise it will show ok. 



#!/bin/bash
###########
# This script will check no open files and if open files will be greater then 85% of max-open-files then it will show warning and open files will be greater then 90% then it will show critical.
#
# It does not require any additional perameter at execution time
# it automatically takes perameter from system files
########### Shankar Patel 22/08/1012

open_files=`cat /proc/sys/fs/file-nr | awk '{print $1}'`
max_open_files=`cat /proc/sys/fs/file-nr | awk '{print $3}'`

warn_of=$(($max_open_files*15/100))
crit_of=$(($max_open_files*10/100))

if [ "$open_files" -lt "$warn_of" ]
then
echo "OK. Number of open files = $open_files | openfiles= $open_files"
exit 0;
elif [ "$open_files" -gt "$warn_of" ] && [ "$open_files" -lt "$crit_of" ]
then
echo "WARNING. Number of open files = $open_files | openfiles= $open_files"
exit 1;
else
echo "CRITICAL. Number of open files = $open_files | openfiles= $open_files"
exit 2;
fi


Script to monitor log file error.


#!/bin/bash

FILE2TAIL=$1
PATTERN=$2
CATCH_UP=0
FPATH=$(dirname $1)
FFILE=$(basename $1)
INODE_NUM=$(ls -i ${FPATH}/${FFILE} | awk '{ print $1 }')
PATTERN_CODE="$(echo -n "$PATTERN"|tr -d \* | tr -d ' ' | tr -d \. )"

FSIZE_FILE=${FPATH}/.${FFILE}_${PATTERN_CODE}_${INODE_NUM}.size

SIZE_CURRENT=$(ls -l ${FPATH}/${FFILE} | awk '{ print $5 }')
exit_status=0

last_exit()
{
# save current size
ls -l ${FPATH}/${FFILE} | awk '{ print $5 }' > ${FSIZE_FILE}
exit $exit_status
}
if [ ! -f ${FSIZE_FILE} ]; then
# first time, save line count
echo "saving initial size of ${FPATH}/${FFILE}"
last_exit
fi
# last size is available
SIZE_LAST=$(cat ${FSIZE_FILE})

SIZE_DELTA=$(($SIZE_CURRENT - $SIZE_LAST))

if [ $SIZE_DELTA -gt 0 ]
then
# new bytes to scan
tail "-${SIZE_DELTA}c" ${FPATH}/${FFILE} > /tmp/ts$$
egrep "$PATTERN" /tmp/ts$$ > /tmp/tsF$$
GREP_STATUS=$?
if [ $GREP_STATUS -eq 0 ]
then
# found a match
SUBJECT="Found $PATTERN in $FILE2TAIL"
BODY="$SUBJECT\n$(cat /tmp/tsF$$)"
echo "$SUBJECT"
#### exec
#cho -e $BODY # | $EXEC_ON_MATCH -s "$SUBJECT" $EMAILADDR
echo "CRITICAL. 'Error writing data to the connection' found in ${FFILE} | error_found=1"
last_exit
fi

if [ $GREP_STATUS -ne 0 ]
then
# found no matches
SUBJECT="Missing $PATTERN in $FILE2TAIL on $HOST_NAME"
BODY="$SUBJECT\n"
echo "OK. 'Error writing data to the connection' not found in ${FFILE} | error_found=0"
last_exit
#### exec
fi

/bin/rm -f /tmp/ts$$ /tmp/tsF$$
fi


No comments: