-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafun-cli.py
227 lines (177 loc) · 8.22 KB
/
grafun-cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
########################################################################################################################
# DO NOT USE!!! UNDER DEVELOPMENT!!!
#
# name: grafun_cli
# usage:
# usage: grafun-cli.py [-h] [--server SERVER] [--config-file CONFIG_FILE] [--api-token API_TOKEN] [--dashboard-name DASHBOARD_NAME]
#
# Grafana Dashboard Configuration
#
# options:
# -h, --help show this help message and exit
# --server SERVER Grafana server URL, if none is specified use from config file
# --config-file CONFIG_FILE
# Path to custom configuration file
# --api-token API_TOKEN
# Grafana API token, if none is specified use from config file
# --dashboard-name DASHBOARD_NAME
# Name of the Grafana dashboard, if none is specified use automated name
#
#
#
########################################################################################################################
VERSION = 0.6
import yaml
import json, requests, logging
import functions_core.yaml_validate
import argparse
from functions_core.grafana_fun import *
from functions_core.grafanafun_dm import *
def new_create_system_dashboard(sys, dash_name=None):
panels = []
templating = []
y_pos = 3
if dash_name is None:
dash_name = f"System {sys['system']} dashboard"
else:
dash_name = dash_name + f" {sys['system']}"
panels = panels + create_title_panel(str(sys['system']))
for res in sys['resources']:
match res['name']:
case "linux_os":
y_pos, res_panel = graph_linux_os(str(sys['system']), str(res['name']), res['data'], y_pos)
panels = panels + res_panel
case "eternus_cs8000":
y_pos, res_panel = graph_eternus_cs8000(str(sys['system']), str(res['name']), res['data'], y_pos)
templating = create_dashboard_vars(res['data'])
panels = panels + res_panel
case "powerstore":
y_pos, res_panel = graph_powerstore(str(sys['system']), str(res['name']), res['data'], y_pos)
panels = panels + res_panel
case "eternus_dx":
y_pos, res_panel = graph_eternus_dx(str(sys['system']), str(res['name']), res['data'], y_pos)
templating = graph_eternus_dx_dashboard_vars(res['data'])
panels = panels + res_panel
my_dashboard = Dashboard(
title=dash_name,
description="fjcollector auto generated dashboard",
tags=[
sys['system'],
],
timezone="browser",
refresh="1m",
panels=panels,
templating=Templating(templating),
).auto_panel_ids()
return my_dashboard
def local_upload_to_grafana(json_data, server, api_key, verify=False):
'''
upload_to_grafana tries to upload dashboard to grafana and prints response
:param json_data - dashboard json generated by grafanalib
:param server - grafana server name
:param api_key - grafana api key with read and write privileges
'''
headers = {'Authorization': f"Bearer {api_key}", 'Content-Type': 'application/json'}
try:
r = requests.post(f"http://{server}/api/dashboards/db", data=json_data, headers=headers, verify=verify)
logging.debug("Message from grafana_fun is %s" % r.json())
except Exception as msgerror:
logging.error("Failed to create report in grafana %s with error %s" % (server, msgerror))
return None
return r
def new_build_dashboards(config, grafana_url=None, grafana_token=None, dash_name=None):
"""
Creates a Grafana dashboard using the provided parameters.
Args:
config (object, optional): The configuration file object (default is None).
grafana_url (str, optional): The Grafana server URL (default is "localhost:3000").
dash_name (str, optional): The name of the dashboard (default is "Default Dashboard").
grafana_token (str, optional): The JWT token for authentication (default is None).
Returns:
str: A message indicating the successful creation of the dashboard.
"""
# Your implementation here
# ...
if grafana_url is None:
grafana_url = config.global_parameters.grafana_server
if grafana_token is None:
grafana_token = config.global_parameters.grafana_api_key
systems = data_model_build(config)
# Create dashboards for each system
for sys_item in systems:
print(f"Creating Dashboard for system: {sys_item['system']}")
my_dashboard = new_create_system_dashboard(sys_item, dash_name)
my_dashboard_json = get_dashboard_json(my_dashboard, overwrite=True, message="Updated by grafun-cli")
logging.debug("Created dashboard %s", my_dashboard_json)
res = local_upload_to_grafana(my_dashboard_json, grafana_url, grafana_token)
if res is not None:
if res.status_code == 200:
print(f"Dashboard for system {sys_item['system']} created ({res.status_code} {res.reason})")
else:
print(f"Unable to create dashboard for system {sys_item['system']} error is: {res.status_code} {res.reason}")
else:
print(f"Unexpected error occurred!!!")
# Create Main Landing page
hosts_per_res = data_model_get_hosts_per_resource_grouped(systems)
my_dashboard = create_main_observit_dashboard(hosts_per_res)
my_dashboard_json = get_dashboard_json(my_dashboard, overwrite=True, message="Updated by grafun-cli")
res = local_upload_to_grafana(my_dashboard_json, grafana_url, grafana_token)
if res is not None:
if res.status_code == 200:
print(f"Main observIT dashboard created ({res.status_code} {res.reason})")
else:
print(f"Unable to create Main observIT dashboard error is: {res.status_code} {res.reason}")
else:
print(f"Unexpected error occurred!!!")
return 1
# return f"{grafana_url} Dashboard '{dash_name}' created successfully!"
def parse_args():
parser = argparse.ArgumentParser(description="Grafana Dashboard Configuration")
parser.add_argument("--server", default=None, help="Grafana server URL, if none is specified use from config file")
parser.add_argument("--config-file", default="config/config.yaml", help="Path to custom configuration file")
parser.add_argument("--api-token", default=None,
help="Grafana API token, if none is specified use from config file")
parser.add_argument("--dashboard-name", default=None, help="Name of the Grafana dashboard, if none is "
"specified use automated name")
return parser.parse_args()
def main():
args = parse_args()
print(f"Grafana command line dashboard creator v{VERSION}")
print(f"Type -h for help.")
print()
print(f"Parameters:")
if args.config_file != "config/config.yaml":
print(f" Config File: {args.config_file}")
if args.server is not None:
print(f" Grafana Server: {args.server}")
if args.api_token is not None:
print(f" API Token: {args.api_token}")
print()
# Read from config file
print(f"Reading from config File: {args.config_file}")
# Read configration file
config, orig_mtime, configfile_running = configfile_read(args.config_file)
# Check if consistent
result_dicts, global_parms = create_metric_ip_dicts(config)
# Get Grafana server from config file
# if args.server is None:
# args.server = config.global_parameters.grafana_server
# print(f" Grafana Server from config file: {args.server}")
# Get Grafana API Token
# if args.api_token is None:
# args.api_token = config.global_parameters.grafana_api_key
# print(f" API Token from config file: {args.api_token}")
# Check Data Model
print(f"Building Data Model...")
systems = data_model_build(config)
print(f"Data model is {systems}")
i = 0
str_sysname = ""
for sys in systems:
i += 1
str_sysname += f"{sys['system']} "
print(f"Found {i} system(s): {str_sysname}")
new_build_dashboards(config, args.server, args.api_token, args.dashboard_name)
# new_build_dashboards(config)
if __name__ == "__main__":
main()