added support to message rocketchat
This commit is contained in:
parent
8df5f497d8
commit
ae12f28eb4
5 changed files with 118 additions and 2 deletions
|
|
@ -1,5 +1,6 @@
|
|||
prometheus_alertmanager:
|
||||
telegrambot: False
|
||||
rocketchatbot: False
|
||||
args:
|
||||
"web.listen-address": "[::1]:9093"
|
||||
config:
|
||||
|
|
|
|||
14
files/prometheus-rocketchat-bot.service
Normal file
14
files/prometheus-rocketchat-bot.service
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[Unit]
|
||||
Description=Sends alerts via rocketchat
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/local/bin/prometheus-rocketchat-bot.py PROD
|
||||
|
||||
DynamicUser=yes
|
||||
NoNewPrivileges=True
|
||||
ProtectSystem=strict
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
|
||||
|
|
@ -8,3 +8,9 @@
|
|||
enabled: True
|
||||
daemon_reload: True
|
||||
state: restarted
|
||||
- name: restart rocketchat bot
|
||||
service:
|
||||
name: prometheus-rocketchat-bot
|
||||
enabled: True
|
||||
daemon_reload: True
|
||||
state: restarted
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
src: prometheus-alertmanager.j2
|
||||
dest: /etc/default/prometheus-alertmanager
|
||||
|
||||
- name: wrtie alertmanager config
|
||||
- name: write alertmanager config
|
||||
notify: restart alertmanager
|
||||
copy:
|
||||
owner: root
|
||||
|
|
@ -27,7 +27,6 @@
|
|||
- python3-dateutil
|
||||
- python3-gevent
|
||||
- python3-pip
|
||||
# - python3-telegram-bot
|
||||
- pip:
|
||||
name: python-telegram-bot
|
||||
executable: pip3
|
||||
|
|
@ -46,3 +45,32 @@
|
|||
dest: /etc/systemd/system
|
||||
owner: root
|
||||
group: root
|
||||
|
||||
- name: setup rocketchat bot
|
||||
when: prometheus_alertmanager.rocketchatbot
|
||||
block:
|
||||
- name: install dependencies
|
||||
apt:
|
||||
pkg:
|
||||
- python3-flask
|
||||
- python3-dateutil
|
||||
- python3-gevent
|
||||
- python3-pip
|
||||
- pip:
|
||||
name: rocketchat_API
|
||||
executable: pip3
|
||||
- name: deploy rocketchat bot
|
||||
notify: restart rocketchat bot
|
||||
template:
|
||||
src: rocketchat-bot.py.j2
|
||||
dest: /usr/local/bin/prometheus-rocketchat-bot.py
|
||||
mode: 0755
|
||||
owner: root
|
||||
group: root
|
||||
- name: Copy systemd service file
|
||||
notify: restart rocketchat bot
|
||||
copy:
|
||||
src: prometheus-rocketchat-bot.service
|
||||
dest: /etc/systemd/system
|
||||
owner: root
|
||||
group: root
|
||||
|
|
|
|||
67
templates/rocketchat-bot.py.j2
Executable file
67
templates/rocketchat-bot.py.j2
Executable file
|
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
from dateutil import parser
|
||||
import json, logging
|
||||
from flask import Flask, request
|
||||
from rocketchat_API.rocketchat import RocketChat
|
||||
from requests import sessions
|
||||
|
||||
|
||||
listenPort = 29120
|
||||
listenIP = '::1'
|
||||
botUser = '{{ prometheus_alertmanager.rocketchatbot.user }}'
|
||||
botPass = '{{ prometheus_alertmanager.rocketchatbot.pass }}'
|
||||
botChatURL ='{{ prometheus_alertmanager.rocketchatbot.url }}'
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.urandom(128)
|
||||
|
||||
api_session = sessions.Session()
|
||||
api = RocketChat(botUser, botPass, server_url=botChatURL, session=api_session)
|
||||
|
||||
@app.route('/<chatName>/alert', methods = ['POST'])
|
||||
def postAlertmanager(chatName):
|
||||
try:
|
||||
content = json.loads(request.get_data())
|
||||
for alert in content['alerts']:
|
||||
message = "Status: "+alert['status']+"\n"
|
||||
if 'name' in alert['labels']:
|
||||
message += "Instance: "+alert['labels'].get('instance', 'unknown')+"("+alert['labels']['name']+")\n"
|
||||
else:
|
||||
message += "Instance: "+alert['labels'].get('instance', 'unknown')+"\n"
|
||||
if 'info' in alert['annotations']:
|
||||
message += "Info: "+alert['annotations']['info']+"\n"
|
||||
if 'summary' in alert['annotations']:
|
||||
message += "Summary: "+alert['annotations']['summary']+"\n"
|
||||
if 'description' in alert['annotations']:
|
||||
message += "Description: "+alert['annotations']['description']+"\n"
|
||||
if alert['status'] == "resolved":
|
||||
correctDate = parser.parse(alert['endsAt']).strftime('%Y-%m-%d %H:%M:%S')
|
||||
message += "Resolved: "+correctDate+"\n"
|
||||
elif alert['status'] == "firing":
|
||||
correctDate = parser.parse(alert['startsAt']).strftime('%Y-%m-%d %H:%M:%S')
|
||||
message += "Started: "+correctDate+"\n"
|
||||
message += "labels: \n"
|
||||
message += "```\n"
|
||||
labels = ""
|
||||
for l in alert['labels']:
|
||||
labels += l + " : "+alert['labels'][l] + "\n"
|
||||
message += labels + "```"
|
||||
if api.chat_post_message(message, channel=chatName, alias='Alertmanager').ok:
|
||||
return "Alert OK", 200
|
||||
else:
|
||||
return "Alert fail", 200
|
||||
except Exception as error:
|
||||
bot.sendMessage(chat_id=chatID, text="Error to read json: "+str(error))
|
||||
app.logger.info("\t%s",error)
|
||||
return "Alert fail", 200
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) == 1:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
app.run(host=listenIP, port=listenPort)
|
||||
else:
|
||||
from gevent.pywsgi import WSGIServer
|
||||
http_server = WSGIServer((listenIP, listenPort), app)
|
||||
http_server.serve_forever()
|
||||
Loading…
Add table
Add a link
Reference in a new issue