-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfsquota2xdmod.py
70 lines (62 loc) · 1.64 KB
/
lfsquota2xdmod.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Script to refurn Lustre quota as a JSON object for ingesting in to XDMoD"""
import subprocess
import datetime
import json
def storage(username, filesystem="/lustre"):
"""Returns a dict"""
lfs_quota = subprocess.run(
[
"lfs",
"quota",
"-q",
"-u",
username,
filesystem,
],
check=True,
stdout=subprocess.PIPE,
)
fields = lfs_quota.stdout.decode("utf-8")
[
filesystem,
kbytes,
quota,
limit,
_,
files,
_,
_,
_,
] = fields.split()
iso_dt = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
usage = {
"resource": "Lustre",
"mountpoint": filesystem,
"user": username,
"pi": username,
"dt": iso_dt,
"soft_threshold": int(quota) * 1024,
"hard_threshold": int(limit) * 1024,
"file_count": int(files.strip('*')),
"logical_usage": int(kbytes.strip('*')) * 1024,
"physical_usage": int(kbytes.strip('*')) * 1024,
}
return usage
def all_users():
"""Returns a list of all the usernames"""
cmd = subprocess.run(
["ipa", "user-find", "--sizelimit=0"],
check=True,
stdout=subprocess.PIPE,
)
usernames = []
for line in cmd.stdout.decode("utf-8").split("\n"):
if "User login: " in line:
usernames.append(line.split()[2])
return usernames
if __name__ == "__main__":
metrics = []
for user in all_users():
metrics.append(storage(user))
print(json.dumps(metrics, indent=2))