Hi,
I want to save a snapshot of Grafana dashboard programmatically. I tried to get dashboard definition with /api/dashboards/uid/ and save it with /api/snapshots, but the saved snapshot contained no data. Please advise.
Hi,
I want to save a snapshot of Grafana dashboard programmatically. I tried to get dashboard definition with /api/dashboards/uid/ and save it with /api/snapshots, but the saved snapshot contained no data. Please advise.
This is not possible through the API as you need the snapshotData section which is filled through the UI. I wrote this selenium script to accomplish this.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
GRAFANA_URL = os.environ.get('GRAFANA_URL')
GRAFANA_USER = os.environ.get('GRAFANA_USER')
GRAFANA_PASS = os.environ.get('GRAFANA_PASS')
def is_element_present(by, selector):
try:
driver.find_element(by, selector)
return True
except:
return False
def safe_get_element(by, identifier):
while not is_element_present(by, identifier):
WebDriverWait(driver, 10).until(EC.presence_of_element_located((by, identifier)))
return driver.find_element(by, identifier)
driver = webdriver.Firefox()
driver.get(GRAFANA_URL)
# Login
login_form = safe_get_element(By.CLASS_NAME, "login-content-box").find_element(By.TAG_NAME, "form")
login_form.find_element(By.NAME, "user").send_keys(GRAFANA_USER)
login_form.find_element(By.NAME, "password").send_keys(GRAFANA_PASS)
login_form.submit()
# Click menu button
safe_get_element(By.ID, "mega-menu-toggle").click()
# Click on dashboards
safe_get_element(By.XPATH, '//a[@href="/dashboards"]').click()
# Select k6 dashboard
safe_get_element(By.XPATH, "//*[text()='k6 Test Results']").click()
# Click on Share button
safe_get_element(By.XPATH, "//button[contains(., 'Share')]").click()
# Click on Snapshot
safe_get_element(By.XPATH, "//button[contains(., 'Snapshot')]").click()
# Publish on Snapshot
safe_get_element(By.XPATH, "//button[contains(., 'Publish to snapshots.raintank.io')]").click()
# Publish on Snapshot
snapshot = safe_get_element(By.ID, "snapshot-url-input").get_attribute("value")
print(snapshot)
driver.quit()
I hope this helps.