From b68064028f1abdf016a4f387beab70836aa4988a Mon Sep 17 00:00:00 2001 From: Matthew Stidham Date: Tue, 20 Apr 2021 11:45:36 -0700 Subject: [PATCH] Fix influx2.py Signed-off-by: Matthew Stidham --- py-dashboard/GrafanaRequest.py | 60 ++++++++++++++++++++++++++++++++++ py-scripts/GrafanaRequest.py | 0 py-scripts/grafana_profile.py | 0 py-scripts/influx2.py | 5 +++ 4 files changed, 65 insertions(+) create mode 100644 py-dashboard/GrafanaRequest.py create mode 100644 py-scripts/GrafanaRequest.py create mode 100644 py-scripts/grafana_profile.py diff --git a/py-dashboard/GrafanaRequest.py b/py-dashboard/GrafanaRequest.py new file mode 100644 index 00000000..37cbf3ae --- /dev/null +++ b/py-dashboard/GrafanaRequest.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 + +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +# Class holds default settings for json requests to Grafana - +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +import sys + +if sys.version_info[0] != 3: + print("This script requires Python 3") + exit() + +import requests + +import json + + +class GrafanaRequest: + def __init__(self, + _grafanajson_host, + _grafanajson_port, + _folderID=0, + _api_token=None, + _headers=dict(), + _overwrite='false', + debug_=False, + die_on_error_=False): + self.debug = debug_ + self.die_on_error = die_on_error_ + self.headers = _headers + self.headers['Authorization'] = 'Bearer '+_api_token + self.headers['Content-Type'] = 'application/json' + self.grafanajson_url = "http://%s:%s" % (_grafanajson_host, _grafanajson_port) + self.data = dict() + self.data['overwrite'] = _overwrite + + def create_bucket(self, + bucket_name=None): + # Create a bucket in Grafana + if bucket_name is not None: + pass + + def list_dashboards(self): + url=self.grafanajson_url + '/api/search?folderIds=0&query=&starred=false' + return requests.get(url).text + + def create_dashboard(self, + dashboard_name=None, + ): + self.grafanajson_url = self.grafanajson_url + "/api/dashboards/db" + + data = ('{ "dashboard": { "id": null, "title": "%s" , "tags": [ "templated" ], "timezone": "browser", "schemaVersion": 6, "version": 0 }, "overwrite": false }' % dashboard_name) + return requests.get(self.grafanajson_url, headers=self.headers, data=data, verify=False) + + def delete_dashboard(self, + dashboard_uid=None): + self.grafanajson_url = self.grafanajson_url + "/api/dashboards/uid/" + dashboard_uid + return requests.post(self.grafanajson_url, headers=self.headers, verify=False) + + def create_dashboard_from_data(self, + json_file=None): diff --git a/py-scripts/GrafanaRequest.py b/py-scripts/GrafanaRequest.py new file mode 100644 index 00000000..e69de29b diff --git a/py-scripts/grafana_profile.py b/py-scripts/grafana_profile.py new file mode 100644 index 00000000..e69de29b diff --git a/py-scripts/influx2.py b/py-scripts/influx2.py index e9f29e1e..9ef92679 100644 --- a/py-scripts/influx2.py +++ b/py-scripts/influx2.py @@ -10,6 +10,11 @@ if sys.version_info[0] != 3: print("This script requires Python 3") exit(1) + +if 'py-json' not in sys.path: + sys.path.append(os.path.join(os.path.abspath('..'), 'py-json')) + + import requests import json import influxdb_client