Skip to content

Commit 381d92a

Browse files
committed
Add a function for converting to camelcase
1 parent 34c78a9 commit 381d92a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

etc/kayobe/ansible/scripts/smartmon.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ def run_command(command, parse_json=False):
7878
return json.loads(result.stdout)
7979
return result.stdout.strip()
8080

81+
def camel_to_snake(name):
82+
"""
83+
Convert a CamelCase string to snake_case.
84+
85+
Reference: https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
86+
"""
87+
return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()
88+
8189
def parse_device_info(device):
8290
"""
8391
Produce Prometheus lines describing the device's identity and SMART status:
@@ -145,8 +153,7 @@ def parse_if_attributes(device):
145153
if callable(val):
146154
continue # skip methods
147155

148-
# Convert CamelCase or PascalCase -> snake_case, e.g. dataUnitsRead -> data_units_read
149-
snake_name = re.sub(r'(?<!^)(?=[A-Z])', '_', attr_name).lower()
156+
snake_name = camel_to_snake(attr_name)
150157

151158
if snake_name in SMARTMON_ATTRS and isinstance(val, (int, float)):
152159
metrics.append(f"{snake_name}{{{labels}}} {val}")

0 commit comments

Comments
 (0)