-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk.py
More file actions
68 lines (63 loc) · 2.27 KB
/
Copy pathdisk.py
File metadata and controls
68 lines (63 loc) · 2.27 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
import os
import re
from .base import BasePlugin
from lib.response import BaseRespone
class Disk(BasePlugin):
def win(self, handler, hostname):
ret = handler.cmd("hostname", hostname)
response = BaseResponse()
return response.dict
def linux(self, handler, hostname):
response = BaseRespone()
try:
if self.debug:
output = open(os.path.join(self.base_dir, 'files', 'disk.out'), 'r').read()
print(output)
else:
shell_command = "sudo MegaCli -PDList -aALL"
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: 解析后的结果
"""
response = {}
result = []
for row_line in content.split("\n\n\n\n"):
result.append(row_line)
for item in result:
temp_dict = {}
for row in item.split("\n"):
if not row.strip():
continue
if len(row.split(":")) != 2:
continue
key, value = row.split(":")
name = self.mega_patter_match(key)
if name:
if key == 'Raw Size':
raw_size = re.search('(\d+\.\d+)', value.strip())
if raw_size:
temp_dict[name] = raw_size.group()
else:
raw_size = '0'
else:
temp_dict[name] = value.strip()
if temp_dict:
response[temp_dict['slot']] = temp_dict
return response
@staticmethod
def mega_patter_match(needle):
grep_pattern = {'Slot': 'slot', 'Raw Size': 'capacity', 'Inquiry': 'model', 'PD Type': 'pd_type'}
for key, value in grep_pattern.items():
if needle.startswith(key):
return value
return False