enable grafana_annotations callback sending annotations to multiple panels (#57575)

* enable grafana_annotations callback plugin message sending to multiple panels

* remove new line at end of file

* restructuring code of grafana_annotations cb plugin

* add missing self keyword

* grafana callback: use list for panel_ids

* grafana callback: convert panel_id to integer

annotations HTTP API states that panelId are integer (same as
dashboardId which is also converted).
pull/63958/head
emschu 5 years ago committed by John R Barker
parent 0f6ca6c4c5
commit 9d91e6275e

@ -48,6 +48,7 @@ DOCUMENTATION = """
ini:
- section: callback_grafana_annotations
key: grafana_url
type: string
validate_certs:
description: validate the SSL certificate of the Grafana server. (For HTTPS url)
env:
@ -68,6 +69,7 @@ DOCUMENTATION = """
- section: callback_grafana_annotations
key: http_agent
default: 'Ansible (grafana_annotations callback)'
type: string
grafana_api_key:
description: Grafana API key, allowing to authenticate when posting on the HTTP API.
If not provided, grafana_login and grafana_password will
@ -77,6 +79,7 @@ DOCUMENTATION = """
ini:
- section: callback_grafana_annotations
key: grafana_api_key
type: string
grafana_user:
description: Grafana user used for authentication. Ignored if grafana_api_key is provided.
env:
@ -85,6 +88,7 @@ DOCUMENTATION = """
- section: callback_grafana_annotations
key: grafana_user
default: ansible
type: string
grafana_password:
description: Grafana password used for authentication. Ignored if grafana_api_key is provided.
env:
@ -93,6 +97,7 @@ DOCUMENTATION = """
- section: callback_grafana_annotations
key: grafana_password
default: ansible
type: string
grafana_dashboard_id:
description: The grafana dashboard id where the annotation shall be created.
env:
@ -100,13 +105,17 @@ DOCUMENTATION = """
ini:
- section: callback_grafana_annotations
key: grafana_dashboard_id
grafana_panel_id:
description: The grafana panel id where the annotation shall be created.
type: integer
grafana_panel_ids:
description: The grafana panel ids where the annotation shall be created.
Give a single integer or a comma-separated list of integers.
env:
- name: GRAFANA_PANEL_ID
- name: GRAFANA_PANEL_IDS
ini:
- section: callback_grafana_annotations
key: grafana_panel_id
key: grafana_panel_ids
default: []
type: list
"""
@ -181,7 +190,7 @@ class CallbackModule(CallbackBase):
self.grafana_user = self.get_option('grafana_user')
self.grafana_password = self.get_option('grafana_password')
self.dashboard_id = self.get_option('grafana_dashboard_id')
self.panel_id = self.get_option('grafana_panel_id')
self.panel_ids = self.get_option('grafana_panel_ids')
if self.grafana_api_key:
self.headers['Authorization'] = "Bearer %s" % self.grafana_api_key
@ -204,11 +213,7 @@ class CallbackModule(CallbackBase):
'text': text,
'tags': ['ansible', 'ansible_event_start', self.playbook]
}
if self.dashboard_id:
data["dashboardId"] = int(self.dashboard_id)
if self.panel_id:
data["panelId"] = int(self.panel_id)
self._send_annotation(json.dumps(data))
self._send_annotation(data)
def v2_playbook_on_stats(self, stats):
end_time = datetime.now()
@ -233,11 +238,7 @@ class CallbackModule(CallbackBase):
'text': text,
'tags': ['ansible', 'ansible_report', self.playbook]
}
if self.dashboard_id:
data["dashboardId"] = int(self.dashboard_id)
if self.panel_id:
data["panelId"] = int(self.panel_id)
self._send_annotation(json.dumps(data))
self._send_annotations(data)
def v2_runner_on_failed(self, result, **kwargs):
text = PLAYBOOK_ERROR_TXT.format(playbook=self.playbook, hostname=self.hostname,
@ -249,15 +250,21 @@ class CallbackModule(CallbackBase):
'tags': ['ansible', 'ansible_event_failure', self.playbook]
}
self.errors += 1
self._send_annotations(data)
def _send_annotations(self, data):
if self.dashboard_id:
data["dashboardId"] = int(self.dashboard_id)
if self.panel_id:
data["panelId"] = int(self.panel_id)
self._send_annotation(json.dumps(data))
if self.panel_ids:
for panel_id in self.panel_ids:
data["panelId"] = int(panel_id)
self._send_annotation(data)
else:
self._send_annotation(data)
def _send_annotation(self, annotation):
try:
response = open_url(self.grafana_url, data=annotation, headers=self.headers,
response = open_url(self.grafana_url, data=json.dumps(annotation), headers=self.headers,
method="POST",
validate_certs=self.validate_grafana_certs,
url_username=self.grafana_user, url_password=self.grafana_password,

Loading…
Cancel
Save