Ejemplo n.º 1
0
    def _get_ksclient(self):
        """Get a new keystone client object.

        The client provides a monasca_url property whose value is pulled from
        Keystone Service Catalog filtering by service_type, endpoint_type
        and/or region_name.

        """
        service_type = self.config.get('service_type', None)
        endpoint_type = self.config.get('endpoint_type', None)
        region_name = self.config.get('region_name', None)

        kc_args = self.get_credential_args()
        if service_type:
            kc_args.update({'service_type': service_type})
        if endpoint_type:
            kc_args.update({'endpoint_type': endpoint_type})
        if region_name:
            kc_args.update({'region_name': region_name})

        return ksclient.KSClient(**kc_args)
def query_measurement_average():

    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=args.metric_name,
                                           dimensions=utils.format_parameters(
                                               args.dimensions))
    metric_averages = {}
    for metric in metrics_data:
        if args.endtime is not None:
            measurements = mon_client.metrics.list_measurements(
                start_time=args.starttime,
                name=args.metric_name,
                dimensions=metric['dimensions'],
                end_time=args.endtime)
        else:
            measurements = mon_client.metrics.list_measurements(
                start_time=args.starttime,
                name=args.metric_name,
                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 + args.metric_name, "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))
Ejemplo n.º 3
0
    def _get_ksclient(self):
        """Get an endpoint and auth token from Keystone.

        """
        auth_url = self.config.get('keystone_url', None)
        username = self.config.get('username', None)
        password = self.config.get('password', None)
        user_domain_id = self.config.get('user_domain_id', None)
        user_domain_name = self.config.get('user_domain_name', None)
        insecure = self.config.get('insecure', False)
        cacert = self.config.get('ca_file', None)
        project_id = self.config.get('project_id', None)
        project_name = self.config.get('project_name', None)
        project_domain_name = self.config.get('project_domain_name', None)
        project_domain_id = self.config.get('project_domain_id', None)

        kc_args = {'auth_url': auth_url,
                   'username': username,
                   'password': password}

        if user_domain_id:
            kc_args.update({'user_domain_id': user_domain_id})
        elif user_domain_name:
            kc_args.update({'user_domain_name': user_domain_name})

        if insecure:
            kc_args.update({'insecure': insecure})
        else:
            if cacert:
                kc_args.update({'os_cacert': cacert})
        if project_id:
            kc_args.update({'project_id': project_id})
        elif project_name:
            kc_args.update({'project_name': project_name})
            if project_domain_name:
                kc_args.update({'domain_name': project_domain_name})
            if project_domain_id:
                kc_args.update({'domain_id': project_domain_id})

        return ksclient.KSClient(**kc_args)
Ejemplo n.º 4
0
    def _keystone_auth(self):
        """ Authenticate to Keystone and set self.token and self.api_url
        """
        if self.module.params['keystone_token'] is None:
            ks = ksclient.KSClient(
                auth_url=self.module.params['keystone_url'],
                username=self.module.params['keystone_user'],
                password=self.module.params['keystone_password'])

            self.token = ks.token
            if self.module.params['monasca_api_url'] is None:
                self.api_url = ks.monasca_url
            else:
                self.api_url = self.module.params['monasca_api_url']
        else:
            if self.module.params['monasca_api_url'] is None:
                self.module.fail_json(
                    msg=
                    'Error: When specifying keystone_token, monasca_api_url is required'
                )
            self.token = self.module.params['keystone_token']
            self.api_url = self.module.params['monasca_api_url']
Ejemplo n.º 5
0
 def re_authenticate(self):
     ks_args = {
         'username': self.username,
         'password': self.password,
         'user_domain_id': self.user_domain_id,
         'user_domain_name': self.user_domain_name,
         'token': '',
         'auth_url': self.auth_url,
         'service_type': self.service_type,
         'endpoint_type': self.endpoint_type,
         'os_cacert': self.ssl_connection_params['os_cacert'],
         'project_id': self.project_id,
         'project_name': self.project_name,
         'domain_id': self.domain_id,
         'domain_name': self.domain_name,
         'insecure': self.ssl_connection_params['insecure'],
         'region_name': self.region_name,
         'keystone_timeout': self.keystone_timeout
     }
     try:
         _ksclient = ksclient.KSClient(**ks_args)
         self.auth_token = _ksclient.token
     except Exception as e:
         raise exc.KeystoneException(e)
    def __authenticate_monasca_client(self, force = 0):        
        
        mutexfile = ""
        lockfile = ""
        if self.__config:
            mutexfile = self.__config['projectpath']['path']+"/mutex.tmp"
            lockfile = self.__config['projectpath']['path']+"/lock_token.tmp"
        else:
            mutexfile = "/tmp/mutex.tmp"
            lockfile = "/tmp/lock_token.tmp"
        mutex = Mutex(mutexfile)
        lock = Lock(lockfile)
        
        #It is used to check if a process can only read or also write inside tokenfile
        onlyReadMode = True
        
        #if we don't force the authentication (we force only first time)
        if not force:
            #if there is mutex, some process is already writing. But it could be dead                            
            if not mutex.mutexExists() or mutex.isObsolete():  
                #if the mutex has been created
                if mutex.createMutex():
                    onlyReadMode = False
   
                    if  lock.lockfileExists():
                        try:
                            #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] Read token file")
                            lock.openLockfile('r')
                        except IOError:
                            mutex.removeMutex()
                            raise Exception("The token file could not be read and mutex was set")
                        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 300 sec(5 mins) ago
                            authenticate_retry_wait_secs = 300
                            if self.__config:
                                authenticate_retry_wait_secs = int(self.__config['keystone']['authenticate_retry_wait_secs'])
                                
                            if difference < authenticate_retry_wait_secs:
                                #check if token is equal to the one stored inside the locked file. If not, take the stored one
                                if parameters[1] != self.__token:
                                    #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] USE TOKEN IN FILE:"+parameters[1])
                                    #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)
                                    mutex.removeMutex()
                                    #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] USE TOKEN IN FILE:return")
                                    return
                                else:
                                    mutex.removeMutex()
                                    raise Exception("We need to wait "+authenticate_retry_wait_secs+" secs before reauthenticating to Keystone. The token in file is no more valid")
                            
                        else:
                            if self.__config and strtobool(self.__config["api"]["debugMode"]):
                                print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] The lock 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")+"] The token file doesn't exists and mutex was set")
                else:
                    if self.__config and strtobool(self.__config["api"]["debugMode"]):
                        print("["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] the fork tried to create mutex, but it was already set")
            
            #the mutex was set so the process can only read
            if mutex.mutexExists() and onlyReadMode:
                #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] --------ONLY READ MODE-------")
                #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] __authenticate_monasca_client::mutex.mutexExists")
                if  lock.lockfileExists():
                    #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] __authenticate_monasca_client::lock.lockfileExists")
                    try:
                        lock.openLockfile('r')
                    except IOError:
                        raise Exception("The token file could not be read and mutex was set by another process")
                    parameters = lock.read()
                    lock.closeLockfile()
                    #if file is not empty
                    if parameters and len(parameters) == 2:
                        #check if token is equal to the one stored inside the locked file. If not, take the stored one
                        if parameters[1] != self.__token:
                            #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] USE TOKEN IN FILE:"+parameters[1])
                            #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)
                            #return
                            return
                        else:
                            raise Exception(str(os.getpid())+"] Keystone token in token file is no more valid. Mutex is set by another process.")
                    else:
                        raise Exception(str(os.getpid())+"] The token file is empty and mutex was set")
                else:
                    raise Exception(str(os.getpid())+"] The token file doesn't exists and mutex was set")
                
        #This part is executed when we need to generate a new token        
        if force or not onlyReadMode:
            try:
                # Authenticate to Keystone
                ks = ksclient.KSClient(auth_url=self.__keystone_endpoint, username=self.__username, password=self.__password)

                #print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] autenticated to ks")

                self.__token = ks.token
                
                # Instantiate a monascaclient object to use for query
                self.__monasca_client = client.Client(self.__api_version, self.__monasca_endpoint, token=self.__token)
            
                lock.openLockfile('w')
                #Try to lock file. If already locked then exception
                lock.acquire()  
                
                if self.__config and strtobool(self.__config["api"]["debugMode"]):
                    print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] Collector is writing inside locked file")
                    
                lock.write(ks.token) 
            except OSError as e:
                if self.__config and strtobool(self.__config["api"]["debugMode"]):
                    print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] OSError File already locked by another fork")
                lock.closeLockfile()
                lock.release()
                mutex.removeMutex()
                raise e
            except IOError as e:
                if self.__config and strtobool(self.__config["api"]["debugMode"]):
                    print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] IOError File already locked by another fork")
                lock.closeLockfile()
                lock.release()
                mutex.removeMutex()
                raise e
            except Exception as e:
                if self.__config and strtobool(self.__config["api"]["debugMode"]):
                    print(str(os.getpid())+"]["+datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000Z")+"] Problem in Authenticating to Keystone")
                    print(traceback.format_exc())
                lock.closeLockfile()
                lock.release()
                mutex.removeMutex()
                raise e
            finally:                
                lock.closeLockfile()
                lock.release()                
                mutex.removeMutex()  
    def _refresh_client(self):
        _ksclient = ksclient.KSClient(**self._kwargs)

        self._kwargs['token'] = _ksclient.token
        self._mon_client = client.Client(cfg.CONF.monasca.clientapi_version,
                                         self._endpoint, **self._kwargs)
