-
Notifications
You must be signed in to change notification settings - Fork 0
/
proccpu.py
495 lines (414 loc) · 15.3 KB
/
proccpu.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/env python
import getopt
import time
import errno
import os
import sys
import re
import atexit
from datetime import datetime
if sys.platform == 'win32':
print "Windows is not supported."
exit(0)
try:
import hashlib
md5_new = hashlib.md5
except ImportError:
import md5
md5_new = md5.new
class Daemon:
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
self.pidfile = pidfile
def daemonize(self):
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(self.stdin, 'r')
so = file(self.stdout, 'a+')
se = file(self.stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
# write pidfile
atexit.register(self.delpid)
pid = str(os.getpid())
file(self.pidfile, 'w+').write("%s\n" % pid)
def delpid(self):
os.remove(self.pidfile)
def start(self):
"""
Start the daemon
"""
# Check for a pidfile to see if the daemon already runs
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if pid:
message = "pidfile %s already exists. proccpu is already running.\n"
sys.stderr.write(message % self.pidfile)
sys.exit(1)
# Start the daemon
self.daemonize()
self.run()
def stop(self):
# Get the pid from the pidfile
try:
pf = file(self.pidfile, 'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
message = "pidfile %s does not exist. proccpu daemon is not running.\n"
sys.stderr.write(message % self.pidfile)
return # not an error in a restart
# Try killing the daemon process
try:
while 1:
os.kill(pid, 7)
time.sleep(0.1)
except OSError, err:
err = str(err)
if err.find("No such process") > 0:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
else:
print str(err)
sys.exit(1)
def restart(self):
self.stop()
self.start()
def run(self):
"""
Override this method with a Daemon subclass
"""
class Proc:
def __init__(self):
self.proc = '/proc'
def path(self, *args):
return os.path.join(self.proc, *(str(a) for a in args))
def open(self, *args):
try:
return open(self.path(*args))
except (IOError, OSError):
val = sys.exc_info()[1]
if val.errno == errno.ENOENT or val.errno == errno.EPERM: # kernel thread or process is gone
raise LookupError
raise
def parse_options():
try:
long_options = ['full-args', 'help', 'total', 'verbose', 'sparse']
opts, args = getopt.getopt(sys.argv[1:], "fhtvsd:p:n:c:w:l:", long_options)
except getopt.GetoptError:
sys.stderr.write(display_help())
sys.exit(3)
if len(args):
sys.stderr.write("Unknown arguments: %s\n" % args)
sys.exit(3)
daemon_cmd = None
logfile_location = None
split_args = False
pids_to_show = None
proc_names_to_show = None
cpus_to_show = None
watch = None
only_total = False
verbose = False
sparse = False
for o, a in opts:
if o in ('-d',):
daemon_cmd = a
if o in ('-l',):
logfile_location = a
if o in ('-f', '--full-args'):
split_args = True
if o in ('-t', '--total'):
only_total = True
if o in ('-v', '--verbose'):
verbose = True
if o in ('-s', '--sparse'):
sparse = True
if o in ('-h', '--help'):
sys.stdout.write(display_help())
sys.exit(0)
if o in ('-n',):
try:
proc_names_to_show = [str(x) for x in a.split(',')]
except:
sys.stderr.write(display_help())
sys.exit(3)
if o in ('-p',):
try:
pids_to_show = [int(x) for x in a.split(',')]
except:
sys.stderr.write(display_help())
sys.exit(3)
if o in ('-c',):
try:
cpus_to_show = [int(x) for x in a.split(',')]
except:
sys.stderr.write(display_help())
sys.exit(3)
if o in ('-w',):
try:
watch = int(a)
except:
sys.stderr.write(display_help())
sys.exit(3)
return daemon_cmd, logfile_location, split_args, pids_to_show, cpus_to_show, \
proc_names_to_show, watch, only_total, verbose, sparse
def display_help():
help_msg = 'Usage: proccpu [OPTION]...\n' \
'\n' \
' -h, -help Show this help\n' \
' -c <cpu1>[,cpu2,...cpuN] Specifies the CPUs to check - threads on all other CPUs are ignored\n'\
' -f, --full-args Show full command-line arguments for processes and tasks\n' \
' -s, --sparse Sparse logging only logs when new processes are started\n' \
' -n <proc1>[,proc2,...procN] Only show threads/processes with the specified names\n' \
' -p <PID1>[,PID2,...PIDN] Only show threads/processes with the specified PIDs\n' \
' -w <N> Check threads/processes every N milliseconds\n' \
' -d (start|stop|restart) Start, stop, or restart proccpu as a daemon\n' \
' -l <logfile> Logfile path when running proccpu as a daemon (-d)\n\n'
return help_msg
def kernel_ver():
kv = proc.open('sys/kernel/osrelease').readline().split(".")[:3]
last = len(kv)
if last == 2:
kv.append('0')
last -= 1
while last > 0:
for char in "-_":
kv[last] = kv[last].split(char)[0]
try:
int(kv[last])
except:
kv[last] = 0
last -= 1
return int(kv[0]), int(kv[1]), int(kv[2]) # (major,minor,release)
def get_cmd_name(pid, split_args):
cmdline = proc.open(pid, 'cmdline').read().split("\0")
if cmdline[-1] == '' and len(cmdline) > 1:
cmdline = cmdline[:-1]
path = proc.path(pid, 'exe')
try:
path = os.readlink(path)
path = path.split('\0')[0]
except OSError:
val = sys.exc_info()[1]
if val.errno == errno.ENOENT or val.errno == errno.EPERM: # either kernel thread or process is gone
raise LookupError
raise
if split_args:
return " ".join(cmdline)
if path.endswith(" (deleted)"):
path = path[:-10]
if os.path.exists(path):
path += " [updated]"
else:
# The path could be have pre-link stuff so try cmdline which might have the full path present.
if os.path.exists(cmdline[0]):
path = cmdline[0] + " [updated]"
else:
path += " [deleted]"
exe = os.path.basename(path)
cmd = proc.open(pid, 'status').readline()[6:-1]
if exe.startswith(cmd):
cmd = exe # show non truncated version
return cmd
def get_cpu_affinity(pids_to_show, proc_names_to_show, cpus_to_show, split_args, include_self=True, only_self=False):
tasks = {}
for procpid in os.listdir(proc.path('')):
if not procpid.isdigit():
continue
try:
proccmd = get_cmd_name(procpid, split_args)
except LookupError:
continue
for pid in os.listdir(proc.path(procpid, 'task')):
if not pid.isdigit():
continue
pid = int(pid)
if only_self and pid != our_pid:
continue
if pid == our_pid and not include_self:
continue
if pids_to_show is not None and pid not in pids_to_show:
continue
try:
if os.path.exists(proc.path(procpid, 'task', pid, 'stat')): # stat
for line in proc.open(procpid, 'task', pid, 'stat').readlines():
cpu = re.split('\(.*\)| ', line)[39]
if cpus_to_show is None or int(cpu) in cpus_to_show:
add = 1
cmd = get_cmd_name(pid, split_args)
if proc_names_to_show is not None:
add = 0
if cmd in proc_names_to_show or proccmd in proc_names_to_show:
add = 1
if add == 1:
tasks[pid] = (cpu, cmd, proccmd)
except LookupError:
# kernel threads don't have exe links or process is gone
continue
return tasks
def verify_environment():
if os.geteuid() != 0:
sys.stderr.write("Root permission is required.\n")
if __name__ == '__main__':
sys.stderr.close()
sys.exit(1)
try:
kv = kernel_ver()
except (IOError, OSError):
val = sys.exc_info()[1]
if val.errno == errno.ENOENT:
sys.stderr.write("Couldn't access " + proc.path('') + "\nOnly GNU/Linux is supported\n")
sys.exit(2)
else:
raise
def std_exceptions(exception_type, value, tb):
sys.excepthook = sys.__excepthook__
if issubclass(exception_type, KeyboardInterrupt):
pass
elif issubclass(exception_type, IOError) and value.errno == errno.EPIPE:
pass
else:
sys.__excepthook__(exception_type, value, tb)
def find_pids(process_names):
include_self = True
only_self = False
pids = []
for pid in os.listdir(proc.path('')):
if not pid.isdigit():
continue
pid = int(pid)
if only_self and pid != our_pid:
continue
if pid == our_pid and not include_self:
continue
if pids_to_show is not None and pid not in pids_to_show:
continue
try:
cmd = get_cmd_name(pid, False)
for name in process_names:
if name == cmd:
pids.append(pid)
except LookupError:
# kernel threads don't have exe links or process is gone
continue
return pids
class ProcCPUDaemon(Daemon):
def run(self):
while True:
proccpu_main(True)
def print_cpu_usage(tasks, last_tasks=None, sparse=False, daemonize=False, logfile=None, proc_names_to_show=None):
sorted_tasks = [x for x in tasks.iteritems()]
sorted_tasks.sort(key=lambda x: x[1][0]) # sort by cpu
if sparse and last_tasks is not None:
if last_tasks is not None: # if this isn't the first time the process has been seen
last_sorted_tasks = [x for x in last_tasks.iteritems()]
last_sorted_tasks.sort(key=lambda x: x[1][0]) # sort by cpu
new_tasks = set(sorted_tasks).difference(last_sorted_tasks)
for newtask in new_tasks:
if daemonize:
logfile.write(str(datetime.now()) + ": ")
logfile.write("%4s%s %9s %20s \t%s%s%s\n" % ("CPU-", newtask[1][0], newtask[0], newtask[1][1], "(", newtask[1][2], ")"))
logfile.flush()
else:
sys.stdout.write(str(datetime.now()) + ": ")
sys.stdout.write("%4s%s %9s %20s \t%s%s%s\n" % ("CPU-", newtask[1][0], newtask[0], newtask[1][1], "(", newtask[1][2], ")"))
else:
for task in sorted_tasks:
if daemonize:
if sparse:
logfile.write(str(datetime.now()) + ": ")
logfile.write("%4s%s %9s %20s \t%s%s%s\n" % ("CPU-", task[1][0], task[0], task[1][1], "(", task[1][2], ")"))
logfile.flush()
else:
if sparse:
sys.stdout.write(str(datetime.now()) + ": ")
sys.stdout.write("%4s%s %9s %20s \t%s%s%s\n" % ("CPU-", task[1][0], task[0], task[1][1], "(", task[1][2], ")"))
def proccpu_main(daemonize=False):
logfile = None
tasks = None
daemon_cmd, logfile_location, split_args, pids_to_show, cpus_to_show, proc_names_to_show, \
watch, only_total, verbose, sparse = parse_options()
if daemonize:
if logfile_location is None:
logfile_location = "/var/log/proccpu.log"
logfile = open(logfile_location, 'w')
if daemonize:
logfile.write(" CPU\tPID\tProcess\t( Parent )\n\n")
logfile.flush()
else:
sys.stdout.write(" CPU\tPID\tProcess\t( Parent )\n\n")
if watch is not None:
try:
while True:
if not sparse:
timestamp = datetime.now()
if daemonize:
logfile.write("\n**** " + str(timestamp) + " ****\n\n")
logfile.flush()
else:
print "\n****", timestamp, "****\n"
last_tasks = tasks
tasks = get_cpu_affinity(pids_to_show, proc_names_to_show, cpus_to_show, split_args)
print_cpu_usage(tasks, last_tasks, sparse, daemonize, logfile, proc_names_to_show)
time.sleep(watch / 1000) # milliseconds
except KeyboardInterrupt:
pass
else:
tasks = get_cpu_affinity(pids_to_show, proc_names_to_show, cpus_to_show, split_args)
print_cpu_usage(tasks, None, sparse, daemonize, logfile, proc_names_to_show)
sys.stdout.close()
if daemonize:
logfile.close()
# Globals
sys.excepthook = std_exceptions
our_pid = os.getpid()
proc = Proc()
if __name__ == '__main__':
daemon_cmd, logfile_location, split_args, pids_to_show, cpus_to_show, \
proc_names_to_show, watch, only_total, verbose, sparse = parse_options()
verify_environment()
daemon = ProcCPUDaemon('/var/run/proccpu.pid')
if daemon_cmd is not None:
if daemon_cmd == 'start':
print "proccpu daemon STARTED"
daemon.start()
elif daemon_cmd == 'stop':
print "proccpu daemon STOPPED"
daemon.stop()
elif daemon_cmd == 'restart':
print "proccpu daemon RESTARTED"
daemon.restart()
else:
proccpu_main()