From f1faca41d1b9ff55f0faa88ab1fec858d9ebe360 Mon Sep 17 00:00:00 2001 From: Adam Rabjerg Date: Sat, 19 Jun 2021 13:40:53 +0200 Subject: [PATCH] initial commit --- .gitignore | 4 ++++ flask_webhook_server.py | 25 +++++++++++++++++++++++++ requirements.txt | 2 ++ run.sh | 17 +++++++++++++++++ secrets.env.sample | 5 +++++ setup.sh | 12 ++++++++++++ twilio_send.py | 24 ++++++++++++++++++++++++ 7 files changed, 89 insertions(+) create mode 100644 .gitignore create mode 100644 flask_webhook_server.py create mode 100644 requirements.txt create mode 100755 run.sh create mode 100644 secrets.env.sample create mode 100755 setup.sh create mode 100644 twilio_send.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3e8a76 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.git +.idea +venv +__pycache__ diff --git a/flask_webhook_server.py b/flask_webhook_server.py new file mode 100644 index 0000000..2dfa885 --- /dev/null +++ b/flask_webhook_server.py @@ -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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9c80e47 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +twilio +flask \ No newline at end of file diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..ecf3517 --- /dev/null +++ b/run.sh @@ -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 \ No newline at end of file diff --git a/secrets.env.sample b/secrets.env.sample new file mode 100644 index 0000000..de57f27 --- /dev/null +++ b/secrets.env.sample @@ -0,0 +1,5 @@ +[TWILIO] +account_sid = YOUR_ACCOUNT_SID +auth_token = YOUR_ACCOUNT_TOKEN +from_nr = YOUR_SENDER_NUMBER +to_nr = RECIVER_NUMBER \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..58eaceb --- /dev/null +++ b/setup.sh @@ -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." diff --git a/twilio_send.py b/twilio_send.py new file mode 100644 index 0000000..987018f --- /dev/null +++ b/twilio_send.py @@ -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)