Ejemplo n.º 8
0
# A metric identity is composed by its name and dimensions (a dictionary
# that represents attributes, as the host of the metric owner and the
# application id). For this example, socket.getfqdn() gets the context hostname.
metric_info = {
    'name': 'metric.name',
    'dimensions': {
        'hostname': socket.getfqdn()
    }
}

# Monasca client needs authenticate via keystone. Creating a keystone
# client is possible to get a token that will be used after to create a
# Monasca client.
ks = ksclient.KSClient(auth_url='http://<keystone_url>:5000/v3',
                       username='******',
                       password='******',
                       project_name='monasca')

# Creating a monasca client using the keystone token
monasca_client = monclient.Client('2_0', ks.monasca_url, token=ks.token)


# For this approach, the format of the log must follow some pattern.
# The method get_metric_value extract the value from this log pattern.
# Flags are used to capture logs of interest and value into these logs
# For this use case "#" is a token to identify where exactly the value
# is located and makes possible to convert the string to float value.
def get_metric_value(log):
    for i in range(len(log) - 1, 0, -1):
        if log[i] == '#':
            value = float(log[i + 1:len(log)])
Ejemplo n.º 9
0
__author__ = 'ryan'

import sys

from monascaclient import ksclient

keystone = {
    'username': '******',
    'password': '******',
    'project_name': 'mini-mon',
    'project_domain_name': 'default',
    'user_domain_name': 'default',
    'auth_url': 'http://192.168.10.6:35357/v3'
}

