Python之路-shell&計划任務


開發腳本自動部署及監控
1.編寫腳本自動部署反向代理、web、nfs;
要求: I、部署nginx反向代理三個web服務,調度算法使用加權輪詢;
           II、所有web服務使用共享存儲nfs,保證所有web都對其有讀寫權限,保證數據一致性;

          編寫的部署腳本有兩個,分別為nginx部署腳本和nfs部署腳本,根據IP檢測要部署到的主機是服務器或者是客戶端,識別出來后,執行不同的安裝配置操作。

a.部署nginx

#!/bin/bash
#Deploy_Web   to : deploy nginx
function Pre_Action(){
        yum clean all
        yum install epel* -y
         if [ $? -eq 0 ]
                then
                echo 'epel_package install right'
        else
                echo 'epel_package install ERROR!!!'
        fi
        yum clean all
        yum install nginx -y
        if [ $? -eq 0 ]
        then
                echo 'nginx_package install right'
        else
                echo 'nginx_pacage install ERROR'
        fi
}
function Close_Wall(){
        systemctl stop firewalld                                                  #close firewall
        setenforce 0                                                              #close selinux
        iptables –L                                                              #clear tables
        systemctl disable firewalld #disable
}                                                                                                           
function Restart_service(){
        systemctl start nginx   #start nginx
        if [ $? -eq 0 ]
        then
                echo 'nginx_service start!!!'
        else
                systemctl restart nginx
                if [ $? -eq 0 ]
                then
                         echo 'nginx_service start!!!'
                else
                         echo 'nginx_servive is not good!!!'
                fi
        fi
}
function Cilent_conf(){
        Pre_Action
        Close_Wall
        Restart_service
}
function Server_conf(){
        Cilent_conf
        msg='upstream mserver{ server 192.168.0.115 weight=3;server 192.168.0.116;server 192.168.0.117; }'  #訪問web115的權重為3
        sed -ri "/^http/a $msg" /etc/nginx/nginx.conf                                                       #增加upstream
        sed -ri "/^ *location \/ \{$/a proxy_pass http://mserver\;" /etc/nginx/nginx.conf                   #修改localtion
        systemctl restart nginx                                                                             #重啟nginx服務
}

ipaddr=`ifconfig|awk 'NR==2{print $2}'`
echo $ipaddr
if [ "$ipaddr" = '192.168.0.114']
then
        Server_conf
else
        Cilent_conf
fi

b.部署nfs

#!/bin/bash
#start nfs_service
function nfs_pre(){
        yum clean all
        yum install rpcbind nfs-utils -y
        if [ $? -eq 0 ]
        then
                echo 'nfs_package install right!!!'
        else
                echo 'nfs_package install ERROR!!!'
                exit
        fi
}

function Server_conf(){
        mkdir /share
        echo '/share 192.168.0.0/24(rw,sync,fsid=0)' > /etc/exports
#       head -1 /etc/exports
        chmod -R 777 /share
}
function Client_conf(){
#       mkdir  /data_disk
        mount -t nfs 192.168.0.118:/share /usr/share/nginx/html
        if [ $? -eq 0 ]
        then
                 echo 'mount successful!!!'
        else
                echo 'mount failed!!!'
                exit
        fi
}

function nfs_start(){
        systemctl enable nfs-server.service
        systemctl enable rpcbind.service

        systemctl start rpcbind.service
        if [ $? -eq 0 ]
        then
                echo 'rpcbind start!!!'
        else
                systemctl restart rpcbind.service
                if [ $? -eq 0 ]
                then
                         echo 'rpcbind_service start!!!'
                else
                         echo 'rpcbind_servive is not good!!!'
                fi
        fi
        systemctl start nfs-server.service
        if [ $? -eq 0 ]
        then
                echo 'nfs-server start!!!'
        else
                systemctl restart nfs-server.service
                if [ $? -eq 0 ]
                then
                         echo 'nfs-server start!!!'
                else
                         echo 'nfs-server is not good!!!'
                fi
        fi
        exportfs
}
ipaddr=`ifconfig|awk 'NR==2{print $2}'`
echo $ipaddr
if [ "$ipaddr" = '192.168.0.118' ]
then
        nfs_pre
        Server_conf
        nfs_start
