Skip to content

Commit

Permalink
ensure json.loads()
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtief committed Feb 27, 2024
1 parent 9ce6e90 commit 888a7b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions collectors/PropertiesCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ def collect(self):
metric_value = value_entry.get('values', [False])[0]

if statkey in self.nested_value_metric_keys:

add_labels, add_label_value_list, add_value = self.unlock_nested_values(statkey, metric_value)

if not add_labels:
metric_suffix = metrics[statkey]['metric_suffix']
self.add_metric_labels(metrics[statkey]['gauge'], [metric_suffix])
metrics[statkey]['gauge'].add_metric(labels=labels+[metric_suffix], value=0)
continue

self.add_metric_labels(metrics[statkey]['gauge'], add_labels)

for add_label_value in add_label_value_list:
Expand Down
12 changes: 10 additions & 2 deletions collectors/SDRSPropertiesCollector.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from collectors.PropertiesCollector import PropertiesCollector
import json
import logging

logger = logging.getLogger('vrops-exporter')

class SDRSPropertiesCollector(PropertiesCollector):

Expand Down Expand Up @@ -27,9 +30,14 @@ def unlock_nested_values(self, statkey, metric_value):

def config_sdrsconfig_vmStorageAntiAffinityRules(self, metric_value):

metric_value = json.loads(metric_value)
rules = metric_value.get("rules") or []
try:
metric_value = json.loads(metric_value)

except (TypeError, json.decoder.JSONDecodeError) as e:
logger.warning(f'metric_value is not a valid json: {e.args}, {metric_value}')
return [], [], 0

rules = metric_value.get("rules") or []
amount_rules = len(rules)

rule_labels = ['rule', 'rule_name', 'rule_type', 'valid', 'virtualmachine']
Expand Down

0 comments on commit 888a7b7

Please sign in to comment.