-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.py
executable file
·166 lines (132 loc) · 5.56 KB
/
logging.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python
from subprocess import check_output
from subprocess import CalledProcessError
from subprocess import Popen
from subprocess import PIPE
import datetime
def memory_warning(cutoff=4.0, node='-1', users=None):
bpstat = Popen('bpstat -P {}'.format(str(node)).split(),
stdin=PIPE, stdout=PIPE)
ls = Popen('ps -e -o pid,vsz,user,comm='.split(), stdout=bpstat.stdin)
output = bpstat.communicate()[0]
ls.wait()
MEM = {}
for line in output.split('\n')[1:-1]:
if str(node) == '-1' or str(node) == 'master':
ID = line.split()[0]
mem = float(line.split()[1]) / 1e6
user = line.split()[2]
com = ' '.join(line.split()[3:])
else:
ID = line.split()[1]
mem = float(line.split()[2]) / 1e6
user = line.split()[3]
com = ' '.join(line.split()[4:])
if user not in MEM.keys():
MEM[user] = [mem, [[mem, com, int(ID)]]]
else:
MEM[user][0] += mem
MEM[user][1] += [[mem, com, int(ID)]]
SUM = sum([v[0] for k, v in MEM.iteritems()])
# sort by memory
sort_users = [u for u, m in sorted([[k, v[0]] for k, v in MEM.iteritems()],
key=lambda x: x[1], reverse=True)]
if users is None:
users = sort_users
elif isinstance(users, str):
users = [users]
time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
stdout = '{}\nTotal allocated memory: {:>24.2f} GB\n'.format(time, SUM)
for user in users:
tot, v = MEM[user]
stdout += 'user: {:<34} {:>7.2f} GB\n'.format(user, tot)
if tot > float(cutoff):
stdout += '\n Top 5 processes:\n'
stdout += 'PID MEM (GB) COMM\n'
ind = [i for i, l in sorted(enumerate(v),
key=lambda x: x[1],
reverse=True)]
for i in ind[:5]:
stdout += '{:<7} {:<10.2f} {:}\n'.format(v[i][2],
v[i][0],
v[i][1])
stdout += '\n'
return stdout
def cpu_warning(cutoff=2.0, node='-1', users=None):
bpstat = Popen('bpstat -P {}'.format(str(node)).split(),
stdin=PIPE, stdout=PIPE)
ls = Popen('ps -e -o pid,%cpu,user,comm='.split(), stdout=bpstat.stdin)
output = bpstat.communicate()[0]
ls.wait()
CPU = {}
for line in output.split('\n')[1:-1]:
if str(node) == '-1' or str(node) == 'master':
ID = line.split()[0]
cpu = float(line.split()[1]) / 100
user = line.split()[2]
com = ' '.join(line.split()[3:])
else:
ID = line.split()[1]
cpu = float(line.split()[2]) / 100
user = line.split()[3]
com = ' '.join(line.split()[4:])
if user not in CPU.keys():
CPU[user] = [cpu, [[cpu, com, int(ID)]]]
else:
CPU[user][0] += cpu
CPU[user][1] += [[cpu, com, int(ID)]]
SUM = sum([v[0] for k, v in CPU.iteritems()])
# Sort by CPU usage
sort_users = [u for u, m in sorted([[k, v[0]] for k, v in CPU.iteritems()],
key=lambda x: x[1], reverse=True)]
if users is None:
users = sort_users
elif isinstance(users, str):
users = [users]
time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
stdout = '{}\nCPU load: {:>38.2f} \n'.format(time, SUM)
for user in users:
tot, v = CPU[user]
stdout += 'user: {:<34} {:>7.2f}\n'.format(user, tot)
if tot > float(cutoff):
stdout += '\n Top 5 processes:\n'
stdout += 'PID CPU COMM\n'
ind = [i for i, l in sorted(enumerate(v),
key=lambda x: x[1],
reverse=True)]
for i in ind[:5]:
stdout += '{:<7} {:<10.2f} {:}\n'.format(v[i][2],
v[i][0],
v[i][1])
stdout += '\n'
return stdout
def main():
for n in range(-1, 31):
try:
time = datetime.datetime.now().strftime('%Y-%m-%dT%H:%M')
node_mem = check_output(['bpsh', str(n), 'free'])
mem0, mem1 = node_mem.split()[15:17]
mem = (float(mem0) / (float(mem1) + float(mem0))) * 100.
node_idle = check_output(['bpsh', str(n), 'mpstat'])
# For some reason, the mpstat run by Cron returns military time
# This changes the indexing which is accounted for here.
try:
cpu = 100. - float(node_idle.split()[26])
except(IndexError):
cpu = 100. - float(node_idle.split()[24])
node_load = check_output(['bpsh', str(n), 'uptime'])
load = node_load.split()[-3].rstrip(',')
sto = '{} {:1.2f} {} {} \n'.format(time, mem, cpu, load)
with open('/tmp/logs/node{}.log'.format(n), 'a') as f:
f.write(sto)
if n == -1:
if mem > 80:
with open('/tmp/logs/memory-warning.log', 'a') as f:
f.write(memory_warning())
if float(load) > 12.:
with open('/tmp/logs/cpu-warning.log', 'a') as f:
f.write(cpu_warning())
except(CalledProcessError):
pass
if __name__ == "__main__":
main()