一個監控未釋放已刪除文件空間的腳本
阿新 • • 發佈:2017-12-27
字符串 最終 in use usr cat 白名單 pytho system 存儲空間
具體需求:
1、 需要分析出是視頻/data分區個類文件占比(實際文件占比多少,一般實際文件小於占比70%以下大多為已刪除文件單未釋放磁盤空間)。
2、 需要統計已刪除文件但未釋放空間的大小(可參考lsof命令)。
3、 根據1和2最終分析結果拿出占比較大的服務列表(針對服務列表建議支持白名單),針對服務列表對已在擺明單內的服務進行重啟釋放存儲空間,未在白名單內的可進行列表打印。
#!/usr/bin/python #coding:utf-8 import os import subprocess import types #文件占比 data_top10 = "du -sk /* |sort -n -t ‘ ‘ -k 1" #已刪除未釋放 data_used = "lsof |grep delete|awk -F ‘ ‘ ‘{print $1,$8}‘" #目前占比 data_now = " df -h |grep /dev/vda1 |awk -F ‘ ‘ ‘{print $5}‘|awk -F ‘%‘ ‘{print $1}‘" def subprocess_caller(cmd): try: p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True) output, error = p.communicate() except OSError, e: print ‘SUBPROCEEE_CALLER function: execute command failed, message is %s‘ % e return dict(output = [], error = [], code = 1) else: return dict(output = output, error = error, code = 0) gele = {} used = {} dic = {} #lsof查看沒有釋放的文件 def lsof_look(): #獲得字符串將其轉換成列表 temp2 = [] str2 = ‘‘ for memeda in used[‘output‘]: if memeda !=‘ ‘ and memeda !=‘\n‘: str2 += memeda else: temp2.append(str2) str2 = ‘‘ #print len(temp2) #lsof的列表,列表的拆分 list3 = [] list4 = [] for i in range(len(temp2)): if i%2 == 0: list3.append(temp2[i]) else: list4.append(temp2[i]) #為了解決最後一個不能匹配的問題 list3.append(‘test‘) list4.append(‘0‘) #解決統計服務與大小的問題 list5 = [] summ = 0 for i in range(len(list3)-1): if list3[i] == list3[i+1]: summ += float(list4[i])/1024 #print summ else: summ += float(list4[i])/1024 if dic.has_key(list3[i]): dic[list3[i]] += summ else: dic[list3[i]] = summ summ = 0 for key in dic: print ‘服務:‘+key,‘所占的空間為(kb):‘,(dic[key]) #分析十個使用量最高的目錄與文件情況 def filerate(): #將字符串轉成列表 temp = [] str = ‘‘ f_dict = {} for memeda in gele[‘output‘]: if memeda != ‘\t‘ and memeda != ‘\n‘: str += memeda else: temp.append(str) str = ‘‘ #將兩個列表合成字典 list1=[] list2=[] for i in range(len(temp)): if i % 2 == 0: # if "K" in temp[i]: temp[i]=float(temp[i]) # elif ‘M‘ in temp[i]: #temp[i]=float(temp[i].strip(‘M‘))*1024 # else: #temp[i]=float(temp[i].strip(‘G‘))*1024*1024 list1.append(temp[i]) else: list2.append(temp[i]) f_dict = dict(zip(list2,list1)) sss = 0 for key in f_dict: t = f_dict[key]/41943040.0*100 sss += t print ‘目錄:‘+key,‘所占實際百分比為:%.2f%%‘ % (t) print ‘=================總占實際比為:%.2f%%‘%(sss) #print sss return sss if __name__ == ‘__main__‘: # 各類文件的使用大小情況 gele = subprocess_caller(data_top10) #print gele["output"] used = subprocess_caller(data_used) #print used[‘output‘] #df -h 所顯示的磁盤占比,這個不是正常的,將其轉化為70%的狀態 now = subprocess_caller(data_now) now_dick_used = float(now[‘output‘])*0.7 #print now_dick_used k = filerate() print("\n") lsof_look() print("\n") #不可刪除的服務名單 immobilization_list = [‘mylala‘,‘apache‘] flag = 0 #獲取服務字典裏面的鍵值 key = dic.keys() if k<now_dick_used: for the_key in key: if the_key in immobilization_list: continue else: #cmd = "killall " + the_key #os.system(cmd) print "\033[1;35m已經殺死該服務:\033[0m",the_key flag = 1 if flag == 0: print "\033[1;35m系統狀態正常!\033[0m"
一個監控未釋放已刪除文件空間的腳本