funcs

grafanacode: utility functions

grafanacode.funcs.addJsonItem(jsondict, keylist, value)[source]

Add an element to a JSON, eventually create parent levels

Parameters:
  • jsondict (dict) – json where to add to (will be modified by function

  • keylist (list) – list with keys on subsequent levels

  • value (any) – value to add, can be bool, str, int, float, list, dict, obj, …

Returns:

nothing

grafanacode.funcs.cleanFilename(source)[source]

Remove invalid characters ‘ %:/,.[]<>*?’ from filename

Parameters:

source (str) – input name

Returns:

string – source converted to valid filename

grafanacode.funcs.cutJsonItem(jsondict, keylist, default='KEYERROR')[source]

Get an element from a JSON if it exists and delete it , otherwise None

Parameters:
  • jsondict (dict) – json source; the found value will be removed

  • keylist (list) – list with keys on subsequent levels

  • default (any) – KEYERROR raises exception, else default if not found

Returns:

any – found value or default value

grafanacode.funcs.deepMerge(destination, dict2add)[source]

Recursive merge the additional dictionary in the destination dictionary, overwrite if key exists.

Parameters:
  • destination (dict) – dictionary, will be modified

  • dict2add (dict) – dictionary to merge, also on sublevels.

Returns:

nothing

grafanacode.funcs.filterByProperty(inlist, propertyname, value)[source]

Filter from list by property; return a new list with the items where named property has requested value. E.g.

filterByProperty([{'propX': value_1, 'a': 1}, {'propY': value_2, 'a': 2}, {'propX': value_1, 'a': 3}, {'propX': value_2, 'a': 4}], 'propX', value_1}

returns: [{'propX': value_1, 'a': 1}, {'propX': value_1, 'a': 3}]
Parameters:
  • inlist (list) – source list

  • propertyname (str) – property name

  • value (any) – value to compare with

Returns:

string – dict key or None

grafanacode.funcs.generateUID(alphabet=None, size=None)[source]

Get an UID; credits: https://github.com/puyuan/py-nanoid

Parameters:
  • alphabet (string) – a string of characters allowed in the UID, defaults to ‘_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’

  • size (int) – the length of the UIT to generate, defaults to 9

Returns:

string – generated UID

grafanacode.funcs.getJsonItem(jsondict, keylist, default='KEYERROR')[source]

Get an element from a JSON if it exists, otherwise None

Parameters:
  • jsondict (dict) – json source

  • keylist (list) – list with keys on subsequent levels

  • default (any) – KEYERROR raises exception, else default if not found

Returns:

any – found value or default value

grafanacode.funcs.getJsonKey(jsondict)[source]

Get the key from a JSON dict if it exists, otherwise None

Parameters:

jsondict (dict) – json source

Returns:

string – dict key or None

grafanacode.funcs.getPlugins()[source]

Get all plugins available in the /plugins map

Parameters:

nothing

Returns:

dict – dict with all available plugins

grafanacode.funcs.getPluginsExtract()[source]

Get all extract functions from the plugins available in the /plugins map

Parameters:

nothing

Returns:

dict – dict with all available extract functions

grafanacode.funcs.printPlugins()[source]

Get and print all plugins available in the /plugins map

grafanacode.funcs.printPluginsExtract()[source]

Get and print all extract functions from the plugins available in the /plugins map

funcs_api

grafanacode: utility functions for interacting with a grafana installation

grafanacode.funcs_api.getAllDashboards(server, user, pwd, verify=False)[source]

Get all dashboards from a Grafana installation

Parameters:
  • server (str) – server name (e.g. xxx.xxx.xxx.xxx:3000)

  • user (str) – user name

  • pwd (str) – password

  • verify (bool) – verify

Returns:

dict – all dashboards

grafanacode.funcs_api.getAllDatasources(server, user, pwd, verify=False)[source]

Get all datasources from a Grafana installation

Parameters:
  • server (str) – server name (e.g. xxx.xxx.xxx.xxx:3000)

  • user (str) – user name

  • pwd (str) – password

  • verify (bool) – verify

Returns:

dict – all datasources

grafanacode.funcs_api.getDashboardFomGrafana(uid, server, user, pwd, verify=False)[source]

Download a specific dashboard from a Grafana installation

Parameters:
  • uid (str) – dashboard uid

  • server (str) – server name (e.g. xxx.xxx.xxx.xxx:3000)

  • user (str) – user name

  • pwd (str) – password

  • verify (bool) – verify

Returns:

dict – dashboard json

grafanacode.funcs_api.getDatasourceFromGrafana(uid, server, user, pwd, verify=False)[source]

Download a specific datasource from a Grafana installation

Parameters:
  • uid (str) – datasource uid

  • server (str) – server name (e.g. xxx.xxx.xxx.xxx:3000)

  • user (str) – user name

  • pwd (str) – password

  • verify (bool) – verify

Returns:

dict – datasource json

grafanacode.funcs_api.getGrafanaJson(datain)[source]

Get then json dictionary from a Grafana dashboard; correct the boolean and null representation to Python

Parameters:

datain (dict) – dict input json

Returns:

dict – json corrected for Python: false->False, true->True, null->None

grafanacode.funcs_api.uploadDashboardToGrafana(jsondict, server, user, pwd, verify=False)[source]

Upload a dashboard to a Grafana installation

Parameters:
  • json (dict) – dashboard json

  • server (str) – server name (e.g. xxx.xxx.xxx.xxx:3000)

  • user (str) – user name

  • pwd (str) – password

  • verify (bool) – verify

Returns:

nothing