Skip to content

Instantly share code, notes, and snippets.

@ziouf
Last active May 30, 2018 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziouf/0cdf09a5fe03fbf06ce9781a66f944cf to your computer and use it in GitHub Desktop.
Save ziouf/0cdf09a5fe03fbf06ce9781a66f944cf to your computer and use it in GitHub Desktop.
Grafana notification to Yammer gateway

Notification gateway from Grafana to Yammer

Why this GIST ?

Yammer notifications are not implemented in Grafana, by default. So I decided to develop a little gateway that publish Grafana alarm webhooks to Yammer thread.

Configuration

Grafana

Add Webhook notification channel to Grafana configuration pointing to gateway url (with following format). Check include image.

Yammer application

In your Yammer configuration panel, add a new custom app. Then generate development token. Copy/past it in env.txt.

Gateway url format

http://<grafana-yammer-gw>:<port>/publish/<yammer-group-id>

Build

docker build -t ziouf/grafana-yammer-gw -f dockerfile -q --rm .
docker run -d --env-file env.txt -p 80:80 ziouf/grafana-yammer-gw
FROM python:3-slim-stretch
ENV PORT 80
ENV IP 0.0.0.0
ENV WORKERS 1
ENV LOG_LEVEL info
COPY requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt && rm /tmp/requirements.txt
WORKDIR /opt
COPY gateway.py /opt/
CMD gunicorn -w ${WORKERS} -b ${IP}:${PORT} --log-level ${LOG_LEVEL} gateway:app
TOKEN=...
PORT=80
WORKERS=2
LOG_LEVEL=info
import logging
import os
import sys
import yampy
from flask import Flask, request
TOKEN = os.environ.get('TOKEN')
app = Flask(__name__)
yam = yampy.Yammer(access_token=TOKEN)
if __name__ != '__main__':
# Workarround to register Flask logger in Gunicorn logger
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
# If TOKEN not set, exit with error
if TOKEN == None:
msg = "Environment variable TOKEN must be set !"
app.logger.error(msg)
sys.exit(msg)
@app.route('/publish/<int:group_id>', methods=['POST'])
def publish(group_id):
json_hook_data = request.get_json(force=True, silent=True)
app.logger.debug("Received POST call for gid [{}] with data : {}".format(group_id, json_hook_data))
# Prepare msg to publish
msg = "{}: {} \n[{}: {}]".format(json_hook_data['title'], json_hook_data['message'], json_hook_data['ruleName'],
json_hook_data['ruleUrl'])
for m in json_hook_data['evalMatches']:
msg = "{}\n => {} : {}".format(msg, m['metric'], m['value'])
# Publish message on Yammer
yam.messages.create(msg, group_id=group_id,
open_graph_object={'image': json_hook_data['imageUrl'], 'url': json_hook_data['imageUrl']},
topics=["Grafana", "Alarm", json_hook_data['ruleName']])
return "Success"
yampy3
flask
gunicorn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment