forked from rebelnz/monitor
/
psstats.py
57 lines (49 loc) · 1.88 KB
/
psstats.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
import psutil
import commands
import platform
class PsutilStats(object):
def loadPlatform(self):
x = platform.uname()
return x
def loadCommandUptime(self):
x = commands.getoutput('uptime')
return x
def loadCommandDf(self):
x = commands.getoutput('df -h')
return x
def loadSummaryData(self):
summary_data = {}
summary_data['cpu'] = [psutil.cpu_percent()]
summary_data['memory'] = [psutil.phymem_usage().percent]
summary_data['disk'] = [psutil.disk_usage('/').percent]
summary_data['virtual'] = [psutil.virtmem_usage().percent]
summary_data['uptime'] = commands.getoutput('uptime')
return summary_data
def getTopProcesses(self,limit=None):
m = {}
for x in psutil.get_process_list():
cpu_time = x.get_cpu_times()[1]
if not cpu_time:
continue
k = x.pid; v = {"pid":x.pid, "name":x.name, "cpu_time":cpu_time}
m[k] = v
p = m.items()
p.sort(key= lambda x: x[1]["cpu_time"],reverse=True)
return p[0:limit]
def getProcessDetails(self,pid):
x = psutil.Process(pid)
xcuse = """Information about this process is either
or you lack the proper permissions to view it"""
try:
process_details = {}
process_details['Status'] = str(x.status)
process_details['Username'] = x.username
process_details['Terminal'] = x.terminal
process_details['IO Count'] = x.get_io_counters()
process_details['CPU Times'] = x.get_cpu_times()
process_details['Threads'] = x.get_threads()
process_details['Memory'] = x.get_memory_info()
process_details['Connections'] = x.get_connections()
except: process_details = xcuse
# return x
return process_details