if len(sys.argv) >= 2:
    keystone['auth_url'] = int(sys.argv[1])

if len(sys.argv) >= 3:
    keystone['username'] = int(sys.argv[2])

if len(sys.argv) >= 4:
    keystone['password'] = int(sys.argv[3])

if len(sys.argv) >= 5:
    keystone['project'] = int(sys.argv[4])

ks_client = ksclient.KSClient(**keystone)

print("Token: {}".format(ks_client.token))
Ejemplo n.º 10
0
def alarm_performance_test():
    if num_processes < num_definitions:
        return False, "Number of agents ({0}) must be >= number of definitions ({1})".format(num_processes,
                                                                                             num_definitions)

    try:
        print('Authenticating with keystone on {}'.format(keystone['auth_url']))
        ks_client = ksclient.KSClient(**keystone)
    except Exception as ex:
        return False, 'Failed to authenticate: {}'.format(ex)

    mon_client = client.Client('2_0', urls[0], token=ks_client.token)

    print('Removing old alarm definitions for {}'.format(alarm_def_name))
    cleanup(mon_client, alarm_def_name)

    alarm_def_id_list = []
    print('Creating alarm definitions')
    for i in xrange(num_definitions):
        expression = alarm_def_expression.format(metric_name+str(i))
        alarm_def_id = create_alarm_definition(mon_client, alarm_def_name+str(i), expression)
        if not alarm_def_id:
            return False, "Failed to create alarm definition"
        alarm_def_id_list.append(alarm_def_id)

    sent_q = multiprocessing.Queue()

    process_list = []
    for i in xrange(num_processes):
        p = multiprocessing.Process(target=agent_sim_process(i, num_requests, num_metrics, urls[(i % len(urls))],
                                                             keystone, queue=sent_q,
                                                             metric_creator=MetricCreatorAlarmPerf).run)
        process_list.append(p)

    start_datetime = datetime.datetime.now()
    start_datetime = start_datetime - datetime.timedelta(microseconds=start_datetime.microsecond)
    print("Starting test at: " + start_datetime.isoformat())
    start_time = time.time()

    for p in process_list:
        p.start()

    try:
        for p in process_list:
            try:
                p.join()
            except Exception:
                pass

    except KeyboardInterrupt:
        return False, "User interrupt"

    final_time = time.time()

    # There is some chance that not all metrics were sent (lost connection, bad status, etc.)
    total_metrics_sent = aggregate_sent_metric_count(sent_q)
    print('Sent {} metrics in {} seconds'.format(total_metrics_sent,final_time-start_time))
    if total_metrics_sent <= 0:
        return False, "Failed to send metrics"

    print('Waiting for alarms to be created')
    alarm_count = 0
    last_count = 0
    last_change = time.time()
    while alarm_count < total_metrics_sent:
        alarm_count = 0
        for id in alarm_def_id_list:
            num = len(mon_client.alarms.list(alarm_definition_id=id))
            alarm_count += num
        if alarm_count > last_count:
            last_change = time.time()
            last_count = alarm_count

        if (last_change + max_wait_time) <= time.time():
            metrics_found = 0
            for i in xrange(num_definitions):
                val = len(mon_client.metrics.list_measurements(start_time=start_datetime.isoformat(), name=metric_name+str(i),
                                                       merge_metrics=True)[0]['measurements'])
                metrics_found += val
            return False, "Max wait time exceeded, {0} / {1} alarms found".format(alarm_count, metrics_found)
        time.sleep(1)

    delta = last_change - start_time

    tot_met = 0
    for i in xrange(num_definitions):
        metrics = mon_client.metrics.list_measurements(start_time=start_datetime.isoformat(), name=metric_name+str(i),
                                                       merge_metrics=True)
        tot_met += len(metrics[0]['measurements'])

    print("Metrics from api: {}".format(tot_met))
    print("-----Test Results-----")
    print("{} alarms in {} seconds".format(alarm_count, delta))
    print("{} per second".format(alarm_count/delta))

    if cleanup_after_test:
        cleanup(mon_client, alarm_def_name)
    return True, ""
