Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from rrey/fix_doc_and_tool
Browse files Browse the repository at this point in the history
Fix doc and tool
  • Loading branch information
rrey authored Mar 18, 2018
2 parents 6d62137 + 2b4bd49 commit a0b35cf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ Automatically publish annotations in grafana when you execute your playbooks !
* Export the required environement variables:

```
$ export GRAFANA_SERVER=<your_grafana_server_address>
$ export GRAFANA_PORT=<your_grafana_server_port>
$ export GRAFANA_HOST=<your_grafana_server_address>:<your_grafana_server_port>
$ export GRAFANA_SECURE=0 # 0 for HTTP, 1 for HTTPS
$ export GRAFANA_API_TOKEN=<your_grafana_api_token>
$ export GRAFANA_API_KEY=<your_grafana_api_token>
```

For token based authentication:

```
$ export GRAFANA_API_KEY=<your_grafana_api_key>
```

For basic http authentication:

```
$ export GRAFANA_USER=<your_grafana_user>
$ export GRAFANA_PASSWORD=<your_grafana_user_password>
```

* Run you playbook:
Expand All @@ -20,3 +32,22 @@ $ ansible-playbook test.yml
```
* See the deployment in grafana:
![Grafana annotations](/screenshot/result.png)

# Restrict the annotations to a specific dashboard

If the annotations should be restricted to a particular dashboard, you can
specify its dashboard id through the dedicated environment variable:

```
$ export GRAFANA_DASHBOARD_ID=<the_grafana_dashboard_id>
```

# Restrict the annotations to a specific panel in a dashboard

If the annotations should be restricted to a particular panel in a dashboard, you can
specify both dashboard id and panel id through the dedicated environment variables:

```
$ export GRAFANA_DASHBOARD_ID=<the_grafana_dashboard_id>
$ export GRAFANA_PANEL_ID=<the_grafana_panel_id>
```
19 changes: 5 additions & 14 deletions callback_plugins/grafana_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
description: Grafana password used for authentication. Ignored if api_key is provided.
env:
- name: GRAFANA_PASSWORD
grafana_dashboard:
grafana_dashboard_id:
description: The grafana dashboard id where the annotation shall be created.
env:
- name: GRAFANA_DASHBOARD_ID
grafana_panel:
grafana_panel_id:
description: The grafana panel id where the annotation shall be created.
env:
- name: GRAFANA_PANEL_ID
Expand Down Expand Up @@ -100,15 +100,6 @@ class CallbackModule(CallbackBase):
callback_plugins = <path_to_callback_plugins_folder>
callback_whitelist = grafana_annotations
and put the plugin in <path_to_callback_plugins_folder>
This plugin makes use of the following environment variables:
GRAFANA_HOST (optional) : Grafana server host and port.
Defaults to 127.0.0.1:3000
GRAFANA_API_KEY (optional) : Grafana API key used for authentication.
GRAFANA_USER (optional) : Grafana user used for authentication.
GRAFANA_PASSWORD (optional) : Grafana password used for authentication.
GRAFANA_PANEL_ID (optional) : Grafana panel id where the annotation is emitted.
GRAFANA_DASHBOARD_ID (optional) : Grafana dashboard id where the annotation is emitted.
"""

CALLBACK_VERSION = 1.0
Expand All @@ -123,14 +114,14 @@ def __init__(self):
self.secure = int(os.getenv('GRAFANA_SECURE', 0))
if self.secure not in [0, 1]:
self.secure = 0
token = os.getenv('GRAFANA_API_KEY', None)
api_key = os.getenv('GRAFANA_API_KEY', None)
grafana_user = os.getenv('GRAFANA_USER', None)
grafana_password = os.getenv('GRAFANA_PASSWORD', '')
self.dashboard_id = os.getenv('GRAFANA_DASHBOARD_ID', None)
self.panel_id = os.getenv('GRAFANA_PANEL_ID', None)

if token:
authorization = "Bearer %s" % token
if api_key:
authorization = "Bearer %s" % api_key
elif grafana_user is not None:
authorization = "Basic %s" % b64encode("%s:%s" % (grafana_user, grafana_password))
else:
Expand Down
19 changes: 8 additions & 11 deletions tools/get_or_create_token.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import httplib
import os
import sys
import json
import argparse
from base64 import b64encode

URI = "/api/auth/keys"
GRAFANA_USER = os.getenv("GRAFANA_USER", "admin")
GRAFANA_PASSWORD = os.getenv("GRAFANA_PASSWORD", "admin")
GRAFANA_HOST = os.getenv("GRAFANA_HOST", "127.0.0.1:3000")

parser = argparse.ArgumentParser()
parser.add_argument("--user", default="admin", help="login for basic authentication")
parser.add_argument("--passwd", default="admin", help="password for basic authentication")
parser.add_argument("--host", default="127.0.0.1", help="Grafana host address")
parser.add_argument("--port", default="3000", help="Grafana listen port")
args = parser.parse_args()

cred = "%s:%s" % (args.user, args.passwd)
cred = "%s:%s" % (GRAFANA_USER, GRAFANA_PASSWORD)
HEADERS = {"Content-Type": "application/json", "Authorization": "Basic %s" % b64encode(cred).decode('ascii')}

http = httplib.HTTPConnection(args.host, args.port)
http = httplib.HTTPConnection(GRAFANA_HOST)
http.request("GET", URI, headers=HEADERS)
response = http.getresponse()
if response.status != 200:
Expand All @@ -28,14 +25,14 @@
if token.get("name") == "ansible-module":
token_id = token.get('id')
if token_id:
http = httplib.HTTPConnection(args.host, args.port)
http = httplib.HTTPConnection(GRAFANA_HOST)
http.request("DELETE", "%s/%d" % (URI, token_id), headers=HEADERS)
response = http.getresponse()
if response.status != 200:
print "Failed to remove 'ansible-module' token [%s]" % response.read()
sys.exit(1)

http = httplib.HTTPConnection(args.host, args.port)
http = httplib.HTTPConnection(GRAFANA_HOST)
http.request("POST", URI, '{"name": "ansible-module", "role": "Editor"}', headers=HEADERS)
response = http.getresponse()
if response.status != 200:
Expand Down

0 comments on commit a0b35cf

Please sign in to comment.