简体   繁体   中英

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?

The way I achieved this one was, taking the backup of the "master-plots" as JSON using '/api/dashboards/uid/<uid of the dashboard' and then compare it with the one inside the "server-plots" (taken similarly) and then update the server-plots json with the diff (basically replace server-plots json with master-plots json) and then write that '/api/dashboards/db/' (POST method). 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM