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.

cmd.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 argparse
  15. import logging
  16. import os
  17. import sys
  18. from grafana_dashboards.builder import Builder
  19. from grafana_dashboards.config import Config
  20. from grafana_dashboards import version
  21. LOG = logging.getLogger(__name__)
  22. class Client(object):
  23. def delete(self):
  24. LOG.info('Deleting dashboards in %s', self.args.path)
  25. builder = Builder(self.config)
  26. builder.delete_dashboard(self.args.path)
  27. def main(self):
  28. self.parse_arguments()
  29. self.read_config()
  30. self.setup_logging()
  31. self.args.func()
  32. def parse_arguments(self):
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument(
  35. '--config-file', dest='config', help='Path to a config file to '
  36. 'use. The default file used is: /etc/grafyaml/grafyaml.conf')
  37. parser.add_argument(
  38. '--debug', dest='debug', action='store_true',
  39. help='Print debugging output (set logging level to DEBUG instead '
  40. ' of default INFO level)')
  41. parser.add_argument(
  42. '--version', dest='version', action='version',
  43. version=version.version_info.release_string(), help="show "
  44. "program's version number and exit")
  45. subparsers = parser.add_subparsers(
  46. title='commands')
  47. parser_delete = subparsers.add_parser('delete')
  48. parser_delete.add_argument(
  49. 'path', help='colon-separated list of paths to YAML files or'
  50. ' directories')
  51. parser_delete.set_defaults(func=self.delete)
  52. parser_update = subparsers.add_parser('update')
  53. parser_update.add_argument(
  54. 'path', help='colon-separated list of paths to YAML files or'
  55. ' directories')
  56. parser_update.set_defaults(func=self.update)
  57. parser_validate = subparsers.add_parser('validate')
  58. parser_validate.add_argument(
  59. 'path', help='colon-separated list of paths to YAML files or'
  60. ' directories')
  61. parser_validate.set_defaults(func=self.validate)
  62. self.args = parser.parse_args()
  63. def read_config(self):
  64. self.config = Config()
  65. if self.args.config:
  66. fp = self.args.config
  67. else:
  68. fp = '/etc/grafyaml/grafyaml.conf'
  69. self.config.read(os.path.expanduser(fp))
  70. def setup_logging(self):
  71. if self.args.debug:
  72. logging.basicConfig(level=logging.DEBUG)
  73. else:
  74. logging.basicConfig(level=logging.INFO)
  75. def update(self):
  76. LOG.info('Updating dashboards in %s', self.args.path)
  77. builder = Builder(self.config)
  78. builder.update_dashboard(self.args.path)
  79. def validate(self):
  80. LOG.info('Validating dashboards in %s', self.args.path)
  81. # NOTE(pabelanger): Disable caching support by default, in an effort
  82. # to improve performance.
  83. self.config.set('cache', 'enabled', 'false')
  84. builder = Builder(self.config)
  85. try:
  86. builder.load_files(self.args.path)
  87. print('SUCCESS!')
  88. except Exception as e:
  89. print('%s: ERROR: %s' % (self.args.path, e))
  90. sys.exit(1)
  91. def main():
  92. client = Client()
  93. client.main()
  94. sys.exit(0)