自动部署及监控脚本

1.编写脚本自动部署反向代理、web、nfs;

要求:
I、部署nginx反向代理三个web服务,调度算法使用加权轮询;

  1 #!/bin/bash
  2 systemctl disable firewalld        #关闭firewalld自启动
  3 systemctl stop firewalld            关闭firewalld
  4 yum -y install nginx                #二进制安装nginx
  5 egrep -v "^$|^#|#"  /etc/nginx/nginx.conf        #把空行,#开头的行,带#号开头的行去掉
  6 mkdir -p /data/www/html
  7 echo "welcome oldboy education" > /data/www/html/index.html
  8 /usr/sbin/nginx -t                                                #验证nginx配置文件是否正确
  9 systemctl start nginx                                        #启动nginx
 10 yum install rpcbind nfs-utils -y                        #二进制安装nfs及相关依赖包
 11 mkdir /share
 12 echo "/share 192.168.152.0/24(rw,sync,fsid=0)" > /etc/exports
 13 echo "bogon       192.168.186.134">> /etc/hosts
 14 echo "python-web1 192.168.186.135">> /etc/hosts
 15 echo "python-web2 192.168.186.136">> /etc/hosts
 16 echo "python-web3 192.168.186.133">> /etc/hosts
 17 systemctl enable rpcbind.service
 18 systemctl enable nfs-server.service
 19 systemctl start rpcbind.service
 20 systemctl start nfs-server.service
 21 rpcinfo
 22 exportfs
 23 showmount -e
 24 mount -t nfs 192.168.186.135:/share /data/www/html/
 25 mount -t nfs 192.168.186.136:/share /data/www/html/
 26 mount -t nfs 192.168.186.133:/share /data/www/html/
 27 showmount -a
 28 chmod -R o+w /share/                                                    #给其他用户添加可写权限
 29 echo "welcome oldboy education" > /data/www/html/test.html
 30 
 31 
 32 touch /etc/nginx/conf.d/proxy.conf
 33 cat > /etc/nginx/conf.d/proxy.conf <<EOF
 34     upstream web {
 35         server 192.168.186.135;
 36         server 192.168.186.136;
 37         server 192.168.186.133
 38     }
 39 
 40 
 41 msg='upstream { }'
 42 sed -ri "/^http/a $msg" /etc/nginx/nginx.conf         #增加upstream
 43 sed -ri "/^ *location / {$/a proxy_pass http://my_upstream;" /etc/nginx/nginx.conf     
 44 II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;
 45 
 46 #!/bin/bash
 47 ngxstatus = `ps -ef | grep nginx | grep -v grep | grep -c nginx`
 48 function ngxProxyInstall() {
 49 if [ -e /usr/sbin/nginx ];    then
 50     echo "nginx is already installed"
 51     exit 110
 52 else
 53         systemctl disable firewalld        #关闭firewalld自启动
 54         systemctl stop firewalld            #关闭firewalld
 55         yum -y install nginx -q            #二进制安装nginx
 56         /bin/cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak  #备份Nginx配置文件
 57         egrep -v "^$|^#|#"  /etc/nginx/nginx.conf        #把空行,‘#’开头的行,带‘#’号开头的行去掉
 58         mkdir -p /data/www/html
 59         echo "install nginx successful"
 60 fi
 61 if    (-e /etc/nginx/nginx.conf) then
 62     sed -ri '/^http/a\t upstream bing { 
	 server 192.168.186.135 weight=3;
	 server 192.168.186.136;
	 server 192.168.186.133;
	 }' /etc/nginx/nginx.conf
 63     sed -ri '/^ *location / {/a\t	 proxy_pass http://luchuangao;' /etc/nginx/nginx.conf
 64     echo "Configuration successful!"
 65     systemctl start nginx
 66 }
 67 
 68 #nfs安装
 69 nfsstatus=`ps -ef | grep nfs | grep -v grep | grep -c nfs`
 70 function nfsInstall() {
 71 if [ -e /usr/sbin/rpcinfo ];then
 72     echo "nfs already installed"
 73     exit 111
 74 else
 75     yum install rpcbind nfs-utils -y -q
 76     echo "echo install nginx successful!"
 77 fi
 78  
 79 if [ ! -d /share ];then
 80     mkdir -p /share
 81     chmod -R o+w /share
 82 fi
 83     echo "bogon       192.168.186.134">> /etc/hosts
 84     echo "python-web1 192.168.186.135">> /etc/hosts
 85     echo "python-web2 192.168.186.136">> /etc/hosts
 86     echo "python-web3 192.168.186.133">> /etc/hosts
 87     echo "/share 192.168.168.134/24(rw,sync,fsid=0)">> /etc/exports
 88     
 89     
 90     chmod -R o+w /share/
 91     systemctl enable rpcbind.service
 92     systemctl enable nfs-server.service
 93     systemctl start rpcbind.service
 94     systemctl start nfs-server.service
 95     
 96 rpcinfo
 97 
 98 showmount -e
 99 mkdir -p /data/www/html/
100 mount -t nfs 192.168.186.135:/share /data/www/html/
101 mount -t nfs 192.168.186.136:/share /data/www/html/
102 mount -t nfs 192.168.186.133:/share /data/www/html/
103 showmount -a
104 chmod -R o+w /share/                                                    #给其他用户添加可写权限
105 }
106 
107 nginx installed
108 nfs installed
View Code

