mirror of
https://github.com/alerta/alerta-contrib.git
synced 2025-03-16 21:43:30 +00:00
82 lines
3.6 KiB
Python
82 lines
3.6 KiB
Python
|
|
from alerta.models.alert import Alert
|
|
from alerta.webhooks import WebhookBase
|
|
import json
|
|
|
|
|
|
class AzureMonitorWebhook(WebhookBase):
|
|
|
|
def incoming(self, query_string, payload):
|
|
|
|
# Environment is not avaible in the payload, use request param.
|
|
try:
|
|
environment = query_string['environment']
|
|
except:
|
|
environment = 'Production'
|
|
|
|
# Azure have two formats for their weebhooks (the first one is new format and the decond one is old format.
|
|
try:
|
|
if payload['data']['status'] == 'Activated':
|
|
severity = 'critical'
|
|
elif payload['data']['status'] == 'Resolved':
|
|
severity = 'ok'
|
|
else:
|
|
severity = 'unknown'
|
|
|
|
resource=payload['data']['context']['resourceName']
|
|
create_time=payload['data']['context']['timestamp']
|
|
event=payload['data']['context']['name']
|
|
service=payload['data']['context']['resourceType']
|
|
group=payload['data']['context']['resourceGroupName']
|
|
tags=['{}={}'.format(k, v) for k, v in payload['data']['properties'].items()]
|
|
if payload['schemaId'] == 'AzureMonitorMetricAlert':
|
|
type = 'MetricAlert'
|
|
text = '{}: {} {} ({} {})'.format(severity.upper(), payload['data']['context']['condition']['allOf'][0]['metricValue'], payload['data']['context']['condition']['allOf'][0]['metricName'], payload['data']['context']['condition']['allOf'][0]['operator'], payload['data']['context']['condition']['allOf'][0]['threshold'])
|
|
value = '{} {}'.format(payload['data']['context']['condition']['allOf'][0]['metricValue'], payload['data']['context']['condition']['allOf'][0]['metricName'])
|
|
else:
|
|
text = '{}'.format(severity.upper())
|
|
value = ''
|
|
type = 'EventAlert'
|
|
|
|
|
|
|
|
except:
|
|
if payload['status'] == 'Activated':
|
|
severity = 'critical'
|
|
elif payload['status'] == 'Resolved':
|
|
severity = 'ok'
|
|
else:
|
|
severity = 'unknown'
|
|
|
|
if payload['context']['conditionType'] == 'Metric':
|
|
text = '{}: {} {} ({} {})'.format(severity.upper(), payload['context']['condition']['metricValue'], payload['context']['condition']['metricName'], payload['context']['condition']['operator'], payload['context']['condition']['threshold'])
|
|
value = '{} {}'.format(payload['context']['condition']['metricValue'], payload['context']['condition']['metricName'])
|
|
else:
|
|
text = '{}'.format(severity.upper())
|
|
value = ''
|
|
|
|
resource=payload['context']['resourceName']
|
|
create_time=payload['context']['timestamp']
|
|
type='{}Alert'.format(payload['context']['conditionType'])
|
|
event=payload['context']['name']
|
|
service=[payload['context']['resourceType']]
|
|
group=payload['context']['resourceGroupName']
|
|
tags=['{}={}'.format(k, v) for k, v in payload['properties'].items()]
|
|
|
|
|
|
return Alert(
|
|
resource=resource,
|
|
create_time=create_time,
|
|
type=type,
|
|
event=event,
|
|
environment=environment,
|
|
severity=severity,
|
|
service=service,
|
|
group=group,
|
|
value=value,
|
|
text=text,
|
|
tags=tags,
|
|
attributes={},
|
|
origin='Azure Monitor',
|
|
raw_data=json.dumps(payload, indent=4)
|
|
)
|