| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2018 Whitestack, LLC |
| 4 | # ************************************************************* |
| 5 | |
| 6 | # This file is part of OSM Monitoring module |
| 7 | # All Rights Reserved to Whitestack, LLC |
| 8 | |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 21 | # contact: glavado@whitestack.com or fbravo@whitestack.com |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 22 | ## |
| 23 | import logging |
| 24 | import requests |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 25 | import base64 |
| 26 | from osm_mon.core.config import Config |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 27 | |
| 28 | log = logging.getLogger(__name__) |
| 29 | |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 30 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 31 | class GrafanaBackend: |
| 32 | def __init__(self, config: Config): |
| 33 | self.conf = config |
| 34 | self.url = config.get('grafana', 'url') |
| 35 | grafana_user = config.get("grafana", "user") |
| 36 | grafana_password = config.get("grafana", "password") |
| 37 | self.headers = { |
| 38 | 'content-type': "application/json", |
| 39 | 'authorization': "Basic %s" % base64.b64encode( |
| 40 | (grafana_user + ":" + grafana_password).encode("utf-8")).decode() |
| 41 | } |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 42 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 43 | def get_all_dashboard_uids(self): |
| 44 | # Gets only dashboards that were automated by OSM (with tag 'osm_automated') |
| 45 | response = requests.request("GET", self.url + "/api/search?tag=osm_automated", headers=self.headers) |
| 46 | dashboards = response.json() |
| 47 | dashboard_uids = [] |
| 48 | for dashboard in dashboards: |
| 49 | dashboard_uids.append(dashboard['uid']) |
| 50 | log.debug("Searching for all dashboard uids: %s", dashboard_uids) |
| 51 | return dashboard_uids |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 52 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 53 | def get_dashboard_status(self, uid): |
| 54 | response = requests.request("GET", self.url + "/api/dashboards/uid/" + uid, headers=self.headers) |
| 55 | log.debug("Searching for dashboard result: %s", response.text) |
| 56 | return response |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 57 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 58 | def create_dashboard(self, uid, name, json_file): |
| 59 | with open(json_file) as f: |
| 60 | dashboard_data = f.read() |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 61 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 62 | dashboard_data = dashboard_data.replace('OSM_ID', uid).replace('OSM_NAME', name) |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 63 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 64 | response = requests.request( |
| 65 | "POST", self.url + "/api/dashboards/db/", data=dashboard_data, headers=self.headers) |
| 66 | log.debug("Creating dashboard result: %s", response.text) |
| 67 | return response |
| lavado | 456d0f3 | 2019-11-15 17:04:02 -0500 | [diff] [blame] | 68 | |
| bravof | 396648b | 2020-03-31 18:42:45 -0300 | [diff] [blame] | 69 | def delete_dashboard(self, uid): |
| 70 | response = requests.request("DELETE", self.url + "/api/dashboards/uid/" + uid, headers=self.headers) |
| 71 | log.debug("Delete dashboard result: %s", response.text) |
| 72 | return response |