獲取服務器分區占用最大的文件
阿新 • • 發佈:2018-03-16
class 轉換成 需要 exc 獲取 服務 sys.argv 大小 urn
#!/usr/bin/python #-*- coding: utf-8 -*- import time import re import os from os.path import join, getsize from sys import stdout import getopt,sys #參數 def pygrep(argv): grep = ‘‘ sgrep = ‘‘ Disk_usage = ‘‘ The_number = ‘‘ Space_occupied_files = ‘‘ km = ‘‘ lv = ‘‘ try: opts, args= getopt.getopt(argv,"hg:e:d:t:o:k:l:",["grep=","egrep=","Disk_usage=","The_number=","Space_occupied_files=","km=","lv="]) except getopt.GetoptError: print ‘‘‘-g\t--grep 設置需要過濾掉的字符,默認不過濾 -e\t--egrep 自定義grep參數,默認為-Ev -d\t--Disk_usage 設置空間百分比,默認80% -t\t--The_number 設置顯示的條數,all顯示全部,默認10條 -o\t--Space_occupied_files 設置已占用空間的百分比顯示,默認100% -k\t--km 查找文件的大小,默認10M -l\t--lv 指定磁盤,默認根據設置的空間百分比查找磁盤,當使用此參數則-d參數失效‘‘‘ sys.exit(2) for opt, arg in opts: if opt == ‘-h‘: print ‘‘‘-g\t--grep 設置需要過濾掉的字符,默認不過濾 -e\t--egrep 自定義grep參數,默認為-Ev -d\t--Disk_usage 設置空間百分比,默認80% -t\t--The_number 設置顯示的條數,all顯示全部,默認10條 -o\t--Space_occupied_files 設置已占用空間的百分比顯示,默認100% -k\t--km 查找文件的大小,默認10M -l\t--lv 指定磁盤,默認根據設置的空間百分比查找磁盤,當使用此參數則-d參數失效‘‘‘ sys.exit() elif opt in ("-g", "--grep"): grep = arg elif opt in ("-e", "--egrep"): sgrep = arg elif opt in ("-d", "--Disk_usage"): Disk_usage = arg elif opt in ("-t", "--The_number"): The_number = arg elif opt in ("-o", "--Space_occupied_files"): Space_occupied_files= arg elif opt in ("-k", "--km"): km=arg elif opt in ("-l", "--lv"): lv=arg return grep,sgrep,Disk_usage,The_number,Space_occupied_files,km,lv #轉換 def GBK(dir,rootdisk,The_number): tmp=[] num=0 lnum=len(dir.split(‘\n‘)) if lnum == 1: num=1 tmp.append(dir.split(‘ ‘)[1]) elif lnum > 1: for i in dir.split(‘\n‘): if i != ‘‘: if The_number != None: if len(tmp) >= The_number: break num += 1 tmp.append(i.split(‘ ‘)[1]) cmd="du -h %s"%‘ ‘.join(tmp) tmp=os.popen(cmd).read() cmd = "df -h | grep ‘%s‘"%rootdisk ttmp=os.popen(cmd).read() return ttmp,tmp,num #主要方法 def serverdisk(Disk_usage,km,Space_occupied_files,The_number,lgrep,lv): tmp=[] #獲取硬盤空間 cmd = "df -P | sed ‘1d‘ " if lv != "": cmd = "df -P | sed ‘1d‘ | grep -E ‘%s‘"%lv #將Disk_usage設置為0 Disk_usage=0 disk=os.popen(cmd).read() disklist=[] rootdisk=None rootdiskM=None rootdiskdir=None #轉換成列表 for i in disk.split(‘\n‘): disklist.append(i.split()) for i in disklist: if len(i) == 0: continue #已用空間大於等於Disk_usage的 if int(i[4].replace(‘%‘,‘‘)) >= Disk_usage : #分區 rootdisk=i[0] #分區已用空間 rootdiskM=i[2] #分區的目錄 rootdiskdir=i[5] #獲取分區下的目錄 cmd = "ls %s"%rootdiskdir dir=os.popen(cmd).read() dirdisk=[] #獲取分區的目錄 for ii in dir.split(): cmd = "df -P %s/%s | sed ‘1d‘ | awk ‘{print $1}‘"%(rootdiskdir,ii) dirsystem=os.popen(cmd).read() #判斷目錄是否為當前獲取的分區 if dirsystem.split()[0] == rootdisk.split()[0]: dirdisk.append(‘%s/%s‘%(rootdiskdir,ii)) if dirdisk: dirdisk=‘ ‘.join(dirdisk) #獲取空間小於等於Disk_usage,文件大於10M,當已用空間大於等於rootdiskM時停止尋找 cmd = "find %s -size +%s -exec du {} \;%s| sort -rn|awk ‘{if($2!=\".\"){num=$1+num;z=num/mx*100;if(z <= %s){print $1,$2}else{ print $1,$2;exit;}}}‘num=0 z=0 mx=%s "%(dirdisk,km,lgrep,Space_occupied_files,rootdiskM) tt=os.popen(cmd).read() if ‘‘ == tt.replace(‘\n‘,‘‘): tmp.append(‘沒有獲取到,調整find -size參數重試‘) return tmp break if "/" not in tt: tmp.append(‘無法獲取到請檢查參數是否正確‘) return tmp break #將上面獲取到的文件信息轉換為GBK GBKrootdisk,tttmp,xians_num=GBK(tt,rootdisk,The_number) zong_num_tmp=[] for ii in tt.split(‘\n‘): if ii != ‘‘ and len(ii) > 0: zong_num_tmp.append(ii) zong_num = len(zong_num_tmp) tmp.append(‘顯示了 %s 條,共有 %s 條\r\n%s%s‘%(xians_num,zong_num,GBKrootdisk.replace(‘\r\n‘,‘‘),tttmp)) return tmp #初始化參數 def start(): ttmp=0 tttmp=[] #條數 The_number=int(‘10‘) #文件占用 Space_occupied_files=int(‘100‘) #使用率 Disk_usage=int(‘80‘) #lgrep="|grep -v ‘‘" km="10M" lv="" lgrep,sgrep,newDisk_usage,newThe_number,newSpace_occupied_files,newkm,newlv=pygrep(sys.argv[1:]) if lgrep != "" and sgrep == "": lgrep="|grep -Ev ‘%s‘"%lgrep elif lgrep != "" and sgrep != "": lgrep="|grep %s ‘%s‘"%(sgrep,lgrep) if newDisk_usage != "": Disk_usage=int(newDisk_usage) if newThe_number != "" and newThe_number == "all": The_number=None elif newThe_number != "": The_number=int(newThe_number) if newSpace_occupied_files != "": Space_occupied_files=int(newSpace_occupied_files) if newkm != "": km=newkm if newlv != "": lv=newlv a=serverdisk(Disk_usage,km,Space_occupied_files,The_number,lgrep,lv) tttmp.append(‘%s‘%(‘\r\n‘.join(a))) return tttmp print ‘%s‘%‘\r\n‘.join(start());
獲取服務器分區占用最大的文件