我正在尝试编写一个 bash 脚本,该脚本将以可预测的单位告诉我给定分区的默认“阻止宽限时间”。到目前为止,我发现最接近的方法是使用 repquota 并解析输出,但它不一致。有时它会报告天数,如下所示“7days”,如您在第三行看到的那样。
[root@hostname]# repquota -g -p /
*** Report for group quotas on device /dev/mapper/vg_in1-lv_root
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
Group used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 2135928 0 0 0 59727 0 0 0
bin -- 480 0 0 0 28 0 0 0
tty -- 24 0 0 0 2 0 0 0
[...]
对于较短的时间段,它会报告为“4:10”,即 4 小时 10 分钟。对于小于 30 秒的值,它会报告 00:00,对于介于 30 秒和 1 分钟之间的值,它会报告 00:01。
如何才能获得实际设置的块宽限时间值,而无需使其更“人类可读”,以便程序能够可靠地解析它?
谢谢!
答案1
我们在 GPFS 配额报告中也遇到了类似的问题。最基本的解决方案是使用正则表达式,以您希望遇到的任何形式获取其中的内容,然后在下一步中对其进行分析。例如在 Python 中
#!/usr/bin/env python
import re
import sys
GRACE_REGEX = re.compile(r"Block grace time: (?P<days>\d+)\s*days?|(?P<hours>\d+):(?P<minutes>\d+)")
for line in sys.stdin.readlines():
grace = GRACE_REGEX.search(line)
if not grace:
continue
grace_groups = grace.groupdict()
if grace_groups.get('days', None):
print "Found days: %d or in seconds: %d" % (
int(grace_groups['days']),
int(grace_groups['days']) * 86400)
if grace_groups.get('hours', None):
print "Found hours: %d and minutes: %d or in seconds: %d" % (
int(grace_groups['hours']),
int(grace_groups['minutes']),
int(grace_groups['hours']) * 3600 + int(grace_groups['minutes']) * 60)
给出示例行
Block grace time: 7days; Inode grace time: 7days
Block grace time: 4:10; Inode grace time: 7days
您将获得以下输出:
Found days: 7 or in seconds: 604800
Found hours: 4 and minutes: 10 or in seconds: 15000
答案2
您可以使用它quotatool
来获取现有块和 inode 宽限时间(以秒为单位),例如:
quotatool -v -n -u -b -t "1 day" /home
quotatool: using uid (null)
quotatool: working with block limits
quotatool: using filesystem /home
quotatool: filesystem /home has device node /dev/xvda4
quotatool:
quotatool: Limit Old New
quotatool: ----- --- ---
quotatool: block grace: 23500 86400
quotatool -v -n -u -i -t "1 day" /home
quotatool: using uid (null)
quotatool: working with inode limits
quotatool: using filesystem /home
quotatool: filesystem /home has device node /dev/xvda4
quotatool:
quotatool: Limit Old New
quotatool: ----- --- ---
quotatool: inode grace: 9400000 86400
然后你可以老的最后一行的列值。请注意,上面的输出将发送到标准错误,而不是标准输出,并且旧列和新列以制表符分隔,而不是空格分隔。
因此,使用 Bash 您可以执行以下操作:
quotatool -v -n -u -b -t "1 day" /home 2>&1 | tail -n1 | awk '{ print $4 }'
23500
quotatool -v -n -u -i -t "1 day" /home 2>&1 | tail -n1 | awk '{ print $4 }'
9400000