Ejemplo n.º 11
0
]

start_time = "2015-09-29T12:00:00.000Z"
end_time = "2015-10-30T23:59:59.000Z"

ks_url = "http://10.241.67.7:35357/v3/"
ks_user = "******"
ks_pass = "******"
ks_project_name = "admin"
ks_project_domain = "default"
mon_url = "http://10.241.67.7:8070/v2.0/"

# get all metrics (in json) and collect into single list, write to file
ksclient = ksclient.KSClient(auth_url=ks_url,
                             username=ks_user,
                             password=ks_pass,
                             project_name=ks_project_name,
                             project_domain_name=ks_project_domain)
mon_client = client.Client("2_0", mon_url, token=ksclient.token)

rabbit_exchange = [
    "rabbitmq.exchange.messages.published_count",
    "rabbitmq.exchange.messages.published_rate",
    "rabbitmq.exchange.messages.received_count",
    "rabbitmq.exchange.messages.received_rate"
]

rabbit_nodes = [
    "rabbitmq.node.fd_used", "rabbitmq.node.mem_used",
    "rabbitmq.node.run_queue", "rabbitmq.node.sockets_used"
]
Ejemplo n.º 12
0
    def main(self, argv):
        # Parse args once to find version
        parser = self.get_base_parser()
        (options, args) = parser.parse_known_args(argv)
        self._setup_logging(options.debug)
        self._setup_verbose(options.verbose)

        # build available subcommands based on version
        api_version = options.monasca_api_version
        subcommand_parser = self.get_subcommand_parser(api_version)
        self.parser = subcommand_parser

        # Handle top-level --help/-h before attempting to parse
        # a command off the command line
        if not args and options.help or not argv:
            self.do_help(options)
            return 0

        # Parse args again and call whatever callback was selected
        args = subcommand_parser.parse_args(argv)

        # Short-circuit and deal with help command right away.
        if args.func == self.do_help:
            self.do_help(args)
            return 0
        elif args.func == self.do_bash_completion:
            self.do_bash_completion(args)
            return 0

        if not args.os_username and not args.os_auth_token:
            raise exc.CommandError("You must provide a username via"
                                   " either --os-username or env[OS_USERNAME]"
                                   " or a token via --os-auth-token or"
                                   " env[OS_AUTH_TOKEN]")

        if not args.os_password and not args.os_auth_token:
            raise exc.CommandError("You must provide a password via"
                                   " either --os-password or env[OS_PASSWORD]"
                                   " or a token via --os-auth-token or"
                                   " env[OS_AUTH_TOKEN]")

        if args.os_no_client_auth:
            if not args.monasca_api_url:
                raise exc.CommandError("If you specify --os-no-client-auth"
                                       " you must specify a Monasca API URL"
                                       " via either --monasca-api-url or"
                                       " env[MONASCA_API_URL]")
        else:
            if not args.os_auth_url:
                raise exc.CommandError("You must provide an auth url via"
                                       " either --os-auth-url or via"
                                       " env[OS_AUTH_URL]")

        if args.os_auth_url and 'v2.0' in args.os_auth_url:
            args.os_auth_url = string.replace(args.os_auth_url, 'v2.0', 'v3')

        kwargs = {
            'username': args.os_username,
            'password': args.os_password,
            'token': args.os_auth_token,
            'auth_url': args.os_auth_url,
            'service_type': args.os_service_type,
            'endpoint_type': args.os_endpoint_type,
            'os_cacert': args.os_cacert,
            'user_domain_id': args.os_user_domain_id,
            'user_domain_name': args.os_user_domain_name,
            'project_id': args.os_project_id,
            'project_name': args.os_project_name,
            'domain_id': args.os_domain_id,
            'domain_name': args.os_domain_name,
            'insecure': args.insecure,
            'region_name': args.os_region_name,
            'keystone_timeout': args.keystone_timeout
        }

        endpoint = args.monasca_api_url

        if not args.os_no_client_auth:
            _ksclient = ksclient.KSClient(**kwargs)
            if args.os_auth_token:
                token = args.os_auth_token
            else:
                try:
                    token = _ksclient.token
                except exc.CommandError:
                    raise exc.CommandError(
                        "User does not have a default project. "
                        "You must provide a project id using "
                        "--os-project-id or via env[OS_PROJECT_ID], "
                        "or you must provide a project name using "
                        "--os-project-name or via env[OS_PROJECT_NAME] "
                        "and a project domain using --os-domain-name, via "
                        "env[OS_DOMAIN_NAME],  using --os-domain-id or "
                        "via env[OS_DOMAIN_ID]")

            kwargs = {
                'token': token,
                'insecure': args.insecure,
                'os_cacert': args.os_cacert,
                'cert_file': args.cert_file,
                'key_file': args.key_file,
                'username': args.os_username,
                'password': args.os_password,
                'service_type': args.os_service_type,
                'endpoint_type': args.os_endpoint_type,
                'auth_url': args.os_auth_url,
                'keystone_timeout': args.keystone_timeout
            }

            if args.os_user_domain_name:
                kwargs['user_domain_name'] = args.os_user_domain_name
            if args.os_user_domain_id:
                kwargs['user_domain_id'] = args.os_user_domain_id
            if args.os_region_name:
                kwargs['region_name'] = args.os_region_name
            if args.os_project_name:
                kwargs['project_name'] = args.os_project_name
            if args.os_project_id:
                kwargs['project_id'] = args.os_project_id
            if args.os_domain_name:
                kwargs['domain_name'] = args.os_domain_name
            if args.os_domain_id:
                kwargs['domain_id'] = args.os_domain_id

            if not endpoint:
                endpoint = _ksclient.monasca_url

        client = monasca_client.Client(api_version, endpoint, **kwargs)

        args.func(client, args)
