def create_trigger(self, properties): """Create trigger.""" severity_key = properties.get('severity', 'info') rule = properties['rule'] trigger_klass = TRIGGER_CLASS.get(properties['item'].itemtype.name) severity = self.db_session.query(TriggerSeverity)\ .filter(TriggerSeverity.key == severity_key).first() if not severity: raise ValueError('Severity "' + severity + '" is invalid') if not trigger_klass.validate_rule(rule): raise ValueError('Rule "' + rule + '" can\'t be validated') trigger = trigger_klass(name=properties['name'], rule=properties['rule'], description=properties.get('description', ""), item_id=properties['item'].id, severity_id=severity.id) self.db_session.add(trigger) return trigger
def create_trigger(self, properties): """Create trigger.""" severity_key = properties.get('severity', 'info') rule = properties['rule'] trigger_klass = TRIGGER_CLASS.get(properties['item'].itemtype.name) severity = self.db_session.query(TriggerSeverity)\ .filter(TriggerSeverity.key == severity_key).first() if not severity: raise ValueError('Severity "' + severity + '" is invalid') if not trigger_klass.validate_rule(rule): raise ValueError('Rule "' + rule + '" can\'t be validated') trigger = trigger_klass( name=properties['name'], rule=properties['rule'], description=properties.get('description', ""), item_id=properties['item'].id, severity_id=severity.id) self.db_session.add(trigger) return trigger
def get_triggers(self, item): """Return all triggers on an item.""" trigger_klass = TRIGGER_CLASS.get(item.itemtype.name) triggers = self.db_session.query(trigger_klass)\ .options(joinedload(trigger_klass.item))\ .filter(trigger_klass.item_id == item.id) return triggers
def validate_trigger_rule(self, item, rule): """Return True if Trigger-rule is valid.""" trigger_klass = TRIGGER_CLASS.get(item.itemtype.name) ret = trigger_klass.validate_rule(rule) if ret is None: return False return True
def delete_trigger_by_id(self, item, trigger_id): """Delete Trigger for Item.""" trigger_klass = TRIGGER_CLASS.get(item.itemtype.name) trigger = self.db_session.query(trigger_klass)\ .filter(trigger_klass.id == trigger_id)\ .filter(trigger_klass.item_id == item.id)\ .first() if trigger: self.db_session.query(trigger_klass)\ .filter(trigger_klass.id == trigger_id)\ .filter(trigger_klass.item_id == item.id)\ .delete() else: raise ValueError("trigger_id not valid for item") return
def get_host_severity(self, host): """Return highest severity of active triggers for this host.""" float_trigger_klass = TRIGGER_CLASS.get('float') float_alert_klass = ALERT_CLASS.get('float') severity = self.db_session.query(TriggerSeverity)\ .filter(TriggerSeverity.id.in_( self.db_session.query(float_trigger_klass.severity_id)\ .filter(float_trigger_klass.item_id.in_( self.db_session.query(Item.id)\ .filter(Item.host_id == host.id) ))\ .filter(float_trigger_klass.id.in_( self.db_session.query(float_alert_klass.trigger_id)\ .filter(float_alert_klass.end_time.is_(None)) )) ))\ .order_by(TriggerSeverity.level.desc())\ .first() return severity
def get_host_severity(self, host): """Return highest severity of active triggers for this host. This should be optimized... running x queries for each individual host that needs it is very slow. - Want to use UNION, but I couldn't get it working with SQLAlchemy. """ existing_query = None max_severity_level = -1 max_severity = None for key in TRIGGER_CLASS: trigger_klass = TRIGGER_CLASS.get(key) alert_klass = ALERT_CLASS.get(key) severity = self.db_session.query(TriggerSeverity)\ .filter(TriggerSeverity.id.in_( self.db_session.query(trigger_klass.severity_id)\ .filter(trigger_klass.item_id.in_( self.db_session.query(Item.id) .filter(Item.host_id == host.id) ))\ .filter(trigger_klass.id.in_( self.db_session.query(alert_klass.trigger_id) .filter(alert_klass.end_time.is_(None)) )) ))\ .order_by(TriggerSeverity.level.desc())\ .first() if severity is not None: if severity.level > max_severity_level: max_severity_level = severity.level max_severity = severity return max_severity