mirror of
				https://github.com/Telecominfraproject/wlan-lanforge-scripts.git
				synced 2025-10-31 10:48:02 +00:00 
			
		
		
		
	Working create_dashboard from grafana_profile.py
Signed-off-by: Matthew Stidham <stidmatt@protonmail.com>
This commit is contained in:
		| @@ -47,10 +47,18 @@ class GrafanaRequest: | |||||||
|                          dashboard_name=None, |                          dashboard_name=None, | ||||||
|                          ): |                          ): | ||||||
|         self.grafanajson_url = self.grafanajson_url + "/api/dashboards/db" |         self.grafanajson_url = self.grafanajson_url + "/api/dashboards/db" | ||||||
|  |         datastore = dict() | ||||||
|         data = ( |         dashboard = dict() | ||||||
|                     '{ "dashboard": { "id": null, "title": "%s" , "tags": [ "templated" ], "timezone": "browser", "schemaVersion": 6, "version": 0 }, "overwrite": false }' % dashboard_name) |         dashboard['id'] = None | ||||||
|         return requests.get(self.grafanajson_url, headers=self.headers, data=data, verify=False) |         dashboard['title'] = dashboard_name | ||||||
|  |         dashboard['tags'] = ['templated'] | ||||||
|  |         dashboard['timezone'] = 'browser' | ||||||
|  |         dashboard['shemaVersion'] = 6 | ||||||
|  |         dashboard['version'] = 0 | ||||||
|  |         datastore['dashboard'] = dashboard | ||||||
|  |         datastore['overwrite'] = False | ||||||
|  |         data = json.dumps(datastore, indent=4) | ||||||
|  |         return requests.post(self.grafanajson_url, headers=self.headers, data=data, verify=False) | ||||||
|  |  | ||||||
|     def delete_dashboard(self, |     def delete_dashboard(self, | ||||||
|                          dashboard_uid=None): |                          dashboard_uid=None): | ||||||
|   | |||||||
							
								
								
									
										87
									
								
								py-scripts/grafana_profile.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										87
									
								
								py-scripts/grafana_profile.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							| @@ -0,0 +1,87 @@ | |||||||
|  | #!/usr/bin/env python3 | ||||||
|  |  | ||||||
|  | import sys | ||||||
|  | import os | ||||||
|  | import argparse | ||||||
|  |  | ||||||
|  | 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')) | ||||||
|  |     sys.path.append(os.path.join(os.path.abspath('..'), 'py-dashboard')) | ||||||
|  |  | ||||||
|  | from GrafanaRequest import GrafanaRequest | ||||||
|  | from LANforge.lfcli_base import LFCliBase | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class UseGrafana(LFCliBase): | ||||||
|  |     def __init__(self, | ||||||
|  |                  _grafana_token, | ||||||
|  |                  host="localhost", | ||||||
|  |                  _grafana_host="localhost", | ||||||
|  |                  port=8080, | ||||||
|  |                  _debug_on=False, | ||||||
|  |                  _exit_on_fail=False, | ||||||
|  |                  _grafana_port=3000): | ||||||
|  |         super().__init__(host, port, _debug=_debug_on, _exit_on_fail=_exit_on_fail) | ||||||
|  |         self.grafana_token = _grafana_token | ||||||
|  |         self.grafana_port = _grafana_port | ||||||
|  |         self.grafana_host = _grafana_host | ||||||
|  |         self.GR = GrafanaRequest(self.grafana_host, str(self.grafana_port), _folderID=0, _api_token=self.grafana_token) | ||||||
|  |  | ||||||
|  |     def create_dashboard(self, | ||||||
|  |                          dashboard_name): | ||||||
|  |         return self.GR.create_dashboard(dashboard_name) | ||||||
|  |  | ||||||
|  |     def delete_dashboard(self, | ||||||
|  |                          dashboard_uid): | ||||||
|  |         return self.GR.delete_dashboard(dashboard_uid) | ||||||
|  |  | ||||||
|  |     def list_dashboards(self): | ||||||
|  |         return self.GR.list_dashboards() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def main(): | ||||||
|  |     parser = LFCliBase.create_basic_argparse( | ||||||
|  |         prog='grafana_profile.py', | ||||||
|  |         formatter_class=argparse.RawTextHelpFormatter, | ||||||
|  |         epilog='''Manage Grafana database''', | ||||||
|  |         description='''\ | ||||||
|  |         grafana_profile.py | ||||||
|  |         ------------------ | ||||||
|  |         Command example: | ||||||
|  |         ./grafana_profile.py | ||||||
|  |             --grafana_token  | ||||||
|  |             --''') | ||||||
|  |     required = parser.add_argument_group('required arguments') | ||||||
|  |     required.add_argument('--grafana_token', help='token to access your Grafana database', required=True) | ||||||
|  |  | ||||||
|  |     optional = parser.add_argument_group('optional arguments') | ||||||
|  |     optional.add_argument('--dashboard_name', help='name of dashboard to create', default=None) | ||||||
|  |     optional.add_argument('--dashboard_uid', help='UID of dashboard to modify', default=None) | ||||||
|  |     optional.add_argument('--delete_dashboard', | ||||||
|  |                           help='Call this flag to delete the dashboard defined by UID', | ||||||
|  |                           default=None) | ||||||
|  |     optional.add_argument('--grafana_port', help='Grafana port if different from 3000', default=3000) | ||||||
|  |     optional.add_argument('--grafana_host', help='Grafana host', default='localhost') | ||||||
|  |     optional.add_argument('--list_dashboards', help='List dashboards on Grafana server', default=None) | ||||||
|  |     args = parser.parse_args() | ||||||
|  |  | ||||||
|  |     Grafana = UseGrafana(args.grafana_token, | ||||||
|  |                          args.grafana_port, | ||||||
|  |                          args.grafana_host | ||||||
|  |                          ) | ||||||
|  |     if args.dashboard_name is not None: | ||||||
|  |         Grafana.create_dashboard(args.dashboard_name) | ||||||
|  |  | ||||||
|  |     if args.delete_dashboard is not None: | ||||||
|  |         Grafana.delete_dashboard(args.dashboard_uid) | ||||||
|  |  | ||||||
|  |     if args.list_dashboards is not None: | ||||||
|  |         Grafana.list_dashboards() | ||||||
|  |  | ||||||
|  |  | ||||||
|  | if __name__ == "__main__": | ||||||
|  |     main() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Matthew Stidham
					Matthew Stidham