else
        nfs_pre
        nfs_start
        Client_conf
        systemctl restart nginx
        if [ $? -eq 0 ]
        then
                echo 'nfs-server restart!!!'
        else
                echo 'nfs-server restart fail!!!'
        fi
fi

c.在部署了一台nginx反向代理服務器,三台web服務器和一台nfs共享存儲服務器后,在共享服務器的/share目錄下創建了文件suc.txt。用瀏覽器訪問nginx反向代理服務器的IP驗證。

image

2.編寫監控腳本,監控集群內所有服務存活狀態,內存、磁盤剩余率檢測,異常則發送報警郵件

a.要監控的服務有nginx和nfs是否關閉,還監控着內存和磁盤剩余率,其中磁盤剩余分為inode使用情況和內存空間使用情況

b.第三方郵件

[root@ym script]# vim /usr/bin/my_mail

具體使用參考 http://www.cnblogs.com/linhaifeng/p/6602149.html#_label7

c.監控腳本如下

#!/bin/bash
cpu_limit=50 #cpu使用超過50%
mem_limit=50 #內存使用超過50%發送郵件
disk='/dev/sda1' #需要監控的磁盤名
disk_inode_limit=50 #磁盤inode使用
disk_space_limit=50 #磁盤空間使用
function m_nginx(){
        ps aux |grep nginx|grep -v grep &> /dev/null
        if [ $? -eq 0 ]
        then
                echo 'nginx is normal'
        else
                 msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(/usr/sbin/ifconfig|awk 'NR==2{print $2}')
                 MSG:nginx is stop!!!"
                 echo $msg
                 /usr/bin/my_mail $msg
        fi
}

function m_nfs(){
        ps aux |grep nfs|grep -v grep &> /dev/null
        if [ $? -eq 0 ]
        then
                echo 'nfs is normal'
        else
                 msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(/usr/sbin/ifconfig|awk 'NR==2{print $2}')
                 MSG:nfs is stop!!!"
                 echo $msg
                 /usr/bin/my_mail $msg
        fi
}
function m_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:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
                 MSG:Memory usage exceeds the limit,current value is ${mem_per}%"
            echo $msg
            /usr/bin/my_mail $msg
    fi
}
function m_disk_inode(){
    inode_use=`df -i $disk |awk 'NR==2{print $5}' |cut -d% -f1`
    if [ $inode_use -gt $disk_inode_limit ]
        then
            msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
                 MSG:Disk inode usage exceeds the limit,current value is ${inode_use}%"
            echo $msg
            /usr/bin/my_mail $msg
    fi
}

function m_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:$(/usr/sbin/ifconfig |awk 'NR==2{print $2}')
                 MSG:Disk space usage exceeds the limit,current value is ${space_use}%"
            echo $msg
            /usr/bin/my_mail $msg
    fi
}
m_nginx &>> /tmp/monitor.log
m_nfs &>> /tmp/monitor.log
m_mem &>> /tmp/monitor.log
m_disk_inode &>> /tmp/monitor.log
m_disk_space &>> /tmp/monitor.log

d.執行驗證

$V90(4N$H3G]SM433}~Z)I5

3.編寫計划任務,定時運行監控腳本,完成監控操作

a.制定計划任務

[root@ym script]# crontab -e -u root

b.計划任務內容

*     *     *     *      *    command

分   時    日    月     周               具體參考  man 5 crontab

注意:盡量全部使用絕對路徑(包括命令,也使用命令的絕對路徑)

c.顯示已經制定的計划任務

[root@ym script]# crontab -l

image 

d.監控正在執行的計划任務

[root@ym script]# tail -f /var/log/cron

image

e. 郵件驗證

D}{FN8Y97`FFY}CTDRXUR1H


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM