Skip to content

Commit abba40c

Browse files
author
Óscar Nájera
committed
Update grafana dashboards over grafana.db
Query the CMK instance http
1 parent c844244 commit abba40c

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

utils/converter.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env python3
22

3+
from datetime import datetime
34
import argparse
45
import json
6+
import pprint
57
import sqlite3
68
import urllib.request as rq
79

@@ -16,11 +18,6 @@ def cli_options():
1618
parser.add_argument(
1719
"-n", "--datasource-new", help="Name of the NEW Grafana Connector Version 2.x"
1820
)
19-
parser.add_argument(
20-
"-t",
21-
"--new-dashboard-title",
22-
help="Rename dashboard. If not set add NEW suffix",
23-
)
2421
return parser
2522

2623

@@ -133,21 +130,8 @@ def _query(context):
133130
return _query
134131

135132

136-
def main():
137-
args = cli_options().parse_args()
138-
con = sqlite3.connect(args.db_file)
139-
cur = con.cursor()
140-
datasource_config = dict(get_datasource_configs(cur))
141-
query_graph = query(datasource_config[args.datasource_old])
142-
143-
row = list(cur.execute("select data from dashboard where slug = 'cmk'"))
144-
dash = json.loads(row[0][0])
145-
146-
# # if title := args.new_dashboard_title:
147-
# # dash["title"] = title
148-
# # else:
149-
dash["title"] += " NEW"
150-
dash["uid"] += "1"
133+
def update_dashboard(data, args, query_graph):
134+
dash = json.loads(data)
151135

152136
for panel in dash["panels"]:
153137
if panel.get("datasource") == args.datasource_old:
@@ -157,11 +141,41 @@ def main():
157141
update_context(target)
158142
update_graph(query_graph, target)
159143

160-
print(json.dumps(dash))
161-
cur.execute(
162-
"update dashboard set data = '%s' where slug = 'cmk-new'" % json.dumps(dash)
163-
)
164-
con.commit()
144+
return dash
145+
146+
147+
def main():
148+
args = cli_options().parse_args()
149+
con = sqlite3.connect(args.db_file)
150+
cur = con.cursor()
151+
datasource_config = dict(get_datasource_configs(cur))
152+
query_graph = query(datasource_config[args.datasource_old])
153+
154+
for did, data, creator in cur.execute("select id, data, created_by from dashboard"):
155+
dash = update_dashboard(data, args, query_graph)
156+
version = dash["version"]
157+
dash["version"] += 1
158+
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
159+
cur.execute(
160+
"insert into dashboard_version (dashboard_id, parent_version, restored_from, version, created, created_by, message, data) values(?,?,?,?,?,?,?,?)",
161+
(
162+
did,
163+
version,
164+
0,
165+
dash["version"],
166+
now,
167+
creator,
168+
"Checkmk connector update",
169+
json.dumps(dash),
170+
),
171+
)
172+
cur.execute(
173+
"""UPDATE dashboard SET
174+
data = ?, version = ?, updated = ?
175+
WHERE id = ?""",
176+
(json.dumps(dash), dash["version"], now, did),
177+
)
178+
con.commit()
165179
con.close()
166180

167181

0 commit comments

Comments
 (0)