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).
This commit is contained in:
emschu 2019-10-25 17:49:20 +00:00 committed by John R Barker
parent 0f6ca6c4c5
commit 9d91e6275e

View file

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