-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystemstatusutils.py
More file actions
executable file
·61 lines (52 loc) · 2.06 KB
/
systemstatusutils.py
File metadata and controls
executable file
·61 lines (52 loc) · 2.06 KB
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
#!/usr/bin/env python
#encoding=utf-8
import commonutils
import const
import os
import psutil
import types
class systemstatusutils():
"""
AdminUtils System Status class,主要用于获取显示系统状况,包括CPU负载,HardDisk,网络I/O,内存等.
"""
SYSTEM_PLATFORM = ""
common_utils = None
const_attributes = None
def __init__(self, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
self.common_utils = commonutils.commonutils()
self.const_attributes = const.Const()
self.SYSTEM_PLATFORM = self.common_utils.get_os_type()
def system_memory_status(self):
memoryStatus = psutil.virtual_memory()
'''For Windwos platform'''
if self.SYSTEM_PLATFORM == self.const_attributes.SYSTEM_PLATFORM_WINDOWS:
result = {"total":memoryStatus.total/1024**2,
"available":memoryStatus.available/1024**2,
"used":memoryStatus.used/1024**2,
"free":memoryStatus.free/1024**2
}
'''For Linux platform'''
if self.SYSTEM_PLATFORM == self.const_attributes.SYSTEM_PLATFORM_LINUX:
result = {"total":memoryStatus.total/1024**2,
"available":memoryStatus.available/1024**2,
"used":memoryStatus.used/1024**2,
"free":memoryStatus.free/1024**2,
"buffers":memoryStatus.buffers/1024**2,
"cached":memoryStatus.cached/1024**2}
return result
def process_memory_status(self, pid=None):
memoryStatus = psutil.virtual_memory()
if pid == None:
p = psutil.Process()
else:
p = psutil.Process(pid)
p_dict = p.as_dict()
return p_dict["memory_info"].rss/1024**2
if __name__ == '__main__':
sys_status = systemstatusutils()
#print sys_status.SYSTEM_PLATFORM
#print "System total memory is: %s M" % (sys_status.system_memory_status()["total"])
print "The special Process cost memory %s M." % (sys_status.process_memory_status(26152))