Ejemplo n.º 13
0
def metric_performance_test():

    try:
        print('Authenticating with keystone on {}'.format(keystone['auth_url']))
        ks_client = ksclient.KSClient(**keystone)
    except Exception as ex:
        return False, 'Failed to authenticate: {}'.format(ex)

    mon_client = client.Client('2_0', urls[0], token=ks_client.token)

    sent_q = multiprocessing.Queue()

    process_list = []
    for i in xrange(num_processes):
        p = multiprocessing.Process(target=agent_sim_process(i, num_requests, num_metrics, urls[(i % len(urls))],
                                                             keystone, queue=sent_q,
                                                             metric_creator=MetricCreatorMetricPerf).run)
        process_list.append(p)

    start_datetime = datetime.datetime.now()
    start_datetime = start_datetime - datetime.timedelta(microseconds=start_datetime.microsecond)
    print("Starting test at: " + start_datetime.isoformat())
    start_time = time.time()

    for p in process_list:
        p.start()

    try:
        for p in process_list:
            try:
                p.join()
            except Exception:
                pass

    except KeyboardInterrupt:
        return False, "User interrupt"



    total_metrics_sent = aggregate_sent_metric_count(sent_q)

    metrics_found = 0
    last_count = 0
    last_change = time.time()
    while metrics_found < total_metrics_sent:

        metrics_found = 0
        try:
            stats = mon_client.metrics.list_statistics(statistics="count",
                                                       start_time=start_datetime.isoformat(),
                                                       name="metric_perf",
                                                       merge_metrics=True)
            metrics_found = stats[0]['statistics'][0][1]
        except Exception as ex:
            print("Failed to retrieve metrics from api\n{}".format(ex))

        if metrics_found > last_count:
            last_change = time.time()
            last_count = metrics_found

        if (last_change + max_wait_time) <= time.time():
            return False, "Max wait time exceeded, {0} / {1} metrics found".format(metrics_found, total_metrics_sent)
        time.sleep(1)

    final_time = time.time()
    print("-----Test Results-----")
    print("{} metrics in {} seconds".format(metrics_found, final_time-start_time))
    print("{} per second".format(metrics_found / (final_time - start_time)))

    return True, ""
