def update_attributes(id): attrs_started = attrs_timer.start_timer() customer = g.get('customer', None) try: alert = db.get_alert(id=id, customer=customer) except Exception as e: attrs_timer.stop_timer(attrs_started) return jsonify(status="error", message=str(e)), 500 if not alert: attrs_timer.stop_timer(attrs_started) return jsonify(status="error", message="not found", total=0, alert=None), 404 attributes = request.json.get('attributes', None) if not attributes: attrs_timer.stop_timer(attrs_started) return jsonify(status="error", message="must supply 'attributes' as parameter"), 400 try: alert = db.update_attributes(id, attributes) except Exception as e: attrs_timer.stop_timer(attrs_started) return jsonify(status="error", message=str(e)), 500 if alert: attrs_timer.stop_timer(attrs_started) return jsonify(status="ok") else: attrs_timer.stop_timer(attrs_started) return jsonify(status="error", message="not found"), 404
def post_receive(self, alert, **kwargs): if alert.repeat: return flapnotify = False if alert.is_flapping(window=FLAPPING_WINDOW, count=FLAPPING_COUNT): if alert.severity != DEFAULT_NORMAL_SEVERITY: if not alert.attributes.get('flapping', False): # notify if it has transitioned to flapping LOG.debug("ALERT HAS STARTED TO FLAP") flapnotify = True alert.attributes['flapping'] = True else: alert.attributes['flapping'] = False flapnotify = True # alert updates in post_receive need # to be saved back into the databbase db.update_attributes(alert.id, None, alert.attributes) if alert.attributes.get('flapping', False) and not flapnotify: LOG.info("SUPRESSING notification due to flapping") return try: payload = self._slack_prepare_payload(alert) LOG.debug('Slack payload: %s', payload) except Exception as e: LOG.error('Exception formatting payload: %s\n%s' % (e, traceback.format_exc())) return try: r = requests.post(SLACK_WEBHOOK_URL, data=json.dumps(payload), headers=SLACK_HEADERS, timeout=2) except Exception as e: raise RuntimeError("Slack connection error: %s", e) LOG.debug('Slack response: %s %s' % (r.status_code, r.text))
def update_attributes(self, attributes: Dict[str, Any]) -> bool: return db.update_attributes(self.id, self.attributes, attributes)
def update_attributes(self, attributes): return db.update_attributes(self.id, self.attributes, attributes)
def status_change(self, alert, status, text): if alert.event_type != 'prometheusAlert': return if alert.status == status: return if status == 'ack': LOG.debug('Alertmanager: Add silence for alertname=%s instance=%s', alert.event, alert.resource) data = { "matchers": [ { "name": "alertname", "value": alert.event }, { "name": "instance", "value": alert.resource } ], "startsAt": datetime.datetime.utcnow().replace(microsecond=0).isoformat() + ".000Z", "endsAt": (datetime.datetime.utcnow() + datetime.timedelta(days=ALERTMANAGER_SILENCE_DAYS)) .replace(microsecond=0).isoformat() + ".000Z", "createdBy": "alerta", "comment": text if text != '' else "silenced by alerta" } base_url = ALERTMANAGER_API_URL or alert.attributes.get('externalUrl', DEFAULT_ALERTMANAGER_API_URL) url = base_url + '/api/v1/silences' try: r = requests.post(url, json=data, timeout=2) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: %s - %s', r.status_code, r.text) # example r={"status":"success","data":{"silenceId":8}} try: silenceId = r.json()['data']['silenceId'] db.update_attributes(alert.id, {'silenceId': silenceId}) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: Added silenceId %s to attributes', silenceId) elif status == 'open': LOG.debug('Alertmanager: Remove silence for alertname=%s instance=%s', alert.event, alert.resource) silenceId = alert.attributes.get('silenceId', None) if silenceId: base_url = ALERTMANAGER_API_URL or alert.attributes.get('externalUrl', DEFAULT_ALERTMANAGER_API_URL) url = base_url + '/api/v1/silence/%s' % silenceId try: r = requests.delete(url, timeout=2) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: %s - %s', r.status_code, r.text) try: db.update_attributes(alert.id, {'silenceId': None}) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: Removed silenceId %s from attributes', silenceId)
def status_change(self, alert, status, text): if alert.event_type != 'prometheusAlert': return if alert.status == status: return if status == 'ack': LOG.debug('Alertmanager: Add silence for alertname=%s instance=%s', alert.event, alert.resource) data = { "matchers": [{ "name": "alertname", "value": alert.event }, { "name": "instance", "value": alert.resource }], "startsAt": datetime.datetime.utcnow().replace(microsecond=0).isoformat() + ".000Z", "endsAt": (datetime.datetime.utcnow() + datetime.timedelta(days=ALERTMANAGER_SILENCE_DAYS)).replace( microsecond=0).isoformat() + ".000Z", "createdBy": "alerta", "comment": text if text != '' else "silenced by alerta" } url = ALERTMANAGER_API_URL + '/api/v1/silences' try: r = requests.post(url, json=data, timeout=2) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: %s - %s', r.status_code, r.text) # example r={"status":"success","data":{"silenceId":8}} try: silenceId = r.json()['data']['silenceId'] db.update_attributes(alert.id, {'silenceId': silenceId}) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: Added silenceId %s to attributes', silenceId) elif status == 'open': LOG.debug( 'Alertmanager: Remove silence for alertname=%s instance=%s', alert.event, alert.resource) silenceId = alert.attributes.get('silenceId', None) if silenceId: url = ALERTMANAGER_API_URL + '/api/v1/silence/%s' % silenceId try: r = requests.delete(url, timeout=2) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: %s - %s', r.status_code, r.text) try: db.update_attributes(alert.id, {'silenceId': None}) except Exception as e: raise RuntimeError("Alertmanager: ERROR - %s", e) LOG.debug('Alertmanager: Removed silenceId %s from attributes', silenceId)