Exemple #1
0
    def monasca(self):
        if self._monasca:
            return self._monasca

        monascaclient_version = self._get_client_option(
            'monasca', 'api_version')
        monascaclient_interface = self._get_client_option(
            'monasca', 'interface')
        token = self.session.get_token()
        watcher_clients_auth_config = CONF.get(_CLIENTS_AUTH_GROUP)
        service_type = 'monitoring'
        monasca_kwargs = {
            'auth_url': watcher_clients_auth_config.auth_url,
            'cert_file': watcher_clients_auth_config.certfile,
            'insecure': watcher_clients_auth_config.insecure,
            'key_file': watcher_clients_auth_config.keyfile,
            'keystone_timeout': watcher_clients_auth_config.timeout,
            'os_cacert': watcher_clients_auth_config.cafile,
            'service_type': service_type,
            'token': token,
            'username': watcher_clients_auth_config.username,
            'password': watcher_clients_auth_config.password,
        }
        endpoint = self.session.get_endpoint(service_type=service_type,
                                             interface=monascaclient_interface)

        self._monasca = monclient.Client(monascaclient_version, endpoint,
                                         **monasca_kwargs)

        return self._monasca
Exemple #2
0
 def __init__(self, module):
     self.module = module
     self.auth_kwargs = self._get_auth_credentials()
     self.monasca = client.Client(
         self.module.params['api_version'],
         **self.auth_kwargs
     )
Exemple #3
0
    def test_client_no_verify_params_for_session(self, mock_session):
        (
            version,
            token,
            project_id,
            user_domain_id,
            project_domain_id,
            verify,
            auth_url,
            endpoint
        ) = _mock_client_args(False)

        the_client = mon_client.Client(
            api_version=version,
            token=token,
            project_id=project_id,
            user_domain_id=user_domain_id,
            project_domain_id=project_domain_id,
            verify=verify,
            auth_url=auth_url,
            endpoint=endpoint
        )

        self.assertIsNotNone(the_client)
        mock_session.assert_called_with(_expected_session_args(False))
Exemple #4
0
    def run(self, data_file):
        try:
            with open(data_file) as f:
                yaml_text = f.read()
        except IOError:
            raise Exception(
                'Unable to open yaml alarm definitions: {}'.format(data_file))

        yaml_data = yaml.safe_load(yaml_text)

        self._keystone_auth()
        self._monasca = client.Client(self._args['api_version'],
                                      self._api_url,
                                      token=self._token)
        self._print_message('Using Monasca at {}'.format(self._api_url))
        if 'notifications' not in yaml_data:
            raise Exception('No notifications section in {}'.format(data_file))

        (processed, changed,
         notification_ids) = self._do_notifications(yaml_data['notifications'])
        self._print_message(
            '{:d} Notifications Processed {:d} Notifications Changed'.format(
                processed, changed))

        if 'alarm_definitions' not in yaml_data:
            raise Exception(
                'No alarm_definitions section in {}'.format(data_file))

        (processed,
         changed) = self.do_alarm_definitions(yaml_data['alarm_definitions'],
                                              notification_ids)
        self._print_message(
            '{:d} Alarm Definitions Processed {:d} Alarm Definitions Changed'.
            format(processed, changed))
Exemple #5
0
    def __init__(self, name='', args=None):
        super(MonascaDriver, self).__init__(name, args=args)
        datasource_driver.ExecutionDriver.__init__(self)
        self.creds = args
        if not self.creds.get('project_name'):
            self.creds['project_name'] = self.creds['tenant_name']

        if not self.creds.get('poll_time'):
            # set default polling time to 1hr
            self.creds['poll_time'] = 3600

        # Monasca uses Keystone V3
        self.creds['auth_url'] = self.creds['auth_url'].replace("v2.0", "v3")
        self.keystone = ksclient.Client(**self.creds)
        self.creds['token'] = self.keystone.auth_token

        if not self.creds.get('endpoint'):
            # if the endpoint not defined retrieved it from keystone catalog
            self.creds['endpoint'] = self.keystone.service_catalog.url_for(
                service_type='monitoring', endpoint_type='publicURL')

        self.monasca = monasca_client.Client('2_0', **self.creds)
        self.add_executable_client_methods(self.monasca, 'monascaclient.')
        self.initialize_update_methods()
        self._init_end_start_poll()
Exemple #6
0
def mon_client(username, password, auth_url, endpoint):
    kwargs = {'username': username, 'password': password, 'auth_url': auth_url}

    _ksclient = ksclient.KSClient(**kwargs)
    kwargs = {'token': _ksclient.token}
    api_version = '2_0'
    return client.Client(api_version, endpoint, **kwargs)
Exemple #7
0
    def test_should_not_warn_when_passing_no_args(self, _, __, ___):
        api_version = mock.Mock()

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            client.Client(api_version)
            self.assertEqual(0, len(w))
 def __init__(self, cloud_name, api_version, monasca_url, verbose, debug):
     self.connection = openstack.connect(cloud_name)
     self.session = self.connection.session
     self.monasca_client = monasca_client.Client(api_version=api_version,
                                                 endpoint=monasca_url,
                                                 session=self.session)
     self._setup_logging(debug)
def get_monasca_client(args, keystone_args):
    api_url = args.get('monasca_api_url')
    if not api_url:
        raise Exception('Error: monasca_api_url is required')

    monasca = client.Client(args['api_version'], api_url, **keystone_args)
    print('Using Monasca at {}'.format(api_url))

    for i in range(MONASCA_RETRIES):
        try:
            # just make an api call that should always succeed
            monasca.notifications.list(limit=1)
            print('Monasca API is ready')
            return monasca

        # I don't feel like spending an hour digging through osc-lib's
        # spaghetti to figure out what exceptions list() raises, so, yeah.
        except Exception as ex:
            print('Attempt {} of {} to {} failed: {}'.format(
                i, MONASCA_RETRIES, api_url, ex.message))

            if i < MONASCA_RETRIES - 1:
                time.sleep(MONASCA_DELAY)

    print('Could not connect to Monasca after {} retries, '
          'giving up!'.format(MONASCA_RETRIES))
    raise Exception('could not connect to monasca at {}', api_url)
Exemple #10
0
 def _create(self):
     interface = self._get_client_option(CLIENT_NAME, 'endpoint_type')
     endpoint = self.url_for(service_type=self.MONITORING,
                             endpoint_type=interface)
     return client.Client(self.VERSION,
                          session=self.context.keystone_session,
                          endpoint=endpoint)
def query_metrics_per_second():

    args = parse_args()

    try:
        ks_client = ksclient.KSClient(**keystone)
    except Exception as ex:
        print 'Failed to authenticate: {}'.format(ex)
        return

    mon_client = client.Client('2_0', args.monasca_api_url, token=ks_client.token)
    metrics_data = mon_client.metrics.list(name="metrics.published")

    metric_averages = {}
    for metric in metrics_data:
        measurements = mon_client.metrics.list_measurements(start_time=args.starttime, name="metrics.published", dimensions=metric['dimensions'])
        values = []
        for m in measurements[0]['measurements']:
            values.append(m[1])
        metric_averages[metric['dimensions']['hostname']] = (sum(values)/len(values))

    with open(args.output_directory + 'metrics_per_second', "w") as output_file:
        output_file.write("{}: {}\n".format("total", sum(metric_averages.values())))
        for k, v in metric_averages.items():
            output_file.write("{} : {}\n".format(k, v))
Exemple #12
0
 def __init__(self,
              proc_num,
              num_requests,
              num_metrics,
              api_url,
              keystone_dict,
              continuous=False,
              interval=60,
              queue=None,
              metric_creator=MetricCreatorSimple,
              token=None):
     self.proc_num = proc_num
     self.num_requests = num_requests
     self.num_metrics = num_metrics
     self.interval = interval
     self.continuous = continuous
     self.queue = queue
     if not token:
         try:
             token = ksclient.KSClient(**keystone_dict).token
         except Exception as ex:
             print("Agent {}: Failed to get auth token from keystone\n{}".
                   format(self.proc_num, keystone_dict))
     #print("Using token: " + token)
     self.mon_client = client.Client('2_0', api_url, session=token)
     self.metric_creator = metric_creator(proc_num)
Exemple #13
0
 def __init__(self, module):
     self.module = module
     self.api_url = self.module.params['monasca_api_url']
     auth_args = self._get_keystone_auth_args()
     auth_args["endpoint"] = self.api_url
     self.monasca = client.Client(self.module.params['api_version'],
                                  **auth_args)
     self.exit_data = {'monasca_api_url': self.api_url}
Exemple #14
0
 def _get_mon_client(self):
     k = keystone.Keystone(self._config)
     endpoint = k.get_monasca_url()
     session = k.get_session()
     c = client.Client(api_version=self._api_version,
                       endpoint=endpoint,
                       session=session,
                       timeout=self.write_timeout,
                       **keystone.get_args(self._config))
     return c
Exemple #15
0
 def __init__(self, module):
     self.module = module
     self._keystone_auth()
     self.exit_data = {
         'keystone_token': self.token,
         'monasca_api_url': self.api_url
     }
     self.monasca = client.Client(self.module.params['api_version'],
                                  self.api_url,
                                  token=self.token)
    def __init__(self, module):
        self.module = module

        if not HAS_MONASCACLIENT:
            self.module.fail_json(
                msg='Failed to import python-monascaclient or keystoneauth1')

        self.api_version = self.module.params['api_version']

        # Create a keystone session from a password
        if self.module.params['keystone_token'] is None:
            sess = self._keystone_session()

            if self.module.params['monasca_api_url'] is None:
                self.api_url = self._endpoint_discover(sess)
            else:
                self.api_url = self.module.params['monasca_api_url']

            self.exit_data = {'monasca_api_url': self.api_url}

            self.monasca = mon_client.Client(api_version=self.api_version,
                                             endpoint=self.api_url,
                                             session=sess)

        else:  # user has supplied a keystone token
            self.token = self.module.params['keystone_token']
            if self.module.params['monasca_api_url'] is None:
                self.module.fail_json(
                    msg=
                    'monasca_api_url param is required when using keystone_token'
                )

            self.api_url = self.module.params['monasca_api_url']

            self.exit_data = {'monasca_api_url': self.api_url}

            self.monasca = mon_client.Client(
                api_version=self.api_version,
                endpoint=self.api_url,
                token=self.token,
                project_name=self.module.params['keystone_project'],
                auth_url=self.module.params['keystone_url'],
                project_domain_id=self.module.params['project_domain_id'])
Exemple #17
0
def alarm_client():
    kwargs = {
        'username': "******",
        'password': "******",
        'project_name': "baz",
        'auth_url': auth_url
    }

    _ksclient = ksclient.KSClient(**kwargs)
    kwargs = {'token': _ksclient.token}
    api_version = '2_0'
    return client.Client(api_version, endpoint, **kwargs).alarms
Exemple #18
0
 def get_client(self):
     """get_client
         get a new mon-client object
     """
     token = self.keystone.refresh_token()
     # Re-create the client.  This is temporary until
     # the client is updated to be able to reset the
     # token.
     kwargs = {
         'token': token
     }
     return client.Client(self.api_version, self.url, **kwargs)
Exemple #19
0
    def create_client(self, version=None, service_type=None):
        """Return monasca client."""
        from monascaclient import client as monasca

        client = monasca.Client(
            self.choose_version(version),
            self._get_endpoint(service_type),
            token=self.keystone.auth_ref.auth_token,
            timeout=CONF.openstack_client_http_timeout,
            insecure=self.credential.insecure,
            **self._get_auth_info(project_name_key="tenant_name"))
        return client
