开发脚本自动部署及监控
1.编写脚本自动部署反向代理、web、nfs;
要求:
I、部署nginx反向代理三个web服务,调度算法使用加权轮询;
II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
1.创建脚本文件并赋予执行权限。
2.编写脚本
1 #!/bin/bash 2 #安装必要软件以及部署准备 3 yum install epel-release -y -q 4 yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -y -q 5 yum install nginx -y -q 6 yum install rpcbind nfs-utils -y -q 7 systemctl stop firewalld 8 9 ps axu | grep 'nagxin' | grep -v 'grep' &> /dev/null 10 if [ $? -ne 0 ] 11 then 12 systemctl restart nginx 13 fi 14 if [ ! -d /share ];then 15 mkdir -p /share 16 chmod -R o+w /share 17 fi 18 #设置反向代理 19 function nginxstart() { 20 if [ -f /etc/nginx/ngxin.conf ]; 21 then 22 sed -ri '/^http/a\t upstream pythonweb { server 192.168.20.132 weight=3; server 192.168.20.130; server 192.168.20.131; }' /etc/nginx/nginx.conf 23 sed -ri '/^ *location / {/a\t proxy_pass http://pythonweb;' /etc/nginx/nginx.conf 24 echo "Configuration successful" 25 fi 26 } 27 #启动nfs并设置共享目录 28 function nfs() { 29 echo '/share 192.168.20.0/24(rw,sync,fsid=0)' > /etc/exports 30 systemctl enable rpcbind.service 31 systemctl enable nfs-server.service 32 systemctl start rpcbind.service 33 systemctl start nfs-server.service 34 mount -t nfs 192.168.20.129:/share /var/html/ 35 chmod -R o+w /share 36 } 37 38 nginxstart 39 nfs
2.编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
1.准备发邮箱的工具
#!/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'] = '内存不足!' msg['From'] = 'python4_mail@163.com' msg['To'] = 'python4_recvmail@163.com' user = 'python4_mail' pwd = 'sbalex3714' content='%s %s' %(' '.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)
2.赋予执行权限,移动到环境变量文件夹。
3.新建监控脚本文件
# vim /root/jiankong.sh
#!/bin/bash mem_limit=80 #内存使用超过80%则报警 function monitor_mem(){ mem_total=`free |awk 'NR==2{print $2}'` mem_use=`free |awk 'NR==2{print $3}'` mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2` if [ $mem_per -gt $mem_limit ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:注意,您的内存使用已经达到 ${mem_per}%" echo $msg /usr/bin/my_mail $msg fi } monitor_mem &>> /tmp/monitor.log
3.编写计划任务,定时运行监控脚本,完成监控操作
# crontab -e -u root