# !/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):
"""
获取所有用户配额和已使用空间
[root@gpu-test01 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接口的话,此种方案比较实用进行功能的简单实现操作