Python access to NFS quota information?
Question
Are there any Python modules that will let me get access to NFS
quota information without having to parse the output of the quota
command? I am aware that if I were sufficiently motivated I could write my own ONC/RPC code using Python's xdrlib
module, but I am hoping with all my heart that I do not have to get that intimately involved with the innards of NFS.
Ideally, I am hoping for an existing RPC implementation, or some higher-level module (like a wrapper for an existing C library or something).
An acceptable substitute would be something like the quota
command that produces more machine-readable output.
Solution
This question was spawned originally by problems with version 3.13 of the quota
command, which is essentially useless as a tool for the end user to determine quota information. For example, if alice
runs quota
, she might see output like this:
Disk quotas for user alice (uid 100):
Filesystem blocks quota limit grace files quota limit grace
fileserver.example.com:/staff/b/bob
9254720 9728000 10240000 119768 0 0
Alice would rightly wonder what this had to do with her account.
It turns out that the minds behind the quota-utils
package at long last realized the problems inherent in the tool; with version 4.00 (available in Fedora 16) there are several new options available that turn the quota
command into something that actually produces useful information.
Of particular interest is the -f
option:
-f, --filesystem-list display quota information only for given filesystems
So now, a user can run:
quota -f ~
And get the quota information for their home directory. Additionally, the -s
option will display "human readable" numbers instead of displaying everything as blocks.
For Alice, this might look like:
$ quota -s -f ~
Disk quotas for user alice (uid 100):
Filesystem blocks quota limit grace files quota limit grace
fileserver.example.com:/staff/a/alice
9038M 9500M 10000M 120k 0 0
Additionally, there are two options that make it much easier to process the output from quota
in a script:
- The
-w
option inhibits the line wrapping when the "filesystem" name is too long. - The
-p
option displays0
for the grace time if the user is not in an over-quota situation. This means that there are always the same number of fields (whereas with the previous version ofquota
the number of fields could vary depending on the situation).
Combining all of the above, we get something like this:
$ quota -wp -f ~
Disk quotas for user alice (uid 100):
Filesystem space quota limit grace files quota limit grace
fileserver.example.com:/staff/a/alice 9254720 9728000 10240000 0 119768 0
Which is much more useful for automation.
OTHER TIPS
command_array = ['quota', '-wp', '-f', '/home/nfs/user1']
output = subprocess.check_output(command_array)
# use only the third line, this is util info
beauty_output = output.split('\n')[2].split()
remote_fs = beauty_output[0]
blocks = beauty_output[1]
quota = beauty_output[2]
limit = beauty_output[3]
...
Consider that /home/nfs/user1 is the nfs mount point. I have done this way.