from threshold_upper import Threshold_Upper from threshold_lower import Threshold_Lower from threshold import Threshold class Thresholds() : WARNING = True CRITICAL = False UPPER = True LOWER = False def __init__(self, alert_config) : self.alert_config = alert_config self.thresholds = {} for level in [ Thresholds.WARNING, Thresholds.CRITICAL ] : self.thresholds[level] = {} for end in [ Thresholds.UPPER, Thresholds.LOWER ] : constructor = Threshold_Upper if end == Thresholds.LOWER : constructor = Threshold_Lower self.thresholds[level][end] = self.create_threshold(end, level, constructor) def create_threshold(self, isUpper, isWarning, constructor) : value, has = self.alert_config.get_threshold(isUpper, isWarning) if not has : constructor = Threshold return constructor(value) def warning_breached(self) : return self.level_breached(Thresholds.WARNING) def critical_breached(self) : return self.level_breached(Thresholds.CRITICAL) def upper_breached(self) : return self.end_breached(Thresholds.UPPER) def lower_breached(self) : return self.end_breached(Thresholds.LOWER) def level_breached(self, level) : return self.get_breached(level=level) def end_breached(self, end) : return self.get_breached(end=end) def can_breach(self) : can_breach = [t for t in self.thresholds.get_thresholds_matching() if not type(t) is Threshold] return len(can_breach) > 0 def get_breached(self, level=None, end=None) : for threshold in self.get_thresholds_matching(level=level, end=end) : if threshold.get_breached() : return True return False def set_breached(self, min_value, max_value) : for threshold in self.get_thresholds_matching(end=Thresholds.LOWER) : threshold.set_breached(min_value) for threshold in self.get_thresholds_matching(end=Thresholds.UPPER) : threshold.set_breached(max_value) def get_thresholds_matching(self, level=None, end=None) : for l in self.thresholds : if level is None or l == level : for e in self.thresholds[l] : if end is None or e == end : yield self.thresholds[l][e]