forked from sfstpala/Usage-Log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage_log.py
47 lines (43 loc) · 1.62 KB
/
usage_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
import json
import time
import random
import subprocess
import configparser
def usagelog_config():
config = configparser.ConfigParser()
config.read('usagelog.cfg')
return config
def log(filename):
with open(filename, "a") as f:
c = usagelog_config()
_, ps = subprocess.getstatusoutput("ps aux")
x = [i.split() for i in ps.split("\n")[1:]]
x = [i[:4] + [i[9]] + [' '.join(i[10:])] for i in x]
result = {}
t_cpu, t_mem = 0, 0
for user, pid, cpu, mem, duration, cmd in x:
t_cpu += float(cpu)
t_mem += float(mem)
if user in result:
result[user]["cpu"] += float(cpu)
result[user]["mem"] += float(mem)
result[user]["exec"].append({"duration": duration, "command": cmd})
else:
result[user] = {"cpu": float(cpu), "mem": float(mem)}
result[user]["exec"] = [{"duration": duration, "command": cmd}]
t = time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime())
f.write(json.dumps({t: result}) + "\n")
return t_cpu, t_mem
if __name__ == '__main__':
log_n_times_per_day = 1000
interval = (24 * 60 * 60) / log_n_times_per_day
d = time.strftime("%Y-%m-%dZ", time.gmtime())
t = time.strftime("%Y-%m-%d %H:%M:%SZ", time.gmtime())
c = usagelog_config()
while True:
t_cpu, t_mem = log("%s/" % c.get('general', 'log_dir') + "usage-%s.log" % d)
try:
print(t, " cpu: %6.2f" % t_cpu, " mem: %6.2f" % t_mem)
except IOError:
pass
time.sleep(interval * (random.random() * 2))