-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
71 lines (65 loc) · 2.17 KB
/
Copy pathmemory.py
File metadata and controls
71 lines (65 loc) · 2.17 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
62
63
64
65
66
67
68
69
70
71
from .base import BasePlugin
import os
from lib import convert
from lib.response import BaseResponse
class Memory(BasePlugin):
def win(self, handler, hostname):
"""
执行命令拿到结果
:return:
"""
ret = handler.cmd('getmac', hostname)
response = BaseResponse()
return response.dict
def linux(self, handler, hostname):
response = BaseResponse()
try:
if self.debug:
output = open(os.path.join(self.base_dir, 'files', 'memory.out'), 'r').read()
else:
shell_command = "sudo dmidecode -q -t 17 2>/dev/null"
output = handler.cmd(shell_command, hostname)
response.data = self.parse(output)
except Exception as e:
msg = traceback.format_exc()
response.status = False
response.error = msg
logger.error(msg)
return response.dict
def parse(self, content):
"""
解析shell命令返回结果
:param content: shell 命令结果
:return:解析后的结果
"""
ram_dict = {}
key_map = {
'Size': 'capacity',
'Locator': 'slot',
'Type': 'model',
'Speed': 'speed',
'Manufacturer': 'manufacturer',
'Serial Number': 'sn',
}
devices = content.split('Memory Device')
for item in devices:
item = item.strip()
if not item:
continue
if item.startswith('#'):
continue
segment = {}
lines = item.split('\n\t')
for line in lines:
if len(line.split(':')) > 1:
key, value = line.split(':')
else:
key = line.split(':')[0]
value = ""
if key in key_map:
if key == 'Size':
segment[key_map['Size']] = convert.convert_mb_to_gb(value, 0)
else:
segment[key_map[key.strip()]] = value.strip()
ram_dict[segment['slot']] = segment
return ram_dict