shell脚本安装部署反向代理 监控进程 计划任务
1.编写脚本自动部署反向代理、web、nfs;
要求:
I、部署nginx反向代理三个web服务,调度算法使用加权轮询;
反向代理服务器脚本配置脚本
#!/bin/bash
#安装eple和nginx
function install(){
rpm -qa | grep epel &> /dev/null
if [ $? != 0 ]
then
yum install epel -y
fi
rpm -qa | grep nginx &> /dev/null
if [ $? != 0 ]
then
yum install nginx -y
fi
}
#启动nginx
function startng(){
ps aux | grep nginx | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart nginx
fi
}
#调用install()
install
echo 'install seccussful'
#nginx.conf追加反向代理客户端ip地址和权重
sed -r -i '/http[ ]*\{/a\upstream mynginx {\nserver 192.168.185.137 weight=3;\nserver 192.168.185.138;\nserver 192.168.185.139;\n}' /etc/nginx/nginx.conf
echo 'insert1 ok'
#追加location内容
sed -r -i '/location \/ \{/a\proxy_pass http://mynginx;' /etc/nginx/nginx.conf
echo 'insert1 ok'
#调用startng()
startng
echo 'start nginx'
反向代理客户端脚本
#!/bin/bash
#安装eple和nginx
function install(){
rpm -qa | grep epel &> /dev/null
if [ $? != 0 ]
then
yum install epel -y
fi
rpm -qa | grep nginx &> /dev/null
if [ $? != 0 ]
then
yum install nginx -y
fi
}
#启动nginx
function startng(){
ps aux | grep nginx | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart nginx
fi
}
#调用install()
install
#调用startng()
startng
II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
存储服务器脚本
#!/bin/bash
#yum安装nfs和RPC
function install(){
rpm -qa | grep rpcbind &> /dev/null
if [ $? != 0 ]
then
yum install rpcbind -y
fi
rpm -qa | grep nfs-utils &> /dev/null
if [ $? != 0 ]
then
yum install nfs-utils -y
fi
}
#调用install()
install
#新建输出目录share,增加写权限
mkdir /share_nfs
chmod -R o+w /share_nfs
#服务端修改配置文件
echo '/share 192.168.185.0/24(rw,sync,fsid=0)'>/etc/exports
#rpcbind和nfs服务开机启动
systemctl enable nfs-server.service
systemctl enable rpcbind.service
#启动rpcbind和nfs服务
function startrn(){
ps aux | grep nfs-server | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart nfs-server.service
fi
ps aux | grep rpcbind | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart rpcbind.service
fi
}
#调用startrn()
startrn
web端脚本
#!/bin/bash
#yum安装nfs和RPC
function install(){
rpm -qa | grep rpcbind &> /dev/null
if [ $? != 0 ]
then
yum install rpcbind -y
fi
rpm -qa | grep nfs-utils &> /dev/null
if [ $? != 0 ]
then
yum install nfs-utils -y
fi
}
#调用install()
install
#rpcbind和nfs服务开机启动
systemctl enable rpcbind.service
#启动rpcbind服务
function startr(){
ps aux | grep nfs-server | grep -v grep &> /dev/null
if [ $? -ne 0 ]
then
systemctl restart rpcbind.serive
fi
}
#调用startr()
startr
#挂载服务端/share目录
mount -t nfs 192.168.185.130:/share_nfs /var/www/html/
2.编写监控脚本,监控nginx,nfs状态,内存、磁盘剩余率检测,异常则发送报警邮件
监控脚本monitor.sh
#!/bin/bash
#monitor nginx
function monitor_nginx(){
ps aux | grep nginx | grep -v grep &> /dev/null
if [[ $? -ne 0 ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig | awk 'NR==2{print $2}')
MSG:nginx service stop"
echo $msg
/usr/bin/mail $msg
fi
}
#monitor nfs
function monitor_nfs(){
ps aux | grep nfs | grep -v grep &> /dev/null
if [[ $? -ne 0 ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig | awk 'NR==2{print $2}')
MSG:nfs service stop"
echo $msg
/usr/bin/mail $msg
fi
}
mem_limit=20
disk_space_limit=20
#monitor memory
function monitor_mem(){
mem_total=`free | awk 'NR==2{print $2}'`
mem_used=`free | awk 'NR==2{print $3}'`
mem_used_per=`echo "scale=2;$mem_used/$mem_total" |bc -l |cut -d. -f2`
if [[ mem_used_per -gt $mem_limit ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig | awk 'NR==2{print $2}')
MSG:Memory usage exceeds the limit,current value is ${mem_used_per}%"
echo $msg
/usr/bin/mail $msg
fi
}
function monitor_disk_space(){
space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
if [[ $space_use -gt $disk_space_limit ]]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
echo $msg
/usr/bin/mail $msg
fi
}
monitor_nginx &>> /tmp/monitor.log
monitor_nfs &>> /tmp/monitor.log
monitor_mem &>> /tmp/monitor.log
monitor_disk_space &>> /tmp/monitor.log
准备发送邮件的工具(将下面述文件内容拷贝到/usr/bin/mail并chmod +x /usr/bin/mail)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text
server = 'smtp.163.com'
port = '25'
def sendmail(server,port,user,pwd,msg):
smtp = smtplib.SMTP()
smtp.connect(server,port)
smtp.login(user, pwd)
smtp.sendmail(msg['from'], msg['to'], msg.as_string())
smtp.quit()
print('email has send out !')
if __name__ == '__main__':
msg = email.mime.multipart.MIMEMultipart()
msg['Subject'] = 'From your monitor server'
msg['From'] = 's*****6@163.com'
msg['To'] = 's*****ve@163.com'
user = 's*****6'
pwd = 's*****3'
content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:]))
txt = email.mime.text.MIMEText(content, _charset='utf-8')
msg.attach(txt)
sendmail(server,port,user,pwd,msg)
3.编写计划任务,定时运行监控脚本,完成监控操作
#用户(-u)root身份编辑(-e)计划任务
crontab -e -u root
#* * * * * [命令] 其中,五个星号表示:分钟 小时 日 月 周
25 14 * * 1-5 /shell/monitor.sh #每周1至周5的14点25分执行monitor脚本
#查看计划任务执行日志
tail -f /var/log/cron