Tools to make Grafana dashboards from templates
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

builder.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2015 Red Hat, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import logging
  15. import os
  16. from grafana_dashboards.cache import Cache
  17. from grafana_dashboards.grafana import Grafana
  18. from grafana_dashboards.parser import YamlParser
  19. LOG = logging.getLogger(__name__)
  20. class Builder(object):
  21. def __init__(self, config):
  22. self.cache = Cache(
  23. config.get('cache', 'cachedir'),
  24. config.getboolean('cache', 'enabled'))
  25. self.grafana = Grafana(
  26. url=config.get('grafana', 'url'),
  27. key=config.get('grafana', 'apikey'))
  28. self.parser = YamlParser()
  29. def delete_dashboard(self, path):
  30. self.load_files(path)
  31. dashboards = self.parser.data.get('dashboard', {})
  32. for name in dashboards:
  33. LOG.debug('Deleting grafana dashboard %s', name)
  34. self.grafana.dashboard.delete(name)
  35. self.cache.set(name, '')
  36. def load_files(self, path):
  37. files_to_process = []
  38. if os.path.isdir(path):
  39. files_to_process.extend([os.path.join(path, f)
  40. for f in os.listdir(path)
  41. if (f.endswith('.yaml')
  42. or f.endswith('.yml'))])
  43. else:
  44. files_to_process.append(path)
  45. for fn in files_to_process:
  46. self.parser.parse(fn)
  47. def update_dashboard(self, path):
  48. self.load_files(path)
  49. dashboards = self.parser.data.get('dashboard', {})
  50. LOG.info('Number of dashboards generated: %d', len(dashboards))
  51. for name in dashboards:
  52. data, md5 = self.parser.get_dashboard(name)
  53. if self.cache.has_changed(name, md5):
  54. self.grafana.dashboard.create(name, data, overwrite=True)
  55. self.cache.set(name, md5)
  56. else:
  57. LOG.debug("'%s' has not changed" % name)