"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-4.0.0/devstack/files/grafana/grafana-init.py" (13 May 2020, 5629 Bytes) of package /linux/misc/openstack/monasca-api-4.0.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 latest Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_4.0.0.

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