python獲取quota的使用者配額資訊
python獲取quota的使用者配額資訊
# !/usr/bin/env python
# -*-coding:utf-8-*-
import os
import subprocess
class CUserQuotaInfo(object):
def __init__(self):
self.userId = ""
self.usedKB = 0 # 已用 單位KB
self.softKB = 0 # 配額 單位KB
self.diskInfo = "" # "*** Report for user quotas on device /dev/mapper/vg3-home02"
def getUserSpaceInfo(seIf, userId):
def __init__(self):
self.cmdQueryQ = "repquota -auvc"
"""
獲取使用者配額和已使用空間
[
10001 +- 307208 100000 200000000 5days 5 0 0
:param userid: 工號如10001
:return:userUsed使用者已用空間,userSoftQ配額 單位:Kb
"""
try:
cmdUserQuerQ = seIf.cmdQueryQ+"grep"+userId
p = subprocess.Popen(cmdUserQuerQ, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if p.returncode != 0:
print("error code :", p.returncode, cmdUserQuerQ, p.communicate())
return None
val = p.communicate()
strQuery = val[0]
userQuotaInfo = CUserQuotaInfo()
userQuotaInfo.userId = strQuery.split()[0]
userQuotaInfo.usedKB = strQuery.split()[2]
userQuotaInfo.softKB = strQuery.split()[3]
return userQuotaInfo
except Exception as e:
print(e)
return None
執行 subprocess.Popen()執行repquota -auvc 獲取對應的shell輸出結果,對於結果進行過濾出想要的結果,需要持續優化中