def _authenticate(self): """Authenticate and renew the authentication token""" # if auth_token is available, just return it if self._auth_token is not None: return self._auth_token # aquire the authentication lock with self._auth_lock: # re-check the auth_token as another thread could set it if self._auth_token is not None: return self._auth_token LOGGER.debug('Authenticating request') # pylint: disable=broad-except try: # create a keystone client if it doesn't exist if self._keystone is None: cfg = Config.instance() self._keystone = ClientV3( auth_url=cfg.OS_AUTH_URL, username=cfg.OS_USERNAME, password=cfg.OS_PASSWORD, tenant_name=cfg.OS_TENANT_NAME ) # store the authentication token self._auth_token = self._keystone.auth_token # get the uri of service endpoint endpoint = self._keystone.get_service_endpoint( "ceilometer", Config.instance().CEILOMETER_URL_TYPE) self._url_base = "{}/v2/meters/%s".format(endpoint) LOGGER.info('Authenticating request - success') self._failed_auth = False except KeystoneException as exc: log_level = logging.DEBUG if not self._failed_auth: log_level = logging.ERROR LOGGER.error( 'Suspending error logs until successful auth' ) LOGGER.log(log_level, 'Authentication error: %s', six.text_type(exc), exc_info=0) if exc.response: LOGGER.debug('Response: %s', exc.response) self._auth_token = None self._failed_auth = True return self._auth_token
def _on_authenticated(self): endpoint = self._keystone.get_service_endpoint( "ceilometer", Config.instance().CEILOMETER_URL_TYPE) self._url_base = "{}/v2/meters/%s".format(endpoint) pass
def _perform_update_request(cls, url, auth_token, payload): """Perform the PUT/update request.""" LOGGER.debug('Performing request to %s', url) # request headers headers = { 'X-Auth-Token': auth_token, 'Content-type': 'application/json' } # perform request and return its result response = None try: LOGGER.debug( "Performing request to: %s with data=%s and headers=%s", url, payload, headers) response = requests.put( url, data=payload, headers=headers, timeout=(Config.instance().CEILOMETER_TIMEOUT / 1000.)) LOGGER.info('Response: %s: %s', response.status_code, response.text) except RequestException as exc: LOGGER.error('aodh request error: %s', six.text_type(exc)) finally: if response is not None: LOGGER.debug( 'Returning response from _perform_update_request(): %s', response.status_code) return response
def _perform_request(url, payload, auth_token): """Perform the POST request""" LOGGER.debug('Performing request to %s', url) # request headers headers = {'X-Auth-Token': auth_token, 'Content-type': 'application/json'} # perform request and return its result response = requests.post( url, data=payload, headers=headers, timeout=(Config.instance().CEILOMETER_TIMEOUT / 1000.)) # Raises exception if there was an error try: response.raise_for_status() # pylint: disable=broad-except except Exception: exc_info = 1 raise else: exc_info = 0 finally: # Log out the result of the request for debugging purpose LOGGER.debug( 'Result: %s, %d, %r', get_status_name(response.status_code), response.status_code, response.text, exc_info=exc_info) return response
def hostname(self, vl): """Get hostname based on the input""" hostname = self._vms.get(vl.host) if not hostname: with self._cache_lock: # check again with lock because another thread could # store the hostname meanwhile hostname = self._vms.get(vl.host) if not hostname: if self._conn is None: self._conn = libvirt.openReadOnly( Config.instance().LIBVIRT_CONN_URI) hostname = self._conn.lookupByName(vl.host).UUIDString() self._vms[vl.host] = hostname return hostname
def register_plugin(collectd): """Bind plugin hooks to collectd and viceversa.""" config = Config.instance() # Setup loggging log_handler = CollectdLogHandler(collectd=collectd) log_handler.cfg = config ROOT_LOGGER.addHandler(log_handler) ROOT_LOGGER.setLevel(logging.NOTSET) # Creates collectd plugin instance instance = Plugin(collectd=collectd, config=config) # Register plugin callbacks collectd.register_config(instance.config) collectd.register_shutdown(instance.shutdown) collectd.register_notification(instance.notify)
def register_plugin(collectd): "Bind plugin hooks to collectd and viceversa" config = Config.instance() # Setup loggging log_handler = CollectdLogHandler(collectd=collectd) log_handler.cfg = config ROOT_LOGGER.addHandler(log_handler) ROOT_LOGGER.setLevel(logging.NOTSET) # Creates collectd plugin instance instance = Plugin(collectd=collectd, config=config) # Register plugin callbacks collectd.register_config(instance.config) collectd.register_write(instance.write) collectd.register_shutdown(instance.shutdown)
def _get_endpoint(self, service): # get the uri of service endpoint endpoint = self._keystone.get_service_endpoint( service, Config.instance().CEILOMETER_URL_TYPE) return endpoint
def unit(self, vl): """Get meter unit""" # pylint: disable=no-self-use return Config.instance().unit(vl.plugin, vl.type)