As shown in the above image, webhook can be crated in the Alerting→Contact Points section
The URL of API server and other required settings are required to be mentioned during webhook creation
Provision for basic authentication, authorization header, custom message title and message are also present
Sample API server
An API server can be developed to process alert information sent from Grafana webhook
Grafana webhook will call the API server to deliver alert data
The API server can use the alert information from JSON body of HTTP request and deliver the alert to a custom alert destination like SMS gateway, custom database etc.
A sample API server developed in python Flask for delivering alert data from webhooks to alert destinations like SMS gateway, database, message queue etc.
from flask import Flask, request
import json
# create a flask server
app = Flask(__name__)
@app.route('/alert', methods=["POST"])defalert():
alertData = request.json
"""
# dump alert data to a file for debugging purposes
out_file = open("test.json", "w")
json.dump(alertData,out_file,indent=4)
out_file.close()
"""# use alert data to deliver notification
alertMsg = alertData["message"]# send alert data to desired destination like an sms gateway, database etc.print(alertMsg)return'ok'
@app.route('/')defindex():return'Hello, World!'# __name__ will be __main__ only if this file is the entry pointif __name__ =='__main__':# run the server on this ip and port 50100
app.run(host='0.0.0.0', port=50100, debug=True)
Comments
Post a Comment