This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquota_log.py
executable file
·52 lines (42 loc) · 1.8 KB
/
quota_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Generates dictionaries for the load on the quota using 'uquota'. If a couchdb
is specified, the dictionaries will be sent there. Otherwise prints the dictionaries.
"""
import argparse
import datetime
import subprocess
from platform import node as host_name
from pprint import pprint
import couchdb
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=("Formats uquota information as "
"a dict, and sends it to a given CouchDB."))
parser.add_argument("--server", dest="server", action="store", help=("Address "
"to the CouchDB server. Add authentication like this if needed: user:password@couchdb_server.domain"))
parser.add_argument("--db", dest="db", action="store", default="uppmax",
help="Name of the CouchDB database")
args = parser.parse_args()
current_time = datetime.datetime.now()
uq = subprocess.Popen(["/sw/uppmax/bin/uquota", "-q"], stdout=subprocess.PIPE)
output = uq.communicate()[0]
projects = output.split("\n/proj/")[1:]
project_list = []
for proj in projects:
project_dict = {"time": current_time.isoformat()}
project = proj.strip("\n").split()
project_dict["project"] = project[0]
project_dict["usage (GB)"] = project[1]
project_dict["quota limit (GB)"] = project[2]
try:
project_dict["over quota"] = project[3]
except IndexError:
# Projects that are not over quota will not have this field, because
# uquota command only outputs a * in case the project account is over quota
pass
project_list.append(project_dict)
if not args.server:
pprint(project_list)
else:
couch = couchdb.Server(args.server)
db = couch[args.db]
for fs_dict in project_list:
db.save(fs_dict)