• 开发脚本自动部署及监控


    开发脚本自动部署及监控

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

         要求:

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

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

     部署ngix代理机 

    #!/bin/bash
    systemctl status nginx
    #查看nginx启动状态
    if(($?==4))
    #没有安装nginx
        then
            yum install -y nginx
            if(($?==0))
                then
                    echo 'install nginx success'
                    systemctl start nginx
                    if(($?==0))
                        then
                            echo 'Congratulations!!Nginx is running!!'
                    else
                        echo 'Sorry is fail!!'
                    fi
            else
                echo 'Sorry install is fail'
            fi
    elif(($==3))
    #安装了nginx没有启动
        then
            systemctl start nginx
            if(($?==0))
                then
                    echo 'Congratulations!!Nginx is running!!'
                else
        echo 'Sorry is fail!!'
            fi
    elif(($?==0))
    #已经启动nginx
        then
            echo echo 'Congratulations!!Nginx is running!!'
    else
        echo 'I am so sorry' 
    
    fi
    echo 'nigix configuration information'
    grep 'upstream myapp1' /etc/nginx/nginx.conf
    #配置ngixn反向代理
    if(($?!=0))
        then
            sed -ri '/^hettp/a 
    	 upstream myapp1 { 
    	 server 192.168.203.136 weight=3;
    	  server 192.167.203.137;
    	 }' /etc/nginx/nginx.conf
            sed -ri '/^ *location / {/a 
    	 proxy_pass http://myapp1;' /etc/nginx/nginx.conf
    fi
    echo 'configuration information is ok'
    systemctl stop firewalld
    #修改完配置重新启动nginx
    systemctl reload nginx
    echo 'HTTP load balancer is OK!'

    部署nfs服务器端

    #!/bin/bash
    systemctl status nfs
    #查看nfs启动状态
    if(($?==4))
    #没有安装nfs
        then
            yum install rpcbind nfs-utils -y
            if(($?==0))
                them
                    systemctl start nfs
                    if(($?==0))
                        then
                            echo 'Congratulation!!nfs is running!'
                    else
                        echo 'Sorry!nfs does not running !!'
                    fi
            else
                echo 'Sorry,install nfs is fail!!'
            fi
    else((&?==3))
    #安装没有启动nfs
        then 
            systemctl start nfs
            if(($?==0))
                then 
                    echo 'Congratulation!!nfs is running!'
            else 
                echo 'Sorry,nfs in death'
            fi
    else(($?==0))
    #已经启动nfs
        then 
            echo 'Congratulation!!nfs is running!'
    else 
        echo 'nfs is missing'
    fi
    echo 'nfs configuration information'
    echo '/fenxiang  192.168.203.0/24(rw,sync,fsid=0)'>/etc/exports
    #设计nfs共享目录
    echo 'nfs configuration information is ok'
    chmod +w /fenxiang
    #给予客户端写权限
    systemctl reload nfs
    #修改完配置重新启动nfs
    if(($?==0))
        then
            echo 'nfs is running!'
    else
        echo 'nfs is missing'
    fi

    部署nginx服务端

    #!/bin/bash
    systemctl status nginx
    #查看启动nginx没有
    if(($?==4))
    #没有安装nginx
        then
            yum install -y nginx
            if(($?==0))
                then
                    echo 'install nginx success'
                    systemctl start nginx
                    if(($?==0))
                        then
                            echo 'Congratulations!!Nginx is running!!'
                    else
                        echo 'Sorry is fail!!'
                    fi
             else
                echo 'Sorry install is fail'
            fi
    elif(($==3))
    #安装了ngix,但是没有启动
        then
            systemctl start nginx
            if(($?==0))
                then
                    echo 'Congratulations!!Nginx is running!!'
                else
        echo 'Sorry is fail!!'
            fi
    elif(($?==0))
    #已经启动nginx
        then
            echo echo 'Congratulations!!Nginx is running!!'
    else
        echo 'I am so sorry' 
    fi
    echo 'nigix configuration information'
    sed -ri '/^ *location {/a 
    	 root /var/www/html;' /ect/nginx/nginx.conf
    #修改nginx服务端的配置,指定访问路径
    echo 'configuration information is ok'
    systemctl stop firewalld
    systemctl reload nginx
    if(($?==0))
        then
            echo 'HTTP load balancer is OK'
    else
        echo 'HTTP load balancer is not OK'
    fi
    systemctl status nfs
    #是否启动nfs
    if(($?==4))
    #没有安装nfs
        then
            yum install rpcbind nfs-utils -y
            if(($?==0))
                them
                    systemctl start nfs
                    if(($?==0))
                        then
                            echo 'Congratulation!!nfs is running!'
                    else
                        echo 'Sorry!nfs does not running !!'
                    fi
            else
                echo 'Sorry,install nfs is fail!!'
            fi
    else((&?==3))
    #安装了nfs,但是没有启动
        then 
            systemctl start nfs
            if(($?==0))
                then 
                    echo 'Congratulation!!nfs is running!'
            else 
                echo 'Sorry,nfs in death'
            fi
    else(($?==0))
    #已经启动nfs
        then 
            echo 'Congratulation!!nfs is running!'
    else 
        echo 'nfs is missing'
    fi
    echo 'nfs mount directory'
    mount -t nfs  192.168.203.138:/fengxiang /var/www/html
    #将自己的目录挂载到nfs服务端的分享目录下
    echo 'nfs mount directory is OK'
    systemctl reload nfs
    if(($?==0))
        then
            echo 'nfs is running!'
    else
        echo 'nfs is missing'
    fi

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

     部署每台机器

    一、准备发送邮件的脚本,将其拷贝到/usr/bin/my_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'] = 'check your service of nginx and nfs'
        msg['From'] = 'python4_mail@163.com'
        msg['To'] = 'python4_recvmail@163.com'
        user = 'python4_mail'
        pwd = '123456789'
        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)

     二、编写监控脚本check.sh,监控集群内nginx和nfs存活状态,内存、磁盘剩余率检测 

    #!/bin/bash
    #监控脚本,监控集群内nginx和nfs存活状态,内存、磁盘剩余率检测,异常则发送报警邮件
    function NginxCheck{
    #监控nginx
        systemctl status nginx
        nginx=$?
        clear
        if(($nginx != 0))
            msg="TIME:$(date +%F_%T)
                HOSTNAME:$(hostname)
                IPADDR:$(ifconfig | awk 'NR==2{print $2}')
                MSG:nginx service is crash ,plese restart it"
            echo $msg
            /usr/bin/my_mail $msg
            systemctl restart nginx
            
        fi
    }
    
    function NfsCheck{
    #监控nfs
        systemctl status nfs
        nfs=$?
        clear
        if(($nfs !=0))
        then
            msg="TIME:$(date +%F_%T)
                    HOSTNAME:$(hostname)
                    IPADDR:$(ifconfig | awk 'NR==2{print $2}')
                    MSG:nfs service is crash ,plese restart it"
            echo $msg
        /usr/bin/my_mail $msg
        systemctl restart nginx    
    
        fi
    }
    
    function MenCheck{
    #监控内存
        men_total=`free |awk 'NR==2{print $2}'`
        men_use=`free |awk 'NR==2{print $3}'`
        men_per=`echo "scale=2;$men_use/$men_totall"|bc -l|cut -d . -f2`
        if[! -e /use/bin/bc]
            then
                yum install bc -y -q
            echo 'bc install successful'
        fi
        if(($men_per>10))
            then
            msg="TIME:$(date +%F_%T)
                    HOSTNAME:$(hostname)
                    IPADDR:$(ifconfig | awk 'NR==2{print $2}')
                    MSG:memory usage exceeds the limit, current value is ${men_per}%"
                    echo $msg
                    /usr/bin/my_mail $msg
        fi    
    }
    function DiskCheck{
    #监控磁盘
        space_use=`df |awk 'NR==2{print $5}'|cut -d % -f1`
        if(($space_use>80))
            then
                msg="TIME:$(date +%F_%T)
                    HOSTNAME:$(hostname)
                    IPADDR:$(ifconfig | awk 'NR==2{print $2}')
                    MSG:disk usage exceeds the limit, current value is ${space_use}%"
                    echo $msg
                    /usr/bin/my_mail $msg
        fi
        
    }

      三、chmod +x /usr/bin/my_mail  chmod +x check.sh  放开执行权限

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

      crontab -e -u root  编写计划任务

    * * * * * /usr/bin/sh    /root/check.sh 
  • 相关阅读:
    全站仪定向距离差 方向不差 这样敢放线吗
    关于老王
    cad巧用插件自定义填充图形
    老王教你永不会错的测量坐标方位角计算方法
    jqgrid 点击列头的超链接或按钮时,不触发列排序事件
    jqgrid 将列头设置为超链接或按钮
    jqgrid 设置隔行换色
    jqgrid 设置行编辑为本地端编辑状态
    jqgrid 让隐藏的列在编辑状态时出现且可编辑
    jqgrid 设置编辑行中的某列为下拉选择项
  • 原文地址:https://www.cnblogs.com/domestique/p/6611460.html
Copyright © 2020-2023  润新知