|
Lines 34-40
Link Here
|
| 34 |
import time |
34 |
import time |
| 35 |
import psutil |
35 |
import psutil |
| 36 |
|
36 |
|
| 37 |
import univention.info_tools as uit |
|
|
| 38 |
import univention.management.console as umc |
37 |
import univention.management.console as umc |
| 39 |
import univention.management.console.modules as umcm |
38 |
import univention.management.console.modules as umcm |
| 40 |
from univention.management.console.log import MODULE |
39 |
from univention.management.console.log import MODULE |
|
Lines 45-62
from univention.management.console.modules.sanitizers import PatternSanitizer
Link Here
|
| 45 |
|
44 |
|
| 46 |
_ = umc.Translation('univention-management-console-module-top').translate |
45 |
_ = umc.Translation('univention-management-console-module-top').translate |
| 47 |
|
46 |
|
|
|
47 |
|
| 48 |
def kill(process): |
48 |
def kill(process): |
| 49 |
if hasattr(process, 'terminate'): |
49 |
if hasattr(process, 'terminate'): |
| 50 |
process.kill() |
50 |
process.kill() |
| 51 |
return |
51 |
return |
| 52 |
process.kill(15) |
52 |
process.kill(15) |
| 53 |
|
53 |
|
|
|
54 |
|
| 54 |
def terminate(process): |
55 |
def terminate(process): |
| 55 |
if hasattr(process, 'terminate'): |
56 |
if hasattr(process, 'terminate'): |
| 56 |
process.terminate() |
57 |
process.terminate() |
| 57 |
return |
58 |
return |
| 58 |
process.kill(9) |
59 |
process.kill(9) |
| 59 |
|
60 |
|
|
|
61 |
|
| 60 |
class Instance(umcm.Base): |
62 |
class Instance(umcm.Base): |
| 61 |
@sanitize(pattern=PatternSanitizer(default='.*')) |
63 |
@sanitize(pattern=PatternSanitizer(default='.*')) |
| 62 |
def query(self, request): |
64 |
def query(self, request): |
|
Lines 64-84
class Instance(umcm.Base):
Link Here
|
| 64 |
pattern = request.options.get('pattern') |
66 |
pattern = request.options.get('pattern') |
| 65 |
processes = [] |
67 |
processes = [] |
| 66 |
for process in psutil.process_iter(): |
68 |
for process in psutil.process_iter(): |
| 67 |
listEntry = {} |
69 |
try: |
| 68 |
# Temporary variables; used to calculate cpu percentage |
70 |
(user_time, system_time, ) = process.get_cpu_times() |
| 69 |
listEntry['timestamp'] = [] |
71 |
listEntry = { |
| 70 |
listEntry['cpu_time'] = [] |
72 |
'timestamp': time.time(), |
| 71 |
listEntry['timestamp'].append(time.time()) |
73 |
'cpu_time': user_time + system_time, |
| 72 |
(user_time, system_time, ) = process.get_cpu_times() |
74 |
'user': process.username, |
| 73 |
listEntry['cpu_time'].append(user_time + system_time) |
75 |
'pid': process.pid, |
|
|
76 |
'cpu': 0.0, |
| 77 |
'mem': process.get_memory_percent(), |
| 78 |
'command': ' '.join(process.cmdline) or process.name, |
| 79 |
} |
| 80 |
except psutil.NoSuchProcess: |
| 81 |
continue |
| 74 |
|
82 |
|
| 75 |
listEntry['user'] = process.username |
|
|
| 76 |
listEntry['pid'] = process.pid |
| 77 |
listEntry['cpu'] = 0.0 |
| 78 |
listEntry['mem'] = process.get_memory_percent() |
| 79 |
listEntry['command'] = ' '.join(process.cmdline) |
| 80 |
if listEntry['command'] == '': |
| 81 |
listEntry['command'] = process.name |
| 82 |
if category == 'all': |
83 |
if category == 'all': |
| 83 |
for value in listEntry.itervalues(): |
84 |
for value in listEntry.itervalues(): |
| 84 |
if pattern.match(str(value)): |
85 |
if pattern.match(str(value)): |
|
Lines 93-112
class Instance(umcm.Base):
Link Here
|
| 93 |
for process_entry in processes: |
94 |
for process_entry in processes: |
| 94 |
try: |
95 |
try: |
| 95 |
process = psutil.Process(process_entry['pid']) |
96 |
process = psutil.Process(process_entry['pid']) |
|
|
97 |
(user_time, system_time, ) = process.get_cpu_times() |
| 96 |
except psutil.NoSuchProcess: |
98 |
except psutil.NoSuchProcess: |
| 97 |
pass |
99 |
pass |
| 98 |
else: |
100 |
else: |
| 99 |
process_entry['timestamp'].append(time.time()) |
101 |
elapsed_time = time.time() - process_entry['timestamp'] |
| 100 |
(user_time, system_time, ) = process.get_cpu_times() |
102 |
elapsed_cpu_time = user_time + system_time - process_entry['cpu_time'] |
| 101 |
process_entry['cpu_time'].append(user_time + system_time) |
|
|
| 102 |
|
| 103 |
elapsed_time = process_entry['timestamp'][1] - process_entry['timestamp'][0] |
| 104 |
elapsed_cpu_time = process_entry['cpu_time'][1] - process_entry['cpu_time'][0] |
| 105 |
cpu_percent = (elapsed_cpu_time / elapsed_time) * 100 |
103 |
cpu_percent = (elapsed_cpu_time / elapsed_time) * 100 |
| 106 |
process_entry['cpu'] = cpu_percent |
104 |
process_entry['cpu'] = cpu_percent |
| 107 |
# Cleanup request result |
105 |
# Cleanup request result |
| 108 |
del process_entry['timestamp'] |
106 |
del process_entry['timestamp'] |
| 109 |
del process_entry['cpu_time'] |
107 |
del process_entry['cpu_time'] |
| 110 |
|
108 |
|
| 111 |
request.status = SUCCESS |
109 |
request.status = SUCCESS |
| 112 |
self.finished(request.id, processes) |
110 |
self.finished(request.id, processes) |