26 lines
1.2 KiB
Python
26 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
from flask import Flask, request, Response
|
|
from twilio_send import send_sms
|
|
import datetime
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route('/grafana_webhook', methods=['POST'])
|
|
def respond_2_grafana():
|
|
print(request.json)
|
|
allowed_Url = 'https://grafana.home:443/' # Basic source authentication, fill with the URL of your Grafana server
|
|
allowed_remote_addr = ["127.0.0.1"] # Only webhooks form these addresses are allowed to send SMS
|
|
payload = f"Grafana alert: {datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')}\n\n" \
|
|
f"Alert type: \"{request.json['ruleName']}\"\n\n" \
|
|
f"Alert state: \"{request.json['state']}\"\n\n" \
|
|
f"Message: \"{request.json['message']}\"\n\n" \
|
|
f"Metric. Value.\n"
|
|
for metric in request.json['evalMatches']:
|
|
payload += f"{metric['metric']} {metric['value']}\n"
|
|
if request.json["ruleUrl"].startswith(allowed_Url) and request.remote_addr in allowed_remote_addr:
|
|
send_sms(payload)
|
|
else:
|
|
print(f"Bad request source. {request.remote_addr} / {request.json['ruleUrl']}")
|
|
return Response(status=418)
|
|
return Response(status=200)
|