Skip to content

Commit 9a59409

Browse files
committed
merged enhancement/storage_regex
2 parents 845ee00 + 5b753e4 commit 9a59409

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

nodescraper/plugins/inband/storage/analyzer_args.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
###############################################################################
2626
from typing import Optional
2727

28-
from pydantic import BaseModel
28+
from pydantic import BaseModel, Field
2929

3030

3131
class StorageAnalyzerArgs(BaseModel):
3232
min_required_free_space_abs: Optional[str] = None
3333
min_required_free_space_prct: Optional[int] = None
34-
ignore_devices: Optional[list] = []
35-
check_devices: Optional[list] = []
34+
ignore_devices: Optional[list[str]] = Field(default_factory=list)
35+
check_devices: Optional[list[str]] = Field(default_factory=list)
36+
regex_match: bool = False

nodescraper/plugins/inband/storage/storage_analyzer.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26+
import re
2627
from typing import Optional
2728

2829
from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus
@@ -39,6 +40,36 @@ class StorageAnalyzer(DataAnalyzer[StorageDataModel, StorageAnalyzerArgs]):
3940

4041
DATA_MODEL = StorageDataModel
4142

43+
def _matches_device_filter(
44+
self, device_name: str, exp_devices: list[str], regex_match: bool
45+
) -> bool:
46+
"""Check if the device name matches any of the expected devices""
47+
48+
Args:
49+
device_name (str): device name to check
50+
exp_devices (list[str]): list of expected devices to match against
51+
regex_match (bool): if True, use regex matching; otherwise, use exact match
52+
53+
Returns:
54+
bool: True if the device name matches any of the expected devices, False otherwise
55+
"""
56+
for exp_device in exp_devices:
57+
if regex_match:
58+
try:
59+
device_regex = re.compile(exp_device)
60+
except re.error:
61+
self._log_event(
62+
category=EventCategory.STORAGE,
63+
description=f"Invalid regex pattern: {exp_device}",
64+
priority=EventPriority.ERROR,
65+
)
66+
continue
67+
if device_regex.match(device_name):
68+
return True
69+
elif device_name == exp_device:
70+
return True
71+
return False
72+
4273
def analyze_data(
4374
self, data: StorageDataModel, args: Optional[StorageAnalyzerArgs] = None
4475
) -> TaskResult:
@@ -71,11 +102,15 @@ def analyze_data(
71102
passing_devices = []
72103
failing_devices = []
73104
for device_name, device_data in data.storage_data.items():
74-
if args.check_devices:
75-
if device_name not in args.check_devices:
76-
continue
77-
elif args.ignore_devices and device_name in args.ignore_devices:
105+
if args.check_devices and not self._matches_device_filter(
106+
device_name, args.check_devices, args.regex_match
107+
):
78108
continue
109+
elif args.ignore_devices and self._matches_device_filter(
110+
device_name, args.ignore_devices, args.regex_match
111+
):
112+
continue
113+
79114
condition = False
80115
if args.min_required_free_space_abs:
81116
min_free_abs = convert_to_bytes(args.min_required_free_space_abs)

0 commit comments

Comments
 (0)