linux基礎 作業篇

來源:互聯網
上載者:User

標籤:set   nbsp   nfs-utils   smtplib   pcb   linux基礎   logs   scale   cut   

1.自動部署反向 Proxy web nfs 

#!/usr/bin/python#-*- coding:utf-8 -*-#開發指令碼自動部署及監控#1.編寫指令碼自動部署反向 Proxy、web、nfs;#!/bin/bash#檢測安裝nginxfunction detection_nginx(){    if [ -f /etc/nginx/nginx.conf ] #判斷nginx檔案是否存在        then            echo ‘nginx has been installed‘            exit    else        then             yum install epel-release -y            yum install nginx -y            echo ‘nginx already installed‘    fi    }#改寫設定檔function modify_nginx_conf(){    msg=‘upstream Pythonweb{ server 192.168.205.129 weight=3;server 192.168.205.129;server 192.168.205.129}‘    sed -ri "/^http/a $msg" /etc/nginx/nginx.conf    #增加upstream    sed -ri "/^ *location \/ \{$/a proxy_pass http://Pythonweb\;" /etc/nginx/nginx.conf  #修改localtion}systemctl reload nginx #重新載入nginx#檢測安裝nfs和rpcbindfunction detection_nfs(){    if [ -d /var/lib/nfs ]                     then            echo ‘nfs has been installed‘            exit            else        then            yum install rpcbind nfs-utils -y             echo ‘rpcbind nfs-utils already installed‘    fi}function start_service(){    #建立共用目錄    mkdir /share    #給使用者增加寫的許可權    chmod -R o+w /share/    #改寫nfs的設定檔    echo ‘/share 192.168.205.0/24(rw,sync,fsid=0)‘ >> /etc/exports #以追加的方式寫入    #啟動服務    systemctl start rpcbind.server    systemctl start nfs-utils.server    #設定開機啟動    systemctl enable nfs-server.service    systemctl enable rpcbind.service}detection_nginx      #執行檢測安裝nginx函數modify_nginx_conf   #執行改寫nginx.conf函數detection_nfs        #執行檢測安裝nfs函數start_service        #執行啟動服務函數
View Code

2.監控主機的記憶體,超過閾值則發送警示郵件

#!/usr/bin/python#-*- coding:utf-8 -*-題目:監控主機的記憶體,超過閾值則發送警示郵件1.準備發送郵件的代碼#!/usr/bin/python# -*- coding: UTF-8 -*-import sysimport smtplibimport email.mime.multipartimport email.mime.textserver = ‘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‘] = ‘[email protected]‘    msg[‘To‘] = ‘[email protected]‘    user = ‘python4_mail‘    pwd = ‘sbalex3714‘    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)#把上述代碼放在/usr/bin/my_mail 中vim /usr/bin/my_mail #把代碼拷貝到裡面chmod +x my_mail #給檔案my_mail 可以執行的許可權2.建立我自己的指令碼監控檔案15.sh#!/bin/bashmen = 0 #記憶體使用量超過0%就會警示function monitor_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 ]        then            msg=‘TIME:$(date +%F_%T)‘            HOSTNAM:$(hostname)            IPADDR:$(ifconfig | awk ‘NR==2{print $2}‘)            echo $msg            /usr/bin/my_mail $mag    fi}#3.編寫計劃任務crontab -e -u root* * * * * /root/home/gandong/15.sh restart #每一分鐘檢測一次
View Code

 

linux基礎 作業篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.