forked from tias/simple-top-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
executable file
·133 lines (111 loc) · 4.25 KB
/
stats.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
#!/usr/bin/python
# gather linux system statistics and output in html/php for this machine
#import commands
from subprocess import *
import sys
import getpass
import os
import time
import csv
NUMPROCS=100
DIR="/home/guyvdb/www/top"
EXT="dat"
########
output = ""
# uptime information:
uptime = Popen("uptime", shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True).communicate()[0].strip()
output += "<tr><td colspan=\"6\">%s</td></tr>"%uptime
load = uptime.split('load average: ')[1].split(', ')
procs = Popen("ps axw -o user:25,nice,pcpu,pmem,etime,args --sort -pcpu | head -n %i"%(NUMPROCS+3), shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True).communicate()[0].strip().split("\n")
#p = Popen("top -c -b -n1 | head -n14", shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True)
#procs = p.stdout.readlines()
#procs = commands.getoutput("top -b -n1 | head -15").split("\n")
totcpu = 0
totmem = 0
users = []
nprocs = 0
for proc in procs:
# only display and count NUMPROCS processes
if nprocs > NUMPROCS:
break
d = [x for x in proc.strip().split(" ") if x != '']
#dd = [x for x in procs[i].strip().split(" ") if x != '']
#cols = [1,3,8,9,10,11]
#d = [dd[i] for i in cols]
this_user = getpass.getuser()
this_script = os.path.abspath(__file__)
# ignore (<3% cpu and <3%ram) comands and cron, plymouth as well as this script
if (d[2] != '%CPU' and (float(d[2]) <= 3 and float(d[3]) <= 3)) or \
(d[0] == 'root' and d[5] == '/USR/SBIN/CRON') or \
(d[0] == 'root' and d[5] == 'CRON') or \
d[5] == '/sbin/plymouthd' or \
d[5] == '/usr/sbin/unity-greeter' or \
(d[0] == this_user and d[5] == 'crond') or \
(d[0] == this_user and d[5] == '[head]') or \
(d[0] == this_user and d[5] == 'head' and d[6] == '-n') or \
(d[0] == this_user and d[5] == 'ps' and d[6] == 'axw') or \
(d[0] == this_user and d[5] == '/bin/sh' and d[7] == 'ps' and d[8] == 'axw') or \
d[-1] == this_script or \
(d[5] == '/usr/bin/python' and d[6] == this_script):
continue
# busy process in bold
if d[2] != '%CPU' and float(d[2]) >= 50:
d[0] = "<b>%s</b>"%d[0]
# condor in italic
if d[0] == "condor":
d[0] = "<i>%s</i>"%d[0]
# root and the header are not real people
if not (d[0] in ("root","USER")):
users.append(d[0])
output += "<tr><td>%s</td><td>%s</td></tr>"%("</td><td>".join(d[0:5]), " ".join(d[5:]))
nprocs += 1
try:
totcpu += float(d[2])
totmem += float(d[3])
except ValueError:
continue
# divide by number of cpus
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
totcpu = totcpu/ncpus
p = Popen("hostname", shell=True, stdout=PIPE, close_fds=True)
hostname = p.stdout.readline().strip().lower().replace('.cs.ucla.edu','')
#hostname = commands.getoutput("hostname")
query_attributes = ['index','name','utilization.gpu','utilization.memory','memory.total','memory.free','memory.used']
gpu_info = dict()
gpus = list()
for att in query_attributes:
gpu_info[att]=dict()
gpu = False
###GPU
try:
p2 = Popen(['nvidia-smi', '--query-gpu='+','.join(query_attributes), '--format=csv'], stdout=PIPE, close_fds=True)
gpu_csv = csv.reader(p2.stdout, skipinitialspace=True)
headers = gpu_csv.next()
for row in gpu_csv:
index = row[0]
name = hostname+'_'+index
gpus.append(name)
for i,att in enumerate(query_attributes):
gpu_info[att][name] = row[i]
gpu = True
except Exception as e:
pass
###
#query_attributes = [s.replace('.','') for s in query_attributes]
output = "<tr><td colspan=\"6\"><b>%s</b> (CPU:%.1f%% - MEM:%.1f%%)</td></tr>"%(hostname, totcpu,totmem)+output
f = open("%s/%s.%s"%(DIR,hostname,EXT), "w")
f.write("<?php\n")
f.write("$cpu['%s'] = %.1f;\n"%(hostname,totcpu))
if gpu:
f.write("$gpu['{}'] = array('{}');\n".format(hostname,"', '".join(gpus)))
f.write("$mem['%s'] = %.1f;\n"%(hostname,totmem))
f.write("$load['%s'] = array('%s');\n"%(hostname,"', '".join(load)))
f.write("$users['%s'] = array('%s');\n"%(hostname,"', '".join(users)))
f.write("$time['%s'] = %s;\n"%(hostname, time.time()))
f.write("$output['%s'] = '%s';\n"%(hostname,output.encode('string_escape')))
if gpu:
for att in query_attributes:
for g in gpus:
f.write("${}['{}'] = '{}';\n".format(att.replace('.',''),g,gpu_info[att][g]))
f.write("?>")
f.close()