Exemple #20
0
    def authenticate(self):

        # Authenticate to Keystone
        try:
            # connecting
            self.ks = ksclient.KSClient(auth_url=KEYSTONE_URL, username=USERNAME, password=PASSWORD)
            # construct the mon client
            self.monasca_client = client.Client(api_version, MONASCA_URL, token=self.ks.token)
        except:
             print('Connection error !')
        else:
            print('Connected to monasca server !')
Exemple #21
0
def create_client(api_version='2_0',
                  username=USERNAME,
                  password=PASSWORD,
                  auth_url=AUTH_URL,
                  project_name=PROJECT_NAME,
                  endpoint=MONITORING_URL):
    return client.Client(api_version=api_version,
                         username=username,
                         password=password,
                         auth_url=auth_url,
                         project_name=project_name,
                         endpoint=endpoint)
Exemple #22
0
 def _create(self):
     interface = self._get_client_option(CLIENT_NAME, 'endpoint_type')
     endpoint = self.url_for(service_type=self.MONITORING,
                             endpoint_type=interface)
     # Change this to use session once it's supported by monascaclient
     return client.Client(
         self.VERSION,
         token=self.context.keystone_session.get_token(),
         endpoint=endpoint,
         cacert=self._get_client_option(CLIENT_NAME, 'ca_file'),
         cert_file=self._get_client_option(CLIENT_NAME, 'cert_file'),
         key_file=self._get_client_option(CLIENT_NAME, 'key_file'),
         insecure=self._get_client_option(CLIENT_NAME, 'insecure'))
Exemple #23
0
    def _get_monasca_client(sess):
        disc = discover.Discover(session=sess)
        ks = disc.create_client(sess=sess)
        ks.auth_ref = sess.auth.get_auth_ref(session=sess)

        catalog = ks.auth_ref.service_catalog
        endpoint = catalog.url_for(service_type='monitoring',
                                   interface=CONF.monasca_client.endpoint_type,
                                   region_name=CONF.monasca_client.region_name)

        return client.Client(api_version='2_0',
                             endpoint=endpoint,
                             session=sess)
Exemple #24
0
def get_monasca_client(conf, conf_opts):
    ks_auth = ks_loading.load_auth_from_conf_options(conf, conf_opts)
    session = ks_loading.load_session_from_conf_options(conf,
                                                        conf_opts,
                                                        auth=ks_auth)
    keystone_client = ks_client.Client(session=session,
                                       interface=conf[conf_opts].interface)
    mon_endpoint = get_monasca_endpoint(conf[conf_opts], keystone_client)
    if not mon_endpoint:
        raise EndpointNotFound()
    return mclient.Client(api_version=MONASCA_API_VERSION,
                          session=session,
                          endpoint=mon_endpoint)
Exemple #25
0
 def _get_mon_client(self):
     try:
         k = keystone.Keystone(self._config)
         endpoint = k.get_monasca_url()
         session = k.get_session()
         c = client.Client(api_version=self._api_version,
                           endpoint=endpoint,
                           session=session,
                           timeout=self.write_timeout,
                           **keystone.get_args(self._config))
         return c
     except keystoneauth_exception.ClientException as ex:
         log.error('Failed to initialize Monasca client. {}'.format(ex))
Exemple #26
0
    def __init__(self, transformers, **kwargs):
        super(MonascaCollector, self).__init__(transformers, **kwargs)

        self.auth = ks_loading.load_auth_from_conf_options(
            CONF, COLLECTOR_MONASCA_OPTS)
        self.session = ks_loading.load_session_from_conf_options(
            CONF, COLLECTOR_MONASCA_OPTS, auth=self.auth)
        self.ks_client = ks_client.Client(session=self.session)
        self.mon_endpoint = self._get_monasca_endpoint()
        if not self.mon_endpoint:
            raise EndpointNotFound()
        self._conn = mclient.Client(api_version=MONASCA_API_VERSION,
                                    session=self.session,
                                    endpoint=self.mon_endpoint)