Ejemplo n.º 14
0
def notification_perf_test():
    global last_response
    global response_count
    global stop_server

    try:
        print('Authenticating with keystone on {}'.format(keystone['auth_url']))
        ks_client = ksclient.KSClient(**keystone)
    except Exception as ex:
        print('Failed to authenticate: {}'.format(ex))
        return False

    token = ks_client.token

    mon_client = client.Client('2_0', urls[0], token=token)

    print("Creating notification method")
    notification_id = create_notification_method(mon_client)
    if not notification_id:
        return False

    alarm_definition['ok_actions'].append(notification_id)
    alarm_definition['alarm_actions'].append(notification_id)
    alarm_definition['undetermined_actions'].append(notification_id)

    print("Creating alarm definition")
    alarm_def_id = create_alarm_definition(mon_client)
    if not alarm_def_id:
        return False

    base_message['alarm-transitioned']['alarmDefinitionId'] = alarm_def_id

    process_list = []

    server = threading.Thread(target=check_notifications, args=(BaseHTTPServer.HTTPServer, TestHTTPRequestHandler))
    server.start()

    start_time = time.time()
    print("Sending {} state transitions".format(num_processes*num_messages))
    for i in xrange(num_processes):
        p = multiprocessing.Process(target=produce_alarm_state_transitions, args=(i,))
        process_list.append(p)
        p.start()

    try:
        for p in process_list:
            try:
                p.join()
            except Exception:
                pass

    except KeyboardInterrupt:
        return 0

    last_response = time.time()

    print("Waiting for notifications".format(num_processes * num_messages))
    while server.isAlive():
        if(last_response + max_wait_time) < time.time():
            stop_server = True
            print("Max wait time exceeded after {} responses".format(response_count))
            return False
        server.join((last_response+max_wait_time)-time.time())

    final_time = time.time()

    print("-----Test Results-----")
    print("{} notifications arrived in {} seconds".format(response_count, final_time-start_time))
    print("{} per second".format(response_count/(final_time-start_time)))
    return True
