return self.common_db.get_one("projects", {"_id": project_id})
def create_alarm(self, alarm: Alarm):
+ action_data = {"uuid": alarm.uuid, "action": alarm.action}
+ self.common_db.create("alarms_action", action_data)
return self.common_db.create("alarms", alarm.to_dict())
def delete_alarm(self, alarm_uuid: str):
+ self.common_db.del_one("alarms_action", {"uuid": alarm_uuid})
return self.common_db.del_one("alarms", {"uuid": alarm_uuid})
def get_alarms(self) -> List[Alarm]:
alarms.append(Alarm.from_dict(alarm_dict))
return alarms
+ def update_alarm_status(self, alarm_state: str, uuid):
+ modified_count = self.common_db.set_one("alarms", {"uuid": uuid}, {"alarm_status": alarm_state})
+ return modified_count
+
+ def get_alarm_by_uuid(self, uuid: str):
+ return self.common_db.get_one("alarms", {"uuid": uuid})
+
def get_user(self, username: str):
return self.common_db.get_one("users", {"username": username})
operation: str = None,
statistic: str = None,
metric: str = None,
+ action: str = None,
tags: dict = {},
+ alarm_status: str = "ok",
):
self.uuid = str(uuid.uuid4())
self.name = name
self.operation = operation
self.statistic = statistic
self.metric = metric
+ self.action = action
self.tags = tags
+ self.alarm_status = alarm_status
def to_dict(self) -> dict:
alarm = {
"metric": self.metric,
"tags": self.tags,
"operation": self.operation,
+ "alarm_status": self.alarm_status,
}
return alarm
alarm.metric = data.get("metric")
alarm.tags = data.get("tags")
alarm.operation = data.get("operation")
+ alarm.alarm_status = data.get("alarm_status")
return alarm
self.loop.run_until_complete(
self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message)
)
+ evaluator_service = EvaluatorService(self.conf)
+ evaluator_service.update_alarm_status(status.value, alarm.uuid)
+ return
def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
log.debug("_build_alarm_response")
ALARM = "alarm"
OK = "ok"
INSUFFICIENT = "insufficient-data"
+ DISABLED = "disabled"
class EvaluatorService:
def _evaluate_metric(self, alarm: Alarm):
log.debug("_evaluate_metric")
metric_value = self._get_metric_value(alarm.metric, alarm.tags)
- if metric_value is None:
- log.warning("No metric result for alarm %s", alarm.uuid)
- self.queue.put((alarm, AlarmStatus.INSUFFICIENT))
- else:
- if alarm.operation.upper() == "GT":
- if metric_value > alarm.threshold:
- self.queue.put((alarm, AlarmStatus.ALARM))
- else:
- self.queue.put((alarm, AlarmStatus.OK))
- elif alarm.operation.upper() == "LT":
- if metric_value < alarm.threshold:
- self.queue.put((alarm, AlarmStatus.ALARM))
- else:
- self.queue.put((alarm, AlarmStatus.OK))
+ if alarm.alarm_status.upper() != AlarmStatus.DISABLED.value.upper():
+ if metric_value is None:
+ log.warning("No metric result for alarm %s", alarm.uuid)
+ self.queue.put((alarm, AlarmStatus.INSUFFICIENT))
+ else:
+ if alarm.operation.upper() == "GT":
+ if metric_value > alarm.threshold:
+ self.queue.put((alarm, AlarmStatus.ALARM))
+ else:
+ self.queue.put((alarm, AlarmStatus.OK))
+ elif alarm.operation.upper() == "LT":
+ if metric_value < alarm.threshold:
+ self.queue.put((alarm, AlarmStatus.ALARM))
+ else:
+ self.queue.put((alarm, AlarmStatus.OK))
+
+ def update_alarm_status(self, alarm_state, uuid):
+ alarm_data = self.common_db.get_alarm_by_uuid(uuid)
+ if alarm_data.get("alarm_status").upper() != AlarmStatus.DISABLED.value.upper():
+ self.common_db.update_alarm_status(alarm_state, uuid)
+ return
def evaluate_alarms(self) -> List[Tuple[Alarm, AlarmStatus]]:
log.debug("evaluate_alarms")
alarm_details["severity"].lower(),
alarm_details["statistic"].lower(),
alarm_details["metric_name"],
+ alarm_details["action"],
alarm_details["tags"],
)
response = response_builder.generate_response(
severity: str,
statistic: str,
metric_name: str,
+ action: str,
tags: dict,
) -> Alarm:
log.debug("create_alarm")
alarm = Alarm(
- name, severity, threshold, operation, statistic, metric_name, tags
+ name, severity, threshold, operation, statistic, metric_name, action, tags
)
self.common_db.create_alarm(alarm)
log.info("Alarm %s created", alarm.name)
@mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
@mock.patch.object(dbmongo.DbMongo, "create")
def test_create_alarm(self, create):
- alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", {})
+ alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", "scale_out", {}, "ok")
alarm.uuid = "1"
common_db_client = CommonDbClient(self.config)
common_db_client.create_alarm(alarm)
"name": "name",
"operation": "operation",
"uuid": "1",
+ "alarm_status": "ok",
},
)