开发脚本自动部署及监控

开发脚本自动部署及监控

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

        要求:

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

            II、所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;

    2.编写监控脚本,监控集群内所有服务存活状态(需要监控的有所有nginx,nfs

nfs也得监控),内存、磁盘剩余率检测,异常则发送报警邮件

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

1.部署nginx反向代理三个web服务,调度算法使用加权轮询(由于计算机问题,只开2台服务器)

#/bin/bash
systemctl status nginx
if(($?==4))
    then 
    yum install -y nginx
    if(($?==0))
        then
        #echo 'Yes!'
        systemctl start nginx
        if(($?==0))
            then
            echo "Congratulations!! Nginx start OK!!"
        else
            echo "Sorry is Fail!"    
        fi
    else
        echo"sorry install is Fail!"
        
    fi 
elif(($?==3))
    then
    systemctl start nginx
    if(($?==0))
        then
        echo "Congratulations! Nginx start OK!"
    else
        echo "sorry!!"
    fi
elif(($?==0))
    then
    echo "OK!"
else 
    echo "I am so sorry"    
fi
echo "config writing...."
grep 'upstream' /etc/nginx/nginx.conf
if(($?!=0))
    then
    sed -ri '/^http/a upstream Yanlong {' /etc/nginx/nginx.conf
    sed -ri '/^upst/a server yanlongweb1 weight=3;' /etc/nginx/nginx.conf
    sed -ri '/^server yanlongweb1/a server yanlongweb2;' /etc/nginx/nginx.conf
    sed -ri '/^server yanlongweb2/a }' /etc/nginx/nginx.conf
    sed -ri '/^( +)(location)( )(/)/a proxy_pass http://Yanlong;' /etc/nginx/nginx.conf
fi
echo "config write is OK!"
systemctl reload nginx
if(($?==0))
    then
    echo "HTTP load balancer is OK!"
else
    echo "Sorry!!"
fi
systemctl status nfs
if(($?==4))
    then 
    yum install rpcbind nfs-utils -y
    if(($?==0))
        then
        #echo 'Yes!'
        systemctl start nfs
        if(($?==0))
            then
            echo "Congratulations! nfs start OK!"
        else
            echo "Sorry is Fail!!!"    
        fi
    else
        echo"sorry install is Fail!"
        
    fi 
elif(($?==3))
    then
    systemctl start nfs
    if(($?==0))
        then
        echo "Congratulations!! nfs start OK!!"
    else
        echo "sorry!"
    fi
elif(($?==0))
    then
    echo "OKOKOK!"
else 
    echo "I am so sorry"    
fi
echo "config writing...."
echo "/webindex 192.168.16.0/24(rw,sync,fsid=0)" > /etc/exports
echo "config write is OK!"
systemctl reload nfs
if(($?==0))
    then
    echo "NFS service is OK!"
else
    echo "Sorry!!"
fi

2.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性;

#/bin/bash
systemctl status nginx
if(($?==4))
        then
        yum install -y nginx
        if(($?==0))
                then
                #echo 'Yes!'
                systemctl start nginx
                if(($?==0))
                        then
                        echo "Congratulations!! Nginx start OK!"
                else
                        echo "Sorry is Fail!" 
                fi
        else
                echo"sorry install is Fail!"

        fi
elif(($?==3))
        then
        systemctl start nginx
        if(($?==0))
                then
                echo "Congratulations! Nginx start OK!"
        else
                echo "sorry!"
        fi
elif(($?==0))
        then
        echo "OK!"
else
        echo "I am so sorry"    
fi
echo "config writing...."
sed -ri '/^( +)(location)( )(/)/a root /nginxwebservice;' /etc/nginx/nginx.conf
sed -ri '/^root /nginxwebservice/a index web.html;' /etc/nginx/nginx.conf
echo "config write is OK!"
systemctl reload nginx
if(($?==0))
        then
        echo "HTTP load balancer YanlongWEBservice is OK!"
else
        echo "Sorry!"
fi
systemctl status nfs
if(($?==4))
    then 
    yum install rpcbind nfs-utils -y
    if(($?==0))
        then
        #echo 'Yes!'
        systemctl start nfs
        if(($?==0))
            then
            echo "Congratulations!! nfs start OK!"
        else
            echo "Sorry is Fail!"    
        fi
    else
        echo"sorry install is Fail!"
        
    fi 
elif(($?==3))
    then
    systemctl start nfs
    if(($?==0))
        then
        echo "Congratulations! nfs start OK!"
    else
        echo "sorry!"
    fi
elif(($?==0))
    then
    echo "OK!"
else 
    echo "I am so sorry"    
fi
echo "config writing...."
mount -t nfs 192.168.16.120:/webindex/ /nginxwebservice/
echo "config write is OK!"
systemctl reload nfs
if(($?==0))
    then
    echo "NFS service is OK!"
else
    echo "Sorry!!"
fi

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

先写个发送邮件的文件

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

  再写监控脚本

#/bin/bash
bu=`free | awk 'NR==2{print $6}'`
to=`free | awk 'NR==2{print $2}'`
mem=`expr "scale=2;$bu/$to" |bc -l | cut -d. -f2`
if(($mem >= 70))
        then
        msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                 MSG:内存high了!已经用了${mem}%"
            echo $msg
        /usr/bin/pymail.py $msg
fi
systemctl status nginx
if(($?!=0))
        then
                msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                 MSG:   Nginx 进程出现异常请注意查看!"
            echo $msg
        /usr/bin/pymail.py $msg
fi
systemctl status nfs
if(($?!=0))
        then
                msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                 MSG:   NFS 进程出现异常请注意查看!"
            echo $msg
        /usr/bin/pymail.py $msg
fi

  

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

 * * * * * /shell/sysCheck.sh

原文地址:https://www.cnblogs.com/zhangsunan/p/6622136.html