Azure monitor webhooks ()

This commit is contained in:
Anton 2018-07-11 22:52:30 +02:00 committed by Nick Satterly
parent 623bfdd4ec
commit 6e778bccf2
3 changed files with 182 additions and 0 deletions

View file

@ -0,0 +1,76 @@
Azure Monitor Webhook
==============
Receive [Azure Monitor](https://azure.microsoft.com/services/monitor/) notifications via webhook callbacks.
For help, join [![Gitter chat](https://badges.gitter.im/alerta/chat.png)](https://gitter.im/alerta/chat)
Installation
------------
Clone the GitHub repo and run:
$ python setup.py install
Or, to install remotely from GitHub run:
$ pip install git+https://github.com/alerta/alerta-contrib.git#subdirectory=webhooks/azuremonitor
Note: If Alerta is installed in a python virtual environment then plugins
need to be installed into the same environment for Alerta to dynamically
discover them.
Configuration
-------------
The custom webhook will be auto-detected and added to the list of available API endpoints.
Add the Alerta API webhook URL in the Azure portal.
https://docs.microsoft.com/sv-se/azure/monitoring-and-diagnostics/insights-webhooks-alerts
Example Output
--------------
```
{
"status": "Activated",
"context": {
"timestamp": "2015-08-14T22:26:41.9975398Z",
"id": "/subscriptions/s1/resourceGroups/useast/providers/microsoft.insights/alertrules/ruleName1",
"name": "ruleName1",
"description": "some description",
"conditionType": "Metric",
"condition": {
"metricName": "Requests",
"metricUnit": "Count",
"metricValue": "10",
"threshold": "10",
"windowSize": "15",
"timeAggregation": "Average",
"operator": "GreaterThanOrEqual"
},
"subscriptionId": "s1",
"resourceGroupName": "useast",
"resourceName": "mysite1",
"resourceType": "microsoft.foo/sites",
"resourceId": "/subscriptions/s1/resourceGroups/useast/providers/microsoft.foo/sites/mysite1",
"resourceRegion": "centralus",
"portalLink": "https://portal.azure.com/#resource/subscriptions/s1/resourceGroups/useast/providers/microsoft.foo/sites/mysite1"
},
"properties": {
"key1": "value1",
"key2": "value2"
}
}
```
References
----------
* Azure Monitor: https://docs.microsoft.com/azure/monitoring-and-diagnostics/monitor-alerts-unified-usage
License
-------
Copyright (c) 2018 Anton Delitsch. Available under the MIT License.

View file

@ -0,0 +1,82 @@
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)
)

View file

@ -0,0 +1,24 @@
from setuptools import setup, find_packages
version = '5.0.0'
setup(
name="alerta-azure-monitor",
version=version,
description='Alerta webhook for Azure Monitor',
url='https://github.com/alerta/alerta-contrib',
license='MIT',
author='Anton Delitsch',
author_email='anton@trugen.net',
packages=find_packages(),
py_modules=['alerta_azuremonitor'],
install_requires=[
],
include_package_data=True,
zip_safe=True,
entry_points={
'alerta.webhooks': [
'azuremonitor = alerta_azuremonitor:AzureMonitorWebhook'
]
}
)