python獲取quota的全部使用者配額資訊
# !/usr/bin/env python
# -*-coding:utf-8-*-
"""
file: sambaCenter.py
date: 2018/11/24
version: 1.0
"""
import os
import subprocess
class CFileInfo(object):
def __int__(self):
self.fileType = 0 # 0:dir 1:file
self.fileName = ""
self.fileSize = 0 # Byte# ,if dir 0
self.parentDir = "" # "/home/10001"
class CSambaCenter(object):
def __init__(self):
self.cmdQueryQ = "repquota -auvc"
def getUsersSpaceInfo(seIf):
"""
獲取所有使用者配額和已使用空間
[[email protected] tmp]# repquota -auvc
*** Report for user quotas on device /dev/mapper/vg3-home02
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 152313644 0 0 956351 0 0
nobody -- 124 0 0 5 0 0
10001 +- 307208 100000 200000000 5days 5 0 0
10002 -- 4 100000000 200000000 1 0 0
10003 -- 4 100000000 200000000 1 0 0
10004 -- 4 100000000 200000000 1 0 0
10005 -- 4 100000000 200000000 1 0 0
10006 -- 4 100000000 200000000 1 0 0
Statistics:
Total blocks: 8
Data blocks: 1
Entries: 8
Used average: 8.000000
*** Report for user quotas on device /dev/mapper/vg1-home
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 220 0 0 25 0 0
nobody -- 134624400 0 0 66564 0 0
Statistics:
Total blocks: 7
Data blocks: 1
Entries: 2
Used average: 2.000000
:return:list使用者,已用空間和配額
"""
try:
p = subprocess.Popen(seIf.cmdQueryQ, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if p.returncode != 0:
print("error code :", p.returncode, seIf.cmdQueryQ, p.communicate())
return None
val = p.communicate()
listQ = str.splitlines()
diskInfo = "Report for user quotas on device"
findStart = "----------------------------------------------------------------------"
findEnd = ""
for c in (0, listQ.count(diskInfo)):
print("quotas on device:", c)
startI = listQ.index(findStart, start=0)
endI = listQ.index(findEnd, start=startI)
listUserQuotaInfo = []
for num in (startI - 1, endI - 1):
strQuery = val[num]
userQuotaInfo = CUserQuotaInfo()
userQuotaInfo.userId = strQuery.split()[0]
userQuotaInfo.usedKB = strQuery.split()[2]
userQuotaInfo.softKB = strQuery.split()[3]
listUserQuotaInfo.append(userQuotaInfo)
startI = listQ.index(findStart, start=endI)
endI = listQ.index(findEnd, start=startI)
for num in (startI - 1, endI - 1):
strQuery = val[num]
userQuotaInfo = CUserQuotaInfo()
userQuotaInfo.userId = strQuery.split()[0]
userQuotaInfo.usedKB = strQuery.split()[2]
userQuotaInfo.softKB = strQuery.split()[3]
listUserQuotaInfo.append(userQuotaInfo)
return listUserQuotaInfo
except Exception as e:
print(e)
return None
完全不夠友好,若有更好方案,還請賜教,剛接觸python,練手功能實現,通過抓取 subprocess.Popen(seIf.cmdQueryQ, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)shell指令碼的返回資訊獲取,效能上有待更一步優化, 若無效能需求,呼叫頻率不夠,基本滿足功能性需求,對於系統沒有提供的介面,或者Linux下的低三方工具沒有對應的api介面的話,此種方案比較實用進行功能的簡單實現操作