#!/bin/bash
# BASH Script to monitor Server uptime,Servies,Memory Usage,Disk Usage,Load Average,Last Login and Reboot Details and Take backup of configuration files.
DATE=`date +%d.%m.%Y`
TIME=`date +%H.%M.%S`
ADMIN=hareeshvv@gmail.com ## Mail ID of Admin
ADMIN1=hareeshvaliyaveettil@gmail.com ## Mail ID of Admin
FILES=(/etc/hosts /usr/local/apache/conf/httpd.conf /etc/php.ini /var/lib/pgsql/data/pg_hba.conf)
services=(http mysql smtp ftp postgresql) ## List of services to be checked
serviceports=(:80 :3306 :25 :21 :5432) ## List of services to be checked
#services=(http https smtp) ## List of services to be checked
#serviceports=(:80 :443 :25) ## List of services to be checked
title=0
## Definition of Functions
# chkuptime function
chkuptime () {
echo " " >> /tmp/$DATE.$TIME
echo "The Server `hostname` is up for `uptime | cut -f1 -d, | awk {'print $3'} ;uptime | cut -f1 -d, | awk {'print $4'}`" >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
}
# lastlogin function
lastlogin () {
echo "Last login Details" >> /tmp/$DATE.$TIME
echo "##################" >> /tmp/$DATE.$TIME
echo "Last login was by `last | head -1 | awk -F" " {'print $1'}` on `last | head -1 | awk -F" " {'print $2'}` at `last | head -1 | awk -F" " {'print $5'}` `last | head -1 | awk -F" " {'print $6'}` `last | head -1 | awk -F" " {'print $7'}` " >> /tmp/$DATE.$TIME
last | head -1 | awk -F" " {'print $2'} | cut -f1 -d/ > /dev/null 2> /dev/null
if [ $? = 0 ]
then
echo "from `last | head -1 | awk -F" " {'print $3'}`" >> /tmp/$DATE.$TIME
fi
`w`
if [ $? = 0 ]
then
echo Details of currently logged users >> /tmp/$DATE.$TIME
`w` >> /tmp/$DATE.$TIME
fi
echo " " >> /tmp/$DATE.$TIME
}
# lastreboot function
lastreboot () {
echo "Last Reboot Details" >> /tmp/$DATE.$TIME
echo "###################" >> /tmp/$DATE.$TIME
echo "Last reboot was at `last | grep "system boot" | head -1 | awk -F" " {'print $5'}` `last | grep "system boot" | head -1 | awk -F" " {'print $6'}` `last | grep "system boot" | head -1 | awk -F" " {'print $7'}` `last | grep "system boot" | head -1 | awk -F" " {'print $8'}`" >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
}
# Services function
services () {
# Check the Service Status of Server and Write them into a text file
# Function Watch()
Watch () {
#Server=192.168.0.254 ## IP of the remote Server to be monitored
#nmap $Server | grep "${serviceports[$i]/"} > /dev/null 2> /dev/null ## Use this one for Remote Server which is not behind any firewall that may block NMAP
netstat -ntpl | grep "${serviceports[$i]} " ## Use this locally .
# The SPACEBAR in "${serviceports[$i]} " is for exactness
if [ $? != 0 ]
then
if [ $title = 0 ] ## Do this once if any of the services is down
then
echo Failed Services >> /tmp/$DATE.$TIME
echo "###############" >> /tmp/$DATE.$TIME
title=1
fi
echo ${services[$i]} is DOWN >> /tmp/$DATE.$TIME
fi
}
###### Watch () Ends here
echo "Services undergone CHECK are" >> /tmp/$DATE.$TIME
echo "#######################" >> /tmp/$DATE.$TIME
for (( i = 0 ; i < ${#services[@]} ; i++ ))
do
echo ${services[$i]} >> /tmp/$DATE.$TIME
done
echo " " >> /tmp/$DATE.$TIME
## Now call the Watch() function
for (( i = 0 ; i < ${#services[@]} ; i++ ))
do
Watch ${services[$i]}
done
grep Failed /tmp/$DATE.$TIME # > /dev/null 2> /dev/null
if [ $? != 0 ]
then
# {
# echo grep failed output is $? >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
echo "All Services are UP.Congratulations" >> /tmp/$DATE.$TIME
# }
fi
grep DOWN /tmp/$DATE.$TIME ## Warn if all the services are down
if [ $? = 0 ]
then
count=`grep DOWN /tmp/$DATE.$TIME | wc -l`
if [ ${#services[@]} = $count ]
then
echo " " >> /tmp/$DATE.$TIME
echo "*** IMPORTANT NOTICE ***" >> /tmp/$DATE.$TIME
echo "All Services are down !!!!!" >> /tmp/$DATE.$TIME
fi
fi
echo " " >> /tmp/$DATE.$TIME
}
loadavg () {
echo Load Average >> /tmp/$DATE.$TIME
echo "############" >> /tmp/$DATE.$TIME
echo Load average is `uptime | cut -f3 -d, | cut -f2 -d:` `uptime | cut -f4 -d,` `uptime | cut -f5 -d,` >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
}
## loadavg() ends here
disk () {
ALERT=90 # Alert level for Used Percentage of Disk Partitions
df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
used=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
mountpoint=`mount | grep $partition | cut -f3 -d" "`
if [ $used -ge $ALERT ]; then
echo "Disk Usage" >> /tmp/$DATE.$TIME
echo "##########" >> /tmp/$DATE.$TIME
echo "Running out of space \"$partition ($used%) mounted on $mountpoint\" on $(hostname) as on $(date)" >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
fi
done
}
## disk() ends here
memory () {
echo "Memory and SWAP Usage" >> /tmp/$DATE.$TIME
echo "#####################" >> /tmp/$DATE.$TIME
MEM=`free -m | grep Mem | awk -F" " {'print $2'}`
USEDMEM=`free -m | grep Mem | awk -F" " {'print $3'}`
SWAP=`free -m | grep Swap | awk -F" " {'print $2'}`
USEDSWAP=`free -m | grep Swap | awk -F" " {'print $3'}`
echo "Total Memory - $MEM" >> /tmp/$DATE.$TIME
echo "Used - $USEDMEM" >> /tmp/$DATE.$TIME
echo "Total Swap - $SWAP" >> /tmp/$DATE.$TIME
echo "Used Swap - $USEDSWAP" >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
}
### memory() ends here
backup () {
for (( i = 0 ; i < ${#FILES[@]} ; i++ ))
do
cat ${FILES[$i]} > ${FILES[$i]}.$DATE.$TIME
done
echo " " >> /tmp/$DATE.$TIME
}
logchk () {
echo "System Logs" >> /tmp/$DATE.$TIME
echo "###########" >> /tmp/$DATE.$TIME
grep "error" /var/log/messages >> /tmp/$DATE.$TIME
grep "warn" /var/log/messages >> /tmp/$DATE.$TIME
echo " " >> /tmp/$DATE.$TIME
}
Sendmail () {
mail -s "Server Status" $ADMIN < /tmp/$DATE.$TIME
mail -s "Server Status" $ADMIN < /tmp/$DATE.$TIME
}
## End of Function definitions
exec > /dev/null 2> /dev/null
# Call to functions
chkuptime
lastlogin
lastreboot
services
loadavg
disk
memory
### backup
echo "on $(hostname) as on $(date)" >> /tmp/$DATE.$TIME
Sendmail
cd -
rm -rf /tmp/$DATE.$TIME
exit
Friday, November 9, 2007
Subscribe to:
Post Comments (Atom)
AT&T USA | Internet not working | Fix by custom APN
If the AT&T Mobile internet is not working on your cellphone, it can be fixed easily by adding an APN configuration. You can read this a...
-
Hi there, You all know how to check TCP port connectivity from a Linux or UNIX machine to a remote machine using telnet as per th exampl...
-
Before you start Ensure that you have installed wvdial, usbmodeswitch and usbmodeswitch_data # dpkg -l | grep wvdial # dpkg -l | grep ...
-
1. Open Applications -> System -> Configuration Editor from the GUI OR Open a terminal, type gconf-editor 2. Go to "...
No comments:
Post a Comment