def notification_function_test():
    global last_response
    global response_count
    global stop_server

    try:
        print('Authenticating with keystone on {}'.format(
            keystone['auth_url']))
        ks_client = ksclient.KSClient(**keystone)
    except Exception as ex:
        print('Failed to authenticate: {}'.format(ex))
        return False

    token = ks_client.token

    mon_client = client.Client('2_0', urls[0], token=token)

    print("Creating notification method")
    notification_id = create_notification_method(mon_client)
    if not notification_id:
        return False

    alarm_definition['ok_actions'].append(notification_id)
    alarm_definition['alarm_actions'].append(notification_id)
    alarm_definition['undetermined_actions'].append(notification_id)

    print("Creating alarm definition")
    alarm_def_id = create_alarm_definition(mon_client)
    if not alarm_def_id:
        return False

    base_message['alarm-transitioned']['alarmDefinitionId'] = alarm_def_id

    server = threading.Thread(target=check_notifications,
                              args=(BaseHTTPServer.HTTPServer,
                                    TestHTTPRequestHandler))
    server.start()

    time.sleep(1)

    start_time = time.time()

    produce_alarm_state_transition()

    last_response = time.time()

    print("Waiting for notifications")
    while server.isAlive():
        if (last_response + max_wait_time) < time.time():
            stop_server = True
            print("Max wait time exceeded after {} responses".format(
                response_count))
            return False
        server.join((last_response + max_wait_time) - time.time())

    final_time = time.time()

    print("-----Test Results-----")
    print("{} notifications arrived in {} seconds".format(
        response_count, final_time - start_time))
    return True
Ejemplo n.º 16
0
    def _get_ksclient(self):
        """Get an endpoint and auth token from Keystone.

        """
        kc_args = self.get_credential_args()
        return ksclient.KSClient(**kc_args)
def get_metric_id(alarm):
    '''Get metric ID for alarm
    :param alarm: Alarm
    :return: ID of metric used in alarm
    '''
    metrics = alarm['metrics']
    metric = metrics[0]
    dimensions = metric['dimensions']
    id = int(dimensions['id'])
    return id


keystone_client = monascaclient_ksclient.KSClient(auth_url=AUTH_URL,
                                                  username=USERNAME,
                                                  project_name=PROJECT_NAME,
                                                  domain_name=DOMAIN_NAME,
                                                  password=PASSWORD)
monasca_client = monascaclient_client.Client('2_0',
                                             keystone_client.monasca_url,
                                             token=keystone_client.token)

# Create alarm definition and wait 10 secsond for alarms to be created
logger.info(
    'create alarm definition and wait 10 seconds for alarms to be created')
alarm_defn = create_alarm_definition()
send_alarm_definition(monasca_client, alarm_defn)
metrics = [create_metric(str(id), 0.0) for id in xrange(NUM_METRICS_PER_TRIAL)]
send_metrics(monasca_client, metrics)
time.sleep(10)
logger.info('done')