def __init__(self, config_name):
        opts = Opts(cloud_name=config_name)

        cc = cloud_config.OpenStackConfig()
        LOG.debug("defaults: %s", cc.defaults)

        # clouds.yaml file should either be in the
        # current directory or
        # ~/.config/openstack directory or
        # /etc/openstack directory.
        cloud = cc.get_one_cloud(opts.cloud)
        LOG.debug("cloud cfg: %s", cloud.config)

        # Create a context for a connection to the cloud provider
        self.conn = connection.from_config(cloud_config=cloud,
                                           options=opts)

        identity_api_version = cloud.config['identity_api_version']
        if identity_api_version != '3':
            LOG.error('Only Identity version 3 is supported.')

        # We still need to use aodhclient until alarming APIs
        # are added to openstackclient
        self.aodh_conn = aodh_client.Client(
            session=cloud.get_session_client('alarming'))
        self.aodh_conn.alarm.url = '/alarms'
Esempio n. 2
0
def aodhclient(request):
    """ Initialization of Gnocchi client.
    """
    auth_endpoint = getattr(settings, 'OPENSTACK_KEYSTONE_URL', None)

    loader = loading.get_plugin_loader('token')
    auth = loader.load_from_options(auth_url=auth_endpoint,
                                    token=request.user.token.id,
                                    project_id=request.user.project_id)
    sess = session.Session(auth=auth)

    LOG.debug('aodhclient connection created using token "%s" '
              'and endpoint "%s"' % (request.user.token.id, auth_endpoint))

    return aodh_client.Client(session=sess)
def get_aodh_session_client(session):
    return aodh_client.Client(session=session)
Esempio n. 4
0
def get_aodh_client(**kwargs):
    return aodhclient.Client('')
Esempio n. 5
0
def openstack_view(request):
    try:
        conf = Openstack_Auth.objects.get(pk=1)
        loader = loading.get_plugin_loader('password')
        auth = loader.load_from_options(
            auth_url=conf.os_url,
            project_domain_name=conf.os_project_domain_name,
            user_domain_name=conf.os_user_domain_name,
            username=conf.os_user_name,
            #password= conf.os_password,
            password=conf.os_password_encrypt,
            tenant_name=conf.os_tenant_name,
            project_name=conf.os_project_name,
        )
        sess = session.Session(auth=auth)
        nova = novaclient.Client('2',
                                 session=sess,
                                 endpoint_type=conf.os_url_type)
        gcon = gclient.Client('1',
                              session=sess,
                              adapter_options={
                                  'connect_retries': 3,
                                  'interface': conf.os_url_type
                              })
        al = nova.servers.list()
        slist = []
        td = datetime.timedelta(hours=5, minutes=30)
        for s in al:
            xlist = []
            if 'asg_name' in s.metadata and s.metadata[
                    'asg_name'] == 'autoscale_demo_1':
                try:
                    #Gnocchi data collection for the server s
                    all_cpu_util_values = gcon.metric.get_measures(
                        'cpu_util', resource_id=s.id)
                    vdate, vgran, cutil = all_cpu_util_values[-1]
                    vdatef = vdate.strftime('%Y-%m-%d %H:%M:%S')
                    for cpu_util_value in all_cpu_util_values:
                        udate, xgran, xutil = cpu_util_value
                        xdate = udate + td
                        xdict = {
                            'xdate': xdate.strftime('%Y-%m-%d %H:%M:%S'),
                            'xutil': xutil
                        }
                        xlist.append(xdict)
                except:
                    all_cpu_util_values = []
                    vdatef, vgran, cutil = ('try after 10 Minutes',
                                            'try after 10 Minutes',
                                            'try after 10 Minutes')
                    xlist = []

                try:
                    list_of_ips = s.networks.itervalues().next()
                    fixedip = list_of_ips[0]
                    floatip = list_of_ips[1]
                except:
                    fixedip = 'getting prepared'
                    floatip = 'getting prepared'

                #Create the various key value parameters for server s
                sdict = {
                    'sobj': s,
                    'fixedip': fixedip,
                    'floatip': floatip,
                    'collection_time': vdatef,
                    'cutil': cutil,
                    'all_cpu_util_values': all_cpu_util_values,
                    'xlist': xlist
                }
                #add all the servers key value in a list for template to unpack
                slist.append(sdict)

    except:
        pass
    alist = []
    ahistory_with_localtime = []
    td = datetime.timedelta(hours=5, minutes=30)
    try:
        acon = aclient.Client(session=sess, interface='internalURL')
        for myalarm in acon.alarm.list():
            rule = myalarm['gnocchi_aggregation_by_resources_threshold_rule']
            rq = rule['query']
            #if 'f447c07f-ff7b-48b8-924b-ff7220e20c0b' in rq:
            if conf.os_stack_id in rq:
                aid = myalarm['alarm_id']
                aname = myalarm['name']
                ahistory_with_utc = acon.alarm_history.get(aid)
                for h in ahistory_with_utc:
                    htime = datetime.datetime.strptime(h['timestamp'],
                                                       '%Y-%m-%dT%H:%M:%S.%f')
                    htime_local = htime + td
                    ht_str = htime_local.strftime('%Y-%m-%dT%H:%M:%S.%f')
                    ahistory_with_localtime.append({
                        'timestamp': ht_str,
                        'detail': h['detail']
                    })
                adict = {
                    'aid': aid,
                    'aname': aname,
                    'ahistory': ahistory_with_localtime
                }
                alist.append(adict)
    except:
        pass
    context = {'slist': slist, 'alist': alist}
    return render(request, 'openstack_view.html', context)