2.编写监控脚本,监控集群内所有服务存活状态,内存、磁盘剩余率检测,异常则发送报警邮件

监控集群内所有服务存活状态,内存、磁盘剩余率检测脚本

 1 #!/bin/sh
 2  
 3 function ngxMonitor(){  #监控nginx服务
 4 ps aux | grep nginx| grep -v grep &>/dev/null
 5 if [ $? -ne 0 ];then
 6     msg="TIME:$(date +%F_%T)
 7          HOSTNAME:$(hostname)
 8          IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
 9          MSG:Nginx program is crash, Waiting to restart"
10     echo $msg
11     /usr/bin/my_mail $msg
12     systemctl restart nginx
13 fi
14 }
15  
16 function nfsMonitor(){ #监控nfs服务
17 ps aux | grep nfs| grep -v grep &>/dev/null
18 if [ $? -ne 0 ];then
19     msg="TIME:$(date +%F_%T)
20          HOSTNAME:$(hostname)
21          IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
22          MSG:NFS program is crash, Waiting to restart"
23     echo $msg
24     /usr/bin/my_mail $msg
25     systemctl restart nginx
26 fi
27 }
28  
29 function memMonitor(){  #监控内存
30 mem_use=`free | awk 'NR==2{print $3}'`
31 mem_total=`free | awk 'NR==2{print $2}'`
32 mem_per=`echo "scale=2;$mem_use/$mem_total"|bc -l |cut -d . -f2`
33  
34 if [ ! -e /usr/bin/bc ];then
35     yum install bc -y -q
36     echo "bc install successful"
37 fi
38 if (( $mem_per > 10 )); then
39     msg="TIME:$(date +%F_%T)
40          HOSTNAME:$(hostname)
41          IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
42          MSG:Memory usage exceeds the limit,current value is ${mem_per}%"
43          echo $msg
44          /usr/bin/my_mail $msg
45 fi
46 }
47  
48 function diskMonitor(){  #监控磁盘
49 space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1`
50  
51 if [ $space_use -gt 80 ];then
52     msg="TIME:$(date +%F_%T)
53          HOSTNAME:$(hostname)
54          IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
55          MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
56     echo $msg
57     /usr/bin/my_mail $msg
58 fi
59 }
60  
61  
62 ngxMonitor  &>>/tmp/monitor.log
63 nfsMonitor  &>>/tmp/monitor.log
64 memMonitor  &>>/tmp/monitor.log
65 diskMonitor &>>/tmp/monitor.log
View Code

邮件脚本如下:

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 import sys
 4 import smtplib
 5 import email.mime.multipart
 6 import email.mime.text
 7 
 8 server = 'smtp.163.com'
 9 port = '25'
10 
11 def sendmail(server,port,user,pwd,msg):
12     smtp = smtplib.SMTP()
13     smtp.connect(server,port)
14     smtp.login(user, pwd)
15     smtp.sendmail(msg['from'], msg['to'], msg.as_string())
16     smtp.quit()
17     print('邮件发送成功email has send out !')
18 
19 
20 if __name__ == '__main__':
21     msg = email.mime.multipart.MIMEMultipart()
22     msg['Subject'] = '你是风儿我是沙,缠缠绵绵回我家'
23     msg['From'] = 'python4_mail@163.com'
24     msg['To'] = 'python4_recvmail@163.com'
25     user = 'python4_mail'
26     pwd = 'sbalex3714'
27     content='%s
%s' %('
'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式处理,专门针对我们的邮件格式
28 
29     txt = email.mime.text.MIMEText(content, _charset='utf-8')
30     msg.attach(txt)
31 
32     sendmail(server,port,user,pwd,msg)
View Code

3.编写计划任务,定时运行监控脚本,完成监控操作

1 [root@bogon ~]# crontab -e
2 */5 * * * * /bin/bash monitor.sh
View Code
原文地址:https://www.cnblogs.com/bingabcd/p/6619208.html