Skip to content

Commit 08c4221

Browse files
author
Carlos Augusto Malucelli
authored
Merge pull request #4 from Sajadorouji/master
All tested, is ok. Enabled authentication and Output.log
2 parents 41dd2e8 + 1d302e8 commit 08c4221

5 files changed

Lines changed: 19 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flaskAlert.log

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Alertmanager webhook for Telegram using Flask
1+
# Alertmanager webhook for Telegram using Flask
22

33
## INSTALL
44

@@ -8,7 +8,7 @@ Change on flaskAlert.py
88
=======================
99
* botToken
1010
* chatID (without -)
11-
11+
1212
Alertmanager configuration example
1313
==================================
1414

@@ -17,7 +17,7 @@ Alertmanager configuration example
1717
webhook_configs:
1818
- url: http://ipFlaskAlert:9119/alert
1919
send_resolved: true
20-
20+
2121
One way to get the chat ID
2222
==========================
2323
1) Add bot on channel
@@ -30,8 +30,10 @@ Running
3030

3131
Running on docker
3232
=================
33-
* docker container run -d -e bottoken="telegramBotToken" -e chatid="telegramChatID" -p 9119:9119 nopp/alertmanager-webhook-telegram:latest
33+
* docker run -d --name telegram-bot -e "bottoken=telegramBotToken" -e "chatid=telegramChatID" -e "username=<username>" -e "password=<password>"-p 9119:9119 nopp/alertmanager-webhook-telegram:latest
34+
35+
> make sure set proper username and password when you exposing your app on internet
3436
3537
Exemple to test
3638
===============
37-
curl -XPOST --data '{"status":"resolved","groupLabels":{"alertname":"instance_down"},"commonAnnotations":{"description":"i-0d7188fkl90bac100 of job ec2-sp-node_exporter has been down for more than 2 minutes.","summary":"Instance i-0d7188fkl90bac100 down"},"alerts":[{"status":"resolved","labels":{"name":"olokinho01-prod","instance":"i-0d7188fkl90bac100","job":"ec2-sp-node_exporter","alertname":"instance_down","os":"linux","severity":"page"},"endsAt":"2019-07-01T16:16:19.376244942-03:00","generatorURL":"http://pmts.io:9090","startsAt":"2019-07-01T16:02:19.376245319-03:00","annotations":{"description":"i-0d7188fkl90bac100 of job ec2-sp-node_exporter has been down for more than 2 minutes.","summary":"Instance i-0d7188fkl90bac100 down"}}],"version":"4","receiver":"infra-alert","externalURL":"http://alm.io:9093","commonLabels":{"name":"olokinho01-prod","instance":"i-0d7188fkl90bac100","job":"ec2-sp-node_exporter","alertname":"instance_down","os":"linux","severity":"page"}}' http://flaskAlert:9119/alert
39+
curl -XPOST --data '{"status":"resolved","groupLabels":{"alertname":"instance_down"},"commonAnnotations":{"description":"i-0d7188fkl90bac100 of job ec2-sp-node_exporter has been down for more than 2 minutes.","summary":"Instance i-0d7188fkl90bac100 down"},"alerts":[{"status":"resolved","labels":{"name":"olokinho01-prod","instance":"i-0d7188fkl90bac100","job":"ec2-sp-node_exporter","alertname":"instance_down","os":"linux","severity":"page"},"endsAt":"2019-07-01T16:16:19.376244942-03:00","generatorURL":"http://pmts.io:9090","startsAt":"2019-07-01T16:02:19.376245319-03:00","annotations":{"description":"i-0d7188fkl90bac100 of job ec2-sp-node_exporter has been down for more than 2 minutes.","summary":"Instance i-0d7188fkl90bac100 down"}}],"version":"4","receiver":"infra-alert","externalURL":"http://alm.io:9093","commonLabels":{"name":"olokinho01-prod","instance":"i-0d7188fkl90bac100","job":"ec2-sp-node_exporter","alertname":"instance_down","os":"linux","severity":"page"}}' http://username:password@flaskAlert:9119/alert

docker/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22
sed -i s/botToken/"$bottoken"/ flaskAlert.py
33
sed -i s/xchatIDx/"$chatid"/ flaskAlert.py
4+
sed -i s/XXXUSERNAME/"$username"/ flaskAlert.py
5+
sed -i s/XXXPASSWORD/"$password"/ flaskAlert.py
46

57
/usr/bin/gunicorn -w 4 -b 0.0.0.0:9119 flaskAlert:app

flaskAlert.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,32 @@
33
import json
44
from flask import Flask
55
from flask import request
6+
from flask_basicauth import BasicAuth
67

78
app = Flask(__name__)
89
app.secret_key = 'aYT>.L$kk2h>!'
10+
app.config['BASIC_AUTH_USERNAME'] = 'XXXUSERNAME'
11+
app.config['BASIC_AUTH_PASSWORD'] = 'XXXPASSWORD'
912

13+
basic_auth = BasicAuth(app)
14+
app.config['BASIC_AUTH_FORCE'] = True
1015
bot = telegram.Bot(token="botToken")
1116
chatID = "-xchatIDx"
1217

1318
@app.route('/alert', methods = ['POST'])
1419
def postAlertmanager():
1520

1621
content = json.loads(request.get_data())
17-
22+
with open("Output.txt", "w") as text_file:
23+
text_file.write("{0}".format(content))
1824
try:
1925

2026
for alert in content['alerts']:
2127

2228
if 'name' in alert['labels']:
2329

2430
message = """
25-
Status """+alert['status']+"""
31+
Status """+alert['status']+"""
2632
Alertname: """+alert['labels']['alertname']+"""
2733
Instance: """+alert['labels']['instance']+"""("""+alert['labels']['name']+""")
2834
"""+alert['annotations']['description']+"""

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
flask
22
python-telegram-bot
3+
Flask-BasicAuth
34
gunicorn

0 commit comments

Comments
 (0)