"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/devstack/files/grafana/grafana-init.py" (27 Sep 2019, 5651 Bytes) of package /linux/misc/openstack/monasca-api-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "grafana-init.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_4.0.0.

    1 #!/usr/bin/env python
    2 # coding=utf-8
    3 
    4 # (C) Copyright 2017 Hewlett Packard Enterprise Development LP
    5 #
    6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    7 # not use this file except in compliance with the License. You may obtain
    8 # a copy of the License at
    9 #
   10 # http://www.apache.org/licenses/LICENSE-2.0
   11 #
   12 # Unless required by applicable law or agreed to in writing, software
   13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   15 # License for the specific language governing permissions and limitations
   16 # under the License.
   17 
   18 import glob
   19 import json
   20 import logging
   21 import os
   22 import sys
   23 import time
   24 
   25 from requests import RequestException
   26 from requests import Session
   27 
   28 LOG_LEVEL = logging.getLevelName(os.environ.get('LOG_LEVEL', 'INFO'))
   29 logging.basicConfig(level=LOG_LEVEL)
   30 
   31 logger = logging.getLogger(__name__)
   32 
   33 GRAFANA_URL = os.environ.get('GRAFANA_URL', 'http://localhost:3000')
   34 GRAFANA_USERNAME = os.environ.get('GRAFANA_USERNAME', 'mini-mon')
   35 GRAFANA_PASSWORD = os.environ.get('GRAFANA_PASSWORD', 'password')
   36 GRAFANA_USERS = [{'user': GRAFANA_USERNAME, 'password': GRAFANA_PASSWORD, 'email': ''}]
   37 
   38 DATASOURCE_NAME = os.environ.get('DATASOURCE_NAME', 'monasca')
   39 DATASOURCE_URL = os.environ.get('DATASOURCE_URL', 'http://localhost/metrics')
   40 DATASOURCE_ACCESS_MODE = os.environ.get('DATASOURCE_ACCESS_MODE', 'proxy')
   41 DATASOURCE_AUTH = os.environ.get('DATASOURCE_AUTH', 'Keystone').capitalize()
   42 DATASOURCE_AUTH_TOKEN = os.environ.get('DATASOURCE_AUTH_TOKEN', '')
   43 
   44 DASHBOARDS_DIR = os.environ.get('DASHBOARDS_DIR', '/dashboards.d')
   45 
   46 
   47 def retry(retries=5, delay=2.0, exc_types=(RequestException,)):
   48     def decorator(func):
   49         def f_retry(*args, **kwargs):
   50             for i in range(retries):
   51                 try:
   52                     return func(*args, **kwargs)
   53                 except exc_types as exc:
   54                     if i < retries - 1:
   55                         logger.debug('Caught exception, retrying...',
   56                                      exc_info=True)
   57                         time.sleep(delay)
   58                     else:
   59                         logger.exception('Failed after %d attempts', retries)
   60                         if isinstance(exc, RequestException):
   61                             logger.debug('Response was: %r', exc.response.text)
   62 
   63                         raise
   64         return f_retry
   65     return decorator
   66 
   67 
   68 def create_login_payload():
   69     if os.environ.get('GRAFANA_USERS'):
   70         try:
   71             json.loads(os.environ.get('GRAFANA_USERS'))
   72         except ValueError:
   73             print("Invalid type GRAFANA_USERS")
   74             raise
   75         grafana_users = json.loads(os.environ.get('GRAFANA_USERS'))
   76     else:
   77         grafana_users = GRAFANA_USERS
   78     return grafana_users
   79 
   80 
   81 @retry(retries=24, delay=5.0)
   82 def login(session, user):
   83     r = session.post('{url}/login'.format(url=GRAFANA_URL),
   84                      json=user,
   85                      timeout=5)
   86     r.raise_for_status()
   87 
   88 
   89 @retry(retries=12, delay=5.0)
   90 def check_initialized(session):
   91     r = session.get('{url}/api/datasources'.format(url=GRAFANA_URL), timeout=5)
   92     r.raise_for_status()
   93 
   94     logging.debug('existing datasources = %r', r.json())
   95 
   96     for datasource in r.json():
   97         if datasource['name'] == DATASOURCE_NAME:
   98             return True
   99 
  100     return False
  101 
  102 
  103 def create_datasource_payload():
  104     payload = {
  105         'name': DATASOURCE_NAME,
  106         'url': DATASOURCE_URL,
  107         'access': DATASOURCE_ACCESS_MODE,
  108         'isDefault': True,
  109     }
  110 
  111     if DATASOURCE_AUTH not in ['Keystone', 'Horizon', 'Token']:
  112         logger.error('Unknown Keystone authentication option: %s',
  113                      DATASOURCE_AUTH)
  114         sys.exit(1)
  115 
  116     keystone_auth = False
  117     if DATASOURCE_AUTH in ['Keystone']:
  118         keystone_auth = True
  119 
  120     payload.update({
  121         'monasca': {
  122             'type': 'monasca-datasource',
  123             'jsonData': {
  124                 'authMode': DATASOURCE_AUTH,
  125                 'keystoneAuth': keystone_auth,
  126                 'token': DATASOURCE_AUTH_TOKEN,
  127             }
  128         }
  129     }.get(DATASOURCE_NAME, {}))
  130 
  131     logging.debug('payload = %r', payload)
  132 
  133     return payload
  134 
  135 
  136 def create_dashboard_payload(json_path):
  137     with open(json_path, 'r') as f:
  138         dashboard = json.load(f)
  139         dashboard['id'] = None
  140 
  141         return {
  142             'dashboard': dashboard,
  143             'overwrite': False
  144         }
  145 
  146 
  147 def main():
  148     for user in create_login_payload():
  149         logging.info('Opening a Grafana session...')
  150         session = Session()
  151         login(session, user)
  152 
  153         if check_initialized(session):
  154             logging.info('Grafana has already been initialized, skipping!')
  155             return
  156 
  157         logging.info('Attempting to add configured datasource...')
  158         r = session.post('{url}/api/datasources'.format(url=GRAFANA_URL),
  159                          json=create_datasource_payload())
  160         logging.debug('Response: %r', r.json())
  161         r.raise_for_status()
  162 
  163         for path in sorted(glob.glob('{dir}/*.json'.format(dir=DASHBOARDS_DIR))):
  164             logging.info('Creating dashboard from file: {path}'.format(path=path))
  165             r = session.post('{url}/api/dashboards/db'.format(url=GRAFANA_URL),
  166                              json=create_dashboard_payload(path))
  167             logging.debug('Response: %r', r.json())
  168             r.raise_for_status()
  169 
  170         logging.info('Ending %r session...', user.get('user'))
  171         session.get('{url}/logout'.format(url=GRAFANA_URL))
  172 
  173     logging.info('Finished successfully.')
  174 
  175 
  176 if __name__ == '__main__':
  177     main()