coderKeed
coderKeed

Reputation: 21

create or update a panel in Grafana using Python

I have a dashboard named as "server-plots" and there is another dashboard named as "master-plots". panels under "master-plots" are most updated graphs and I want to add the new panels inside "master-plots" dashboard to "server-plots" as well, everything with Python code (not manually or using curl). I am able to programmatically take the backup of these plots using Grafana GET APIs ,as JSON. I want to find the new panels inside the "master-plots" dashboard JSON and add those into "server-plots" dashboard , all using Python. I am unable to find any API to do that. Any idea how can I achieve this?

Upvotes: 0

Views: 1658

Answers (1)

coderKeed
coderKeed

Reputation: 21

The way I achieved this was by taking the backup of the master-plots as JSON using:

/api/dashboards/uid/<uid of the dashboard>

Then comparing it with the one inside the server-plots (taken similarly), and then updating the server-plots JSON with the diff (basically replacing server-plots JSON with the master-plots JSON), and finally writing that to the following using the POST method:

/api/dashboards/db/

One thing to consider here: The new JSON which is being written into the server-plots should have different uid and overwrite=True:

--snip--
f = open(myjsonfile,)  # the updated JSON of server-plots
data = json.load(f)
data["dashboard"]["id"] = None
data["overwrite"] = True
data["folderId"] = 123 # this is the folder ID of the server-plots
data["dashboard"]["uid"] = <logic to generate randum alpha-num>
url = "https://mygrafanaurl.com/api/dashboards/db"
headers = {'Authorization': 'auth',   'Content-Type': 'application/json' }
response = requests.request("POST", url, headers=headers, data=json.dumps(data))

Upvotes: 1

Related Questions