Exemple #27
0
 def __init__(self):
     self.log = logging.getLogger('L.MONASCA')
     self.api_version = "2_0"
     self.whoami = cc.conf['whoami']
     ks_conf = cc.conf['keystone']['monasca']
     self.ks = ksclient.KSClient(auth_url=ks_conf['auth_url'],
                                 username=ks_conf['username'],
                                 password=ks_conf['password'])
     self.log.debug('Got keystone token')
     self.monasca_client = client.Client(self.api_version,
                                         self.ks.monasca_url,
                                         token=self.ks.token)
     self.log.debug('Got monasca client')
     self.alarms = None
Exemple #28
0
    def test_should_warn_when_passing_args(self, _, __, ___):

        api_version = mock.Mock()
        endpoint = mock.Mock()

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')

            client.Client(api_version, endpoint)

            self.assertEqual(1, len(w))
            self.assertEqual(DeprecationWarning, w[0].category)
            self.assertRegex(str(w[0].message),
                             'explicit configuration of the client using')
Exemple #29
0
    def test_should_reuse_the_session_if_initialized_with_one(
            self, get_session, get_auth, _):
        from keystoneauth1 import session as k_session

        api_version = mock.Mock()
        session = mock.Mock(spec=k_session.Session)

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            client.Client(api_version, session=session)
            self.assertEqual(0, len(w))

        get_auth.assert_not_called()
        get_session.assert_not_called()
 def update_token(self):
     if self.__config:
         tokenfile = self.__config['projectpath']['path']+"/lock_token.tmp"
     else:
         tokenfile = "/tmp/lock_token.tmp"
     
     lock = Lock(tokenfile)
     if  lock.lockfileExists():
         try:
             lock.openLockfile('r')
         except IOError:
             if self.__config and strtobool(self.__config["api"]["debugMode"]):
                 print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] CollectorMonasca.set_token_from_token_file: the token file could not be opened and read")
             return
         
         parameters = lock.read()
         lock.closeLockfile()
         #if file is not empty
         if parameters and len(parameters) == 2:
             timestamp = utils.from_monasca_ts_to_datetime_ms(parameters[0])
             difference = int((datetime.datetime.now()-timestamp).total_seconds())
             #if token was got less than 3600 sec(1 hour) ago
             token_ttl = 3600
             if self.__config:
                 token_ttl = int(self.__config['keystone']['token_ttl'])
                 
             if difference < token_ttl:
                 #check if token is equal to the one stored inside the locked file. If not, take the stored one
                 if parameters[1] != self.__token:
                     #if the stored token is different from the one I have
                     self.__token = parameters[1]
                     # Instantiate a monascaclient object to use for query
                     self.__monasca_client = client.Client(self.__api_version, self.__monasca_endpoint, token=self.__token)
                     
                     if self.__config and strtobool(self.__config["api"]["debugMode"]):
                         print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] CollectorMonasca.set_token_from_token_file: the parent Collector is taking and storing the token from file")
                 else:
                     if self.__config and strtobool(self.__config["api"]["debugMode"]):
                         print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] CollectorMonasca.set_token_from_token_file: the parent Collector has already an updated token")
             #we need to reauthenticate to keystone
             else:
                 if self.__config and strtobool(self.__config["api"]["debugMode"]):
                         print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] CollectorMonasca.set_token_from_token_file: the parent Collector is going to reauthenticate because the token file was older than "+str(token_ttl)+" seconds")
                 self.__authenticate_monasca_client(1)
         else:
             if self.__config and strtobool(self.__config["api"]["debugMode"]):
                 print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] CollectorMonasca.set_token_from_token_file: the token file is empty")
     else:
         if self.__config and strtobool(self.__config["api"]["debugMode"]):
             print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] CollectorMonasca.set_token_from_token_file: the token file doesn't exists")