Skip to main content

Simple Server Monitor

#!/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

Comments

Popular posts from this blog

Check remote UDP connectivity from Linux

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 example below $ telnet 127.0.0.1 25 but we can't adopt TELNET to check UDP connectivity. Linux and most of the UNIXes come with a network layer utility called nc (abbreviation for netcat) which is very useful to check UDP connectivity and to explore a lot with both TCP and UDP. An example is shown below # nc -v -u -z -w 3 172.24.16.131 123 Connection to 172.24.16.131 123 port [udp/ntp] succeeded!

The best putty package available

Bored of Black screened Task bar filling putty? Issues with porting Saved sessions from machine to machine? Do you like tabbed SSH sessions? Start using portaputty instead of normal putty and link it with puttycm . Puttycm supports sessions to be saved in its own Database files. You can use the Putty sessions you have saved already right inside putty. You can have any number of databases which allow you to arrange Remote servers in folders and convenient namings. I personally recommend creating Database with puttycm rather than using the sessions saved in putty which doesn't offer any option to create folders and saving sessions under that directory tree. You can even save username/password to get it logged automatically and there is an option to pass commands to be run soon after login. I can't recommend this since some bug was found with these options. Portaputty is a variant of putty which stores all the Configuration data in text files instead of MS Window

PING.sh

#!/usr/bin/env bash ## Ping all machines in a Network PING="$(which ping) -c 1 -W 1" echo "Enter Subnet(eg:192.168.0)" read Subnet echo "Do you want to PING the entire network or a RANGE of IPs ? Enter your choice" echo 1. Ping Entire Network echo 2. Ping a RANGE read choice if [ $choice = 1 ]; then { echo Pinging..... for((i=1;i<255;i++)); do ${PING} ${Subnet}.${i} > /dev/null 2> /dev/null if [ $? -eq 0 ]; then echo -e "${Subnet}.${i} is up" fi done } fi if [ $choice = 2 ]; then { echo Enter the Starting IP of Range read a echo Enter the Last IP of Range read b echo Pinging..... for((i=$a;i<$b;i++)); do ${PING} ${Subnet}.${i} > /dev/null 2> /dev/null if [ $? -eq 0 ]; then echo -e "${Subnet}.${i} is up" fi done } fi exit 0