In [41]:
import requests
import pandas as pd
import json
from IPython.display import Image
In [43]:
Image("http://api-dupi2.localshi.com/image/441246b6ace049a788202677b746d49f.png")
Out[43]:
In [44]:
Image("http://api-dupi2.localshi.com/image/f59d2286a32e4189b09a1865486ce4e6.png")
Out[44]:
In [2]:
s = requests.Session()
In [3]:
payload = {
    "user": "test",
    "password": "test",
}
r = s.post("http://10.7.0.61:3000/login", json=payload)
In [4]:
r.json()
Out[4]:
{'message': 'Logged in'}
In [5]:
s.cookies
Out[5]:
<RequestsCookieJar[Cookie(version=0, name='grafana_session', value='90236f10f4e736f4a2676091c96159c8', port=None, port_specified=False, domain='10.7.0.61', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=1651882809, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None, 'SameSite': 'Lax'}, rfc2109=False)]>
In [6]:
s.headers
Out[6]:
{'User-Agent': 'python-requests/2.25.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
In [7]:
#post http://10.7.0.61:3000/api/user/using/2
In [9]:
dash = json.loads(open("./dash.txt").read())
In [20]:
dash['version']
Out[20]:
1
In [27]:
dash['panels'][0]['title'] = 'lolo'
dash['version'] = 3
In [28]:
# post
s.headers.update({
    "x-grafana-org-id": '2'
})
In [29]:
payload = {
    "dashboard": dash,
    "folderId": 1,
    "message": "",
    "overwrite": False,
}
r = s.post("http://10.7.0.61:3000/api/dashboards/db/", json=payload)
In [30]:
r.json()
Out[30]:
{'id': 2,
 'slug': '1',
 'status': 'success',
 'uid': 'j1Mf2ZUnz',
 'url': '/d/j1Mf2ZUnz/1',
 'version': 4}
In [ ]:
 
In [ ]:
 

Dashboards

In [32]:
r = s.get("http://10.7.0.61:3000/api/dashboards/uid/j1Mf2ZUnz")
In [34]:
r.json()
Out[34]:
{'meta': {'type': 'db',
  'canSave': True,
  'canEdit': True,
  'canAdmin': False,
  'canStar': True,
  'slug': '1',
  'url': '/d/j1Mf2ZUnz/1',
  'expires': '0001-01-01T00:00:00Z',
  'created': '2022-04-07T00:21:39Z',
  'updated': '2022-04-07T00:26:13Z',
  'updatedBy': 'test',
  'createdBy': 'test',
  'version': 4,
  'hasAcl': False,
  'isFolder': False,
  'folderId': 1,
  'folderUid': 'dlT-hZU7z',
  'folderTitle': 'dev',
  'folderUrl': '/dashboards/f/dlT-hZU7z/dev',
  'provisioned': False,
  'provisionedExternalId': ''},
 'dashboard': {'annotations': {'list': [{'builtIn': 1,
     'datasource': '-- Grafana --',
     'enable': True,
     'hide': True,
     'iconColor': 'rgba(0, 211, 255, 1)',
     'name': 'Annotations & Alerts',
     'target': {'limit': 100,
      'matchAny': False,
      'tags': [],
      'type': 'dashboard'},
     'type': 'dashboard'}]},
  'editable': True,
  'fiscalYearStartMonth': 0,
  'graphTooltip': 0,
  'id': 2,
  'links': [],
  'liveNow': False,
  'panels': [{'fieldConfig': {'defaults': {'color': {'mode': 'palette-classic'},
      'custom': {'axisLabel': '',
       'axisPlacement': 'auto',
       'barAlignment': 0,
       'drawStyle': 'line',
       'fillOpacity': 0,
       'gradientMode': 'none',
       'hideFrom': {'legend': False, 'tooltip': False, 'viz': False},
       'lineInterpolation': 'linear',
       'lineWidth': 1,
       'pointSize': 5,
       'scaleDistribution': {'type': 'linear'},
       'showPoints': 'auto',
       'spanNulls': False,
       'stacking': {'group': 'A', 'mode': 'none'},
       'thresholdsStyle': {'mode': 'off'}},
      'mappings': [],
      'thresholds': {'mode': 'absolute',
       'steps': [{'color': 'green', 'value': None},
        {'color': 'red', 'value': 80}]}},
     'overrides': []},
    'gridPos': {'h': 9, 'w': 12, 'x': 0, 'y': 0},
    'id': 2,
    'options': {'legend': {'calcs': [],
      'displayMode': 'list',
      'placement': 'bottom'},
     'tooltip': {'mode': 'single', 'sort': 'none'}},
    'title': 'lolo',
    'type': 'timeseries'}],
  'schemaVersion': 35,
  'style': 'dark',
  'tags': [],
  'templating': {'list': []},
  'time': {'from': 'now-6h', 'to': 'now'},
  'timepicker': {},
  'timezone': '',
  'title': '1',
  'uid': 'j1Mf2ZUnz',
  'version': 4,
  'weekStart': ''}}

Create Dashboard in Folder

In [40]:
payload = {'dashboard': {'annotations': {'list': [{'builtIn': 1,
     'datasource': '-- Grafana --',
     'enable': True,
     'hide': True,
     'iconColor': 'rgba(0, 211, 255, 1)',
     'name': 'Annotations & Alerts',
     'target': {'limit': 100,
      'matchAny': False,
      'tags': [],
      'type': 'dashboard'},
     'type': 'dashboard'}]},
  'editable': True,
  'fiscalYearStartMonth': 0,
  'graphTooltip': 0,
  'links': [],
  'liveNow': False,
  'panels': [],
  'schemaVersion': 35,
  'style': 'dark',
  'tags': [],
  'templating': {'list': []},
  'time': {'from': 'now-6h', 'to': 'now'},
  'timepicker': {},
  'timezone': '',
  'title': 'New dashboard 2',
  'version': 0,
  'weekStart': '',
  'id': None,
  'uid': '',
  'hideControls': False},
 'message': '',
 'overwrite': False,
 'folderId': 1}
r = s.post("http://10.7.0.61:3000/api/dashboards/db/", json=payload)
r.json()
Out[40]:
{'id': 4,
 'slug': 'new-dashboard-2',
 'status': 'success',
 'uid': 'SUQGPGU7z',
 'url': '/d/SUQGPGU7z/new-dashboard-2',
 'version': 1}
In [ ]:
 

Search Folders

In [35]:
params = {
    "query": "",
    "starred": False,
    "skipRecent": True,
    "skipStarred": True,
    "folderIds": 0,
    "layout": "folders",
    "prevSort": None,
}
r = s.get("http://10.7.0.61:3000/api/search", params=params)
In [36]:
r.json()
Out[36]:
[{'id': 1,
  'uid': 'dlT-hZU7z',
  'title': 'dev',
  'uri': 'db/dev',
  'url': '/dashboards/f/dlT-hZU7z/dev',
  'slug': '',
  'type': 'dash-folder',
  'tags': [],
  'isStarred': False,
  'sortMeta': 0}]
In [45]:
Image("http://api-dupi2.localshi.com/image/5c940fe4727d4e4a85e996048e51b8da.png")
Out[45]:
In [37]:
r = s.get("http://10.7.0.61:3000/api/search?folderIds=1")
r.json()
Out[37]:
[{'id': 2,
  'uid': 'j1Mf2ZUnz',
  'title': '1',
  'uri': 'db/1',
  'url': '/d/j1Mf2ZUnz/1',
  'slug': '',
  'type': 'dash-db',
  'tags': [],
  'isStarred': False,
  'folderId': 1,
  'folderUid': 'dlT-hZU7z',
  'folderTitle': 'dev',
  'folderUrl': '/dashboards/f/dlT-hZU7z/dev',
  'sortMeta': 0}]
In [46]:
Image("http://api-dupi2.localshi.com/image/60a1bc234f444b29bea4a5a9a3bbc557.png")
Out[46]:
In [ ]: