一個監控掛載盤的python指令碼

來源:互聯網
上載者:User

標籤:掛載   python   監控指令碼   

我們產品線有一個公用的掛載盤,主要是用來方便各位開發放置他們自己的一些工作材料,比如異常的日誌,或者tcpdump的抓包等等,但是這個盤由於使用人眾多,容量自然要有監控,於是就有了寫這個指令碼的動機。


在這裡我寫了兩個指令碼,上面這個是用來監控磁碟容量,然後通過df -h的排序產生前十名占容量最大的檔案夾,把這個檔案夾的名字和對應的大小重新導向到一個叫alarm.txt這個檔案裡,這個檔案就是郵件內文。然後在確定他們的主人,得到他們主人的郵箱地址,最後用下面那個指令碼來發送郵件:

#!/usr/bin/env python# coding=utf-8import osimport AutoMailimport commands#設定變數判斷是否掛載和掛載盤的容量mount = commands.getoutput("mount | grep ‘:.*nfs‘|wc -l")size = commands.getoutput("df -h | grep share | awk ‘{print $5}‘ | cut -d ‘%‘ -f 1")#建立發郵件的文字檔def Createlist():    if os.path.exists(‘/root/chenscript/alarm.txt‘) == True:        os.system("python /root/chenscript/weixin_sharealarm.py")        print ("警示已經發送!")        os.system("cd /root/chenscript; echo ‘share盤容量大於80%,現在將調出容量排名前十位的檔案夾名字及對應的容量,請各位處理一下不需要的檔案!‘ >>alarm.txt")        os.system("cd /share ;du -s * --exclude=‘yunwei‘ --exclude=‘General_LeChange_Chn*‘ --exclude=‘Lecheng_Open*‘ |sort -nr |head >>/root/chenscript/alarm.txt")        os.system("cd /share ;du -s * --exclude=‘yunwei‘ --exclude=‘General_LeChange_Chn*‘ --exclude=‘Lecheng_Open*‘ |sort -nr |head|awk \‘{print $2\"@dahuatech.com\"}\‘  >>/root/chenscript/list.txt")        os.system("echo ‘\n‘ >> /root/chenscript/alarm.txt")    if os.path.exists(‘/root/chenscript/alarm.txt‘) == False:        os.system("cd /root/chenscript;touch alarm.txt")        def Sendmail():        fp = open(‘/root/chenscript/alarm.txt‘, ‘r‘)        content = fp.read()        AutoMail.send_mail(‘share掛載盤容量大於80%!請各位整理自己對應的檔案夾!‘, content)        os.system("cd /root/chenscript/;rm -f list.txt;echo ‘[email protected]‘>list.txt")#將郵件的檔案重新整理def Dellist():        os.system("cd /root/chenscript/;rm -f alarm.txt;touch alarm.txt")if mount == ‘1‘ and size >= ‘80‘:        print ("掛載盤存在!")        print ("share盤容量大於80%...")        Createlist()        Sendmail()        Dellist()elif mount == ‘1‘ and size < ‘80‘:        print ("掛載盤存在!")        print ("share盤容量正常...")else:        print ("掛載盤不存在,現在重新掛載...")        os.system("mount -t nfs -o acl,rw,intr,soft,nolock,rsize=8192,wsize=8192 10.160.43.172:/share /share ")
#!/usr/bin/env python#coding=utf-8import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.application import MIMEApplicationmailto_list=[]    #這裡為空白list,會從list.txt裡一行一行的當做元素添加進來with open(‘/root/chenscript/list.txt‘,‘r‘) as f:    f=f.readlines()for i in f:        i=i.strip(‘\n‘)        mailto_list.append(i)mail_host="這裡填寫郵箱主機"mail_user="這裡填寫發送人的郵箱地址"mail_pass="發送人的郵箱密碼"mail_postfix="dahuatech.com"mail_sender="與mail_host內容相同"def send_mail(sub, content):    me=mail_sender    msg = MIMEMultipart()    msg[‘Subject‘] = sub    msg[‘From‘] = me    msg[‘To‘] = ";".join(mailto_list)    content1 = MIMEText(str(content), ‘plain‘, ‘utf-8‘)    msg.attach(content1)    try:        s = smtplib.SMTP()        s.connect(mail_host)        s.login(mail_user,mail_pass)        s.sendmail(me, mailto_list, msg.as_string())        print(‘發送成功!\n‘)        s.close()    except Exception as e: print(str(e))


執行的效果如下:

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M00/06/98/wKiom1m6i9azyByDAABqTWMs_tc175.png" title="1.png" alt="wKiom1m6i9azyByDAABqTWMs_tc175.png" />


隱藏的知識點!

1)du -s 是按照位元組來統計,“--exclude=‘yunwei‘”是在排序的時候忽略掉yunwei這個檔案夾,容後再用sort -nr|head是得到從大到小前10名,如果得到後10名就是sort -nr|tail;

2)如果使用的是import commands,那麼commands.getoutput得到的是字串

3)用“mount | grep ‘:.*nfs‘”來判斷掛載盤是否存在是一個很簡單的方式,如果掛了多個,就用ip in的方式來進一步判斷;

4)要一行一行的讀取檔案,就readline;

5)python按行讀取檔案,去掉分行符號"\n"的方法:           

for line in file.readlines():    line=line.strip(‘\n‘)


本文出自 “生活就是等待戈多” 部落格,請務必保留此出處http://chenx1242.blog.51cto.com/10430133/1965358

一個監控掛載盤的python指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.