initial commit
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
.git
|
||||||
|
.idea
|
||||||
|
venv
|
||||||
|
__pycache__
|
||||||
25
flask_webhook_server.py
Normal file
25
flask_webhook_server.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#!/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)
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
twilio
|
||||||
|
flask
|
||||||
17
run.sh
Executable file
17
run.sh
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
source ./venv/bin/activate
|
||||||
|
export FLASK_APP=flask_webhook_server.py
|
||||||
|
|
||||||
|
if [[ -z "$APP_DEPLOYMENT_TYPE" ]]; then
|
||||||
|
export APP_DEPLOYMENT_TYPE=private
|
||||||
|
echo -e "APP_DEPLOYMENT_TYPE not set \nIn order to set it to public: \"export APP_DEPLOYMENT_TYPE=public\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$APP_DEPLOYMENT_TYPE" == "public" ]]; then
|
||||||
|
echo -e "Running app in \"Public mode\", app is exposed to the network!"
|
||||||
|
/usr/bin/env python -m flask run --host=0.0.0.0
|
||||||
|
else
|
||||||
|
echo -e "Running app in \"Private mode\", app is NOT exposed to the network and only available to localhost."
|
||||||
|
/usr/bin/env python -m flask run
|
||||||
|
fi
|
||||||
5
secrets.env.sample
Normal file
5
secrets.env.sample
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
[TWILIO]
|
||||||
|
account_sid = YOUR_ACCOUNT_SID
|
||||||
|
auth_token = YOUR_ACCOUNT_TOKEN
|
||||||
|
from_nr = YOUR_SENDER_NUMBER
|
||||||
|
to_nr = RECIVER_NUMBER
|
||||||
12
setup.sh
Executable file
12
setup.sh
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "Creating virtual environment."
|
||||||
|
python3 -m venv ./venv
|
||||||
|
|
||||||
|
echo "Activating virtual environment."
|
||||||
|
source ./venv/bin/activate
|
||||||
|
|
||||||
|
echo "Installing packages to virtual environment."
|
||||||
|
pip3 install -r ./requirements.txt
|
||||||
|
|
||||||
|
echo "We are done here."
|
||||||
24
twilio_send.py
Normal file
24
twilio_send.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
|
from twilio.rest import Client
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read("secrets.env")
|
||||||
|
|
||||||
|
account_sid = config["TWILIO"]["account_sid"]
|
||||||
|
auth_token = config["TWILIO"]["auth_token"]
|
||||||
|
reciver_number = config["TWILIO"]["to_nr"]
|
||||||
|
sender_number = config["TWILIO"]["from_nr"]
|
||||||
|
client = Client(account_sid, auth_token)
|
||||||
|
|
||||||
|
|
||||||
|
def send_sms(payload):
|
||||||
|
message = client.messages \
|
||||||
|
.create(
|
||||||
|
body=payload,
|
||||||
|
from_=sender_number,
|
||||||
|
to=reciver_number
|
||||||
|
)
|
||||||
|
|
||||||
|
print(message.status)
|
||||||
Reference in New Issue
Block a user