Archive

Posts Tagged ‘bash shell’

Auto-monitor your server status and restart or reboot server(bash shell script)

November 22nd, 2010 2 comments

I’m now in charge of about 70 vps.Every vps has www web-server & proftpd ftp server & mysql db server on it.Now I want to know every server’s status.There’s of course method that notify you waking up at mid-night and handling with the simple reboot of apache & mysql & proftpd,yes,that’s true,for your boss won’t be happy enough to see all sites offline on servers that are within your duty.This is called a ‘pull’ working style.Ok.Now why don’t we try the ‘push’ working style now?Just write the following script on your server,and add it to the cron schedule at a proper execution rate.

The bash script auto-detect and monitor status of your service on a server,and if it find that the software has stopped working,the script will actively start it.And,if the number of  httpd or mysqld exceeds the point you set in the script,the server will restart.All of this primarily should be your working scope,and now it’s all the bash script’s duty!

Here goes the script:
#!/bin/bash
#check apache,mysql thread and auto reboot system
#Powered by ipaddr(aspbiz)
#Modified by doxer.org
which os is the system,debian or centos;others exit 1
ip_address=’xxx.xxx.xxx.xxx’
mail_doxer()
{
mail -s “Abnormal Message from VPS $ip_address” [email protected]</tmp/result.txt
}
echo ”>/tmp/result.txt
os=””;
apachename=””;
grep “centos” /etc/issue -i -q
if [ $? = '0' ];then
os=’centos’
apachename=”httpd”
fi

grep “debian” /etc/issue -i -q
if [ $? = '0' ];then
os=’debian’
apachename=”apache”
fi

if [ $os = "" ];then
echo “not a valid system os”
exit 1
fi

ApacheThread=`ps -A|grep ${apachename}|wc -l`
MysqldThread=`ps -A|grep mysql|wc -l`
ProftpThread=`ps -A|grep proftpd|wc -l`

if [ $ApacheThread -eq 0 ]
then
echo “Apache Server has stoped, it will start at $(date +”%y-%m-%d %H:%M:%S”)”>>/tmp/result.txt
mail_doxer
/etc/init.d/apache2 start
fi

if [ $MysqldThread -eq 0 ]
then
echo “MySQL Server has stoped, it will start at $(date +”%y-%m-%d %H:%M:%S”)”>>/tmp/result.txt
mail_doxer
/etc/init.d/mysql start
fi

if [ $ProftpThread -eq 0 ]
then
echo “ProftpThread Server has stoped, it will start at $(date +”%y-%m-%d %H:%M:%S”)”>>/tmp/result.txt
mail_doxer
/etc/init.d/proftpd start
fi

#process forks too much,needs reboot the system
MaxApacheThread=30
MaxMysqlThread=250

NeedReboot=0

if [ $ApacheThread -gt $MaxApacheThread ]
then
NeedReboot=1
fi

if [ $MysqldThread -gt $MaxMysqlThread ]
then
NeedReboot=1
fi

if [ $NeedReboot -eq 1 ]
then
date_reboot=$(date +”%y-%m-%d %H:%M:%S”)

echo “System is busy,reboot\nApache:$ApacheThread;Mysql:$MysqldThread;Vsftp:$ProftpThread.”>>/tmp/result.txt
mail_doxer
reboot
fi

Ok.Now add the script to your cron schedule by crontab -e and type in the following(change it to meet your needs):
5/* * * * * /usr/local/apache_mysql_ftp_check_restart.sh #every 5 minutes

Categories: Linux Tags: ,

Using expect to auto login and execute commands(relieve you from tedious login and typing)

November 19th, 2010 No comments

Another imagination:

You have 100 servers.Now you’ve got a task.The boss needs to create a directory under /root and put some files under it.

What would you do next?Just be honestly and sincerely and start log in ssh then use mkdir to create directory,then use sftp or even ftp to put the files under the directory?

That would tire you so much,sour you hands and mind.Now let one line bash shell script to help you.

Here goes the general idea about the solution:

1.Write a bash script with multiple parameters.Each two of the parameters match the ip address and password to your ssh root account.

2.Now the script runs.It auto get the two parameters as a pair,and use expect to auto answer to interactive output of ssh command(‘yes’ to ‘Are you sure you want to continue connecting (yes/no)?’,and your password parameter to ‘[email protected]’s password:’).

3.After log in the remote server using ssh,the script download a script from url you designate.Like http://yourblog.com/mkdir_and_put_files_in_it.sh.After the download,use ‘sh ./mkdir_and_put_files_in_it.sh’ to run it which has command ‘mkdir’ and ftp get in it.

Above is the skeleton of the design.Now let’s go to the code area:
#!/bin/bash
while [ $# -ne 0];
do
sshhost=$1
password=$2
shift 2
CMD=’curl -o nginx_log.sh http://yourblog.com/mkdir_and_put_files_in_it.sh && sh ./mkdir_and_put_files_in_it.sh’
echo ”>/root/.ssh/known_hosts #first,delete known_hosts to let the expect “*yes*” work
/usr/bin/expect <<EOF
spawn ssh -lroot -p22 $sshhost $CMD
set timeout 60 #1 minute,command will be executed on the remote server even though 60s
expect “*yes*”
send “yes\r”
expect “*password*”
send “$password\r”
#interact
expect eof
EOF
done
Ok.Name the above shell script auto_sh.sh,grant it with execute privilege.

Then,create your mkdir and ftp get files(You can refer to here to auto-ftp get files).

Now use ./auto_sh.sh first_server_ip first_server_ip_password second_server_ip second_server_ip_password third_server_ip third_server_ip_password forth_server_ip forth_server_ip_password.You can assign as many pairs ip/password to the parameter position as you want.Just be relieved,your work will be perfectly done as you wish!

Categories: Linux Tags: ,

ip ping in bulk batch shell script

September 30th, 2010 1 comment

Imagine,you’ve got a task now to verify a bulk of domains the speed of response and  whether they are active or not.Here a bulk of,means,for example,1,000.On getting this task,what will you do next?Type in every ip in the command line in Microsoft’s cmd and waits for the result?1,000,you must consider it.You may imagine,how tired after you’ve pinged the full 1,000 of the urls.

Here is my implement of the task.Firstly,put the urls in domain.txt,one url per line.Then run the script:sh if.sh.Lastly,waits for the result in ip.txt.During the execution time,you may go out and breath fresh air or have a cup of tea,not type in the Microsoft cmd any more!

Now,have a try by yourself!

if.sh:
#!/bin/bash
#copyright:doxer.org
rm -rf ./ip.txt
for i in `cat ./domain.txt`
do
echo -e ‘BEGIN\n\n’ >>ip.txt
ping $i -c 3 >>ip.txt
echo -e ‘\n\nEND\n’ >>ip.txt
done

Put your list of domains in the file:
xx1.com
xx2.com
xx3.com
xx4.com

Now,please use cat ip.txt to see the joyful result!

PS:

I have known that the script in this article ping host one by one for 3 years(now is 2013). So I’ve written an upgraded rpm package called pping_doxer, which pings hosts in parallel and proves to be very efficient. You can read more about it on http://www.doxer.org/projects-doxer/.