From c82c2b8ef73caa1e130e44e0e4ea81333c52610d Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Tue, 12 Jun 2018 13:04:25 +1000 Subject: [PATCH] Add alerts to graphs Graphs can setup alerts. Add a basic framework for them. Change-Id: I6a80041eaaa03a3b3ff3e3de76401bbc77f42e96 --- grafana_dashboards/schema/panel/graph.py | 14 ++++ tests/schema/fixtures/dashboard-0031.json | 90 +++++++++++++++++++++++ tests/schema/fixtures/dashboard-0031.yaml | 41 +++++++++++ 3 files changed, 145 insertions(+) create mode 100644 tests/schema/fixtures/dashboard-0031.json create mode 100644 tests/schema/fixtures/dashboard-0031.yaml diff --git a/grafana_dashboards/schema/panel/graph.py b/grafana_dashboards/schema/panel/graph.py index e33d773..66c54a5 100644 --- a/grafana_dashboards/schema/panel/graph.py +++ b/grafana_dashboards/schema/panel/graph.py @@ -22,6 +22,19 @@ from grafana_dashboards.schema.panel.base import Base class Graph(Base): def get_schema(self): + + alert_format = { + # could enforce "evaulator"/"operator"/"query" on this... + v.Required('conditions'): v.All(list), + v.Required('frequency', default='60s'): v.All(str), + v.Required('name'): v.All(str), + v.Required('executionErrorState', default='alerting'): ( + v.Any('alerting', 'keep_state')), + v.Required('noDataState', default='no_data'): ( + v.Any('no_data', 'alerting', 'ok', 'keep_state')), + v.Optional('notifications', default=[]): v.All(list), + } + yaxes_format = [ { v.Optional('decimals'): v.All(int), @@ -76,6 +89,7 @@ class Graph(Base): series_overrides = [series_override] graph = { + v.Optional('alert'): v.All(alert_format), v.Required('bars', default=False): v.All(bool), v.Optional('datasource'): v.All(str), v.Optional('decimals'): v.All(int), diff --git a/tests/schema/fixtures/dashboard-0031.json b/tests/schema/fixtures/dashboard-0031.json new file mode 100644 index 0000000..f03d233 --- /dev/null +++ b/tests/schema/fixtures/dashboard-0031.json @@ -0,0 +1,90 @@ +{ + "dashboard": { + "new-dashboard": { + "rows": [ + { + "collapse": false, + "editable": true, + "height": "250px", + "panels": [ + { + "alert": { + "conditions": [ + { + "evaluator": { + "params": [ + 0.85 + ], + "type": "gt" + }, + "operator": { + "type": "and" + }, + "query": { + "params": [ + "C", + "5m", + "now" + ] + }, + "reducer": { + "params": [], + "type": "avg" + }, + "type": "query" + } + ], + "executionErrorState": "alerting", + "frequency": "60s", + "name": "A test alert", + "noDataState": "no_data", + "notifications": [] + }, + "bars": false, + "datasource": "graphite", + "editable": true, + "error": false, + "fill": 1, + "lines": true, + "linewidth": 2, + "percentage": false, + "pointradius": 5, + "points": false, + "span": 12, + "stack": false, + "steppedLine": false, + "targets": [ + { + "hide": true, + "refId": "A", + "target": "stats.gauges.afs.afs01_dfw_openstack_org.part.vicepa.total" + }, + { + "hide": false, + "refId": "B", + "target": "divideSeries(stats.gauges.afs.afs01_dfw_openstack_org.part.vicpa.used, #A)" + } + ], + "title": "no title (click here)", + "type": "graph", + "x-axis": true, + "y-axis": true + } + ], + "showTitle": false, + "title": "New row" + } + ], + "templating": { + "enabled": false, + "list": [] + }, + "time": { + "from": "2018-02-07T08:42:27.000Z", + "to": "2018-02-07T13:48:32.000Z" + }, + "timezone": "utc", + "title": "New dashboard" + } + } +} diff --git a/tests/schema/fixtures/dashboard-0031.yaml b/tests/schema/fixtures/dashboard-0031.yaml new file mode 100644 index 0000000..e261915 --- /dev/null +++ b/tests/schema/fixtures/dashboard-0031.yaml @@ -0,0 +1,41 @@ +dashboard: + time: + from: "2018-02-07T08:42:27.000Z" + to: "2018-02-07T13:48:32.000Z" + title: New dashboard + rows: + - title: New row + height: 250px + panels: + - title: no title (click here) + type: graph + datasource: graphite + targets: + - target: "stats.gauges.afs.afs01_dfw_openstack_org.part.vicepa.total" + refId: "A" + hide: true + - target: "divideSeries(stats.gauges.afs.afs01_dfw_openstack_org.part.vicpa.used, #A)" + refId: "B" + hide: false + alert: + conditions: + - evaluator: + params: + - 0.85 + type: gt + operator: + type: and + query: + params: + - C + - 5m + - now + reducer: + params: [] + type: avg + type: query + executionErrorState: alerting + frequency: 60s + name: A test alert + noDataState: no_data +