標籤: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基礎 作業篇