shell腳本自動部署及監控


一、shell腳本部署nginx反向代理和三個web服務

1 對反向代理服務器進行配置


#!/bin/bash

#修改用戶交互頁面 用戶輸入參數執行相應的參數
#安裝epel擴展包和nginx
function ng_yum_install(){

    yum install epel-release -y
    yum install nginx -y
    yum install rpcbind nfs-utils -y

}
#nginx init function 
#nginx status
#nginx 開機enable
function ng_init(){

    systemctl start nginx
    systemctl enable nginx
}

#腳本執行
ng_yum_install
sed -ri '/^http/a \ \ \ \ upstream pythonweb{\n server\ 192.168.43.21;weight=3;\n server\ 192.168.43.23;\nserver\ 192.168.43.24;\n}' /etc/nginx/nginx.conf  #四個反斜杠是四個空格
sed -ri '/^ +location \/ /a proxy_pass http:\/\/pythonweb;' /etc/nginx/nginx.conf

systemctl stop firewalld
systemctl enable firewalld

echo 'share 192.168.43.0/24(rw,sync,fsid=0)' >/etc/exports
 chmod -R o+w /share #修改share目錄的權限
systemctl enable nfs-service.service
systemctl enable rpcbind.service
systemctl start rpcbind.service
systemctl start nfs_service.service


2 對三台web服務器分別進行配置

function ng_yum_install(){

    yum install epel-release -y
    yum install nginx -y
    yum install rpcbind nfs-utils -y #安裝rpcbind和nfs
    systemctl enable rpcbind.service && systemctl start rpcbind.service

}
#nginx init function 
#nginx status
#nginx 開機enable
function ng_init(){

    systemctl start nginx
    systemctl enable nginx
}


ng_yum_install

mkdir /html
touch /html/index.html
echo 'welcome nginx' >/html/index.html
 sed -ri '/^ +location \/ /a root \/html;\nindex index.html; ' /etx/nginx/nginx.conf


ng_init 
systemctl stop firewalld
systemctl enable firewalld

mount -t nfs 192.168.43.20:/share /var/www.html

二、編寫監控腳本 服務 內存 磁盤使用率 異常報警##

ps aux |grep nginx |grep -v 'grep'
if[ $? -ne 0 ]
then
        echo 'nginx is die'
        systemctl start nginx
        if[  $? -eq 0  ]
        then
                echo 'nginx now is activing'
        fi
fi

1 Python 發送郵件工具

將此文件放到/bin下並給予可執行權限

#!/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'] = 'python'
    msg['From'] = 'example@163.com' #發件人地址
    msg['To'] = 'example@163.com' #收件人地址
    user = '郵箱用戶名'
    pwd = 'stmp客戶端密碼'
    content='%s\n%s' %('\n'.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 內存監控腳本


#!/bin/bash

mem_limit=0  #測試的時候設置成0  后期根據實際需要設置
function memcheck(){
memtotal=`free |awk 'NR==2{print $2}'`
memuse1=`free |awk 'NR==2{print $3}'`
memuse2=`free |awk 'NR==2{print $6}'`
mempercent=`echo "scale=2;($memuse1+$memuse2)/$memtotal"|bc -l |cut -d. -f2`
#echo ${mempercent}%

if [ $mempercent -gt $mem_limit ]
        then
            msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                 MSG:Memory usage exceeds the limit,current value is ${mem_percent}%"
            echo $msg
           /usr/bin/mail $msg
fi

}

memcheck   #執行函數

下面是shell中執行成功的界面

這是郵箱收到的信息

3 計划任務crond

crond默認是開機啟動的

crontab -e -u root  #  為root用戶添加計划任務

* * * * *  

*代表分鍾、小時、日、月、周
crontab -l  查看上次添加的計划任務


tail -f /var/log/cron  # 查看日志


免責聲明!

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



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