Skip to content

Commit 16e5bb2

Browse files
committed
fix: plugin (raid) - Show degradation only when stats are available
fixes nicolargo#2908
1 parent 69f4a5f commit 16e5bb2

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

glances/plugins/raid/__init__.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def msg_curse(self, args=None, max_width=None):
7979
if max_width:
8080
name_max_width = max_width - 12
8181
else:
82-
# No max_width defined, return an emptu curse message
82+
# No max_width defined, return an empty curse message
8383
logger.debug(f"No max_width defined for the {self.plugin_name} plugin, it will not be displayed.")
8484
return ret
8585

@@ -93,68 +93,76 @@ def msg_curse(self, args=None, max_width=None):
9393
# Data
9494
arrays = sorted(iterkeys(self.stats))
9595
for array in arrays:
96-
# New line
97-
ret.append(self.curse_new_line())
98-
# Display the current status
99-
if not isinstance(self.stats[array], dict):
96+
array_stats = self.stats[array]
97+
98+
if not isinstance(array_stats, dict):
10099
continue
100+
101+
# Display the current status
101102
status = self.raid_alert(
102-
self.stats[array]['status'],
103-
self.stats[array]['used'],
104-
self.stats[array]['available'],
105-
self.stats[array]['type'],
103+
array_stats['status'],
104+
array_stats['used'],
105+
array_stats['available'],
106+
array_stats['type'],
106107
)
108+
109+
# New line
110+
ret.append(self.curse_new_line())
107111
# Data: RAID type name | disk used | disk available
108-
array_type = self.stats[array]['type'].upper() if self.stats[array]['type'] is not None else 'UNKNOWN'
112+
array_type = array_stats['type'].upper() if array_stats['type'] is not None else 'UNKNOWN'
109113
# Build the full name = array type + array name
110114
full_name = f'{array_type} {array}'
111115
msg = '{:{width}}'.format(full_name, width=name_max_width)
112116
ret.append(self.curse_add_line(msg))
113-
if self.stats[array]['type'] == 'raid0' and self.stats[array]['status'] == 'active':
114-
msg = '{:>7}'.format(len(self.stats[array]['components']))
117+
if array_stats['type'] == 'raid0' and array_stats['status'] == 'active':
118+
msg = '{:>7}'.format(len(array_stats['components']))
115119
ret.append(self.curse_add_line(msg, status))
116120
msg = '{:>7}'.format('-')
117121
ret.append(self.curse_add_line(msg, status))
118-
elif self.stats[array]['status'] == 'active':
119-
msg = '{:>7}'.format(self.stats[array]['used'])
122+
elif array_stats['status'] == 'active':
123+
msg = '{:>7}'.format(array_stats['used'])
120124
ret.append(self.curse_add_line(msg, status))
121-
msg = '{:>7}'.format(self.stats[array]['available'])
125+
msg = '{:>7}'.format(array_stats['available'])
122126
ret.append(self.curse_add_line(msg, status))
123-
elif self.stats[array]['status'] == 'inactive':
127+
elif array_stats['status'] == 'inactive':
124128
ret.append(self.curse_new_line())
125-
msg = '└─ Status {}'.format(self.stats[array]['status'])
129+
msg = '└─ Status {}'.format(array_stats['status'])
126130
ret.append(self.curse_add_line(msg, status))
127-
components = sorted(iterkeys(self.stats[array]['components']))
131+
components = sorted(iterkeys(array_stats['components']))
128132
for i, component in enumerate(components):
129133
if i == len(components) - 1:
130134
tree_char = '└─'
131135
else:
132136
tree_char = '├─'
133137
ret.append(self.curse_new_line())
134-
msg = ' {} disk {}: '.format(tree_char, self.stats[array]['components'][component])
138+
msg = ' {} disk {}: '.format(tree_char, array_stats['components'][component])
135139
ret.append(self.curse_add_line(msg))
136140
msg = f'{component}'
137141
ret.append(self.curse_add_line(msg))
138-
if self.stats[array]['type'] != 'raid0' and (self.stats[array]['used'] < self.stats[array]['available']):
142+
143+
if array_stats['type'] != 'raid0' and (
144+
array_stats['used'] and array_stats['available'] and array_stats['used'] < array_stats['available']
145+
):
139146
# Display current array configuration
140147
ret.append(self.curse_new_line())
141148
msg = '└─ Degraded mode'
142149
ret.append(self.curse_add_line(msg, status))
143-
if len(self.stats[array]['config']) < 17:
150+
if len(array_stats['config']) < 17:
144151
ret.append(self.curse_new_line())
145-
msg = ' └─ {}'.format(self.stats[array]['config'].replace('_', 'A'))
152+
msg = ' └─ {}'.format(array_stats['config'].replace('_', 'A'))
146153
ret.append(self.curse_add_line(msg))
147154

148155
return ret
149156

150-
def raid_alert(self, status, used, available, type):
157+
@staticmethod
158+
def raid_alert(status, used, available, raid_type) -> str:
151159
"""RAID alert messages.
152160
153161
[available/used] means that ideally the array may have _available_
154162
devices however, _used_ devices are in use.
155163
Obviously when used >= available then things are good.
156164
"""
157-
if type == 'raid0':
165+
if raid_type == 'raid0':
158166
return 'OK'
159167
if status == 'inactive':
160168
return 'CRITICAL'

0 commit comments

Comments
 (0)