Ejemplo n.º 1
0
def pricing(meters_used,meter_list,from_date,to_date,from_time,to_time,user_id_stack,token_data,token_id,pricing_list,unit):
    all_stats=[] 
    time_period=0
    total=[None]*len(meters_used)
    for i in range(len(meters_used)):
        total[i]=0
        for j in range(len(meter_list)):
            if meters_used[i]==meter_list[j]["meter-name"]:
                resource_id=meter_list[j]["resource-id"]
                q=ceilometer_api.set_query(from_date,to_date,from_time,to_time,resource_id,user_id_stack,True)
                status,stat_list=ceilometer_api.meter_statistics(meters_used[i], token_data["metering"],token_id,meter_list,True,q=q)
                if stat_list==[]:
                    total[i]+=0
                else:
                    if meter_list[j]["meter-type"]=="cumulative":
                        total[i]+=stat_list[0]["max"]-stat_list[0]["min"]                                           
                    if meter_list[j]["meter-type"]=="gauge":
                        t1=datetime.datetime.combine(datetime.datetime.strptime(from_date,"%Y-%m-%d").date(),datetime.datetime.strptime(from_time,"%H:%M:%S").time())
                        t2=datetime.datetime.combine(datetime.datetime.strptime(to_date,"%Y-%m-%d").date(),datetime.datetime.strptime(to_time,"%H:%M:%S").time())
                        t=t2-t1
                        time_period=t.total_seconds()
                        total[i]+=stat_list[0]["average"]*time_period
                    if meter_list[j]["meter-type"]=="delta":
                        total[i]+=stat_list[0]["sum"]
        all_stats.append(total[i])
        for s in range(len(pricing_list)):
            if(pricing_list[s]==meters_used[i]):
                pricing_list[s]=total[i]


    for i in range(len(pricing_list)):
        if pricing_list[i]==None:
            pricing_list[i]=0
        price=0.0    
    for i in range(len(pricing_list)):
        if i==0:   
            if periodic.is_number(str(pricing_list[i])):    
                price=price+float(str(pricing_list[i]))
 
        if i%2!=0:
            if pricing_list[i] in ["+","-","*","/","%"]:
                if periodic.is_number(str(pricing_list[i+1])):
                    x=float(str(pricing_list[i+1]))                             
                else:
                    break                          
            if pricing_list[i]=="+":
                price=price+x
            if pricing_list[i]=="-": 
                price=price-x
            if pricing_list[i]=="*":
                price=price*x
            if pricing_list[i]=="/":
                if x!=0:
                    price=price/x
            if pricing_list[i]=="%":
                price=price*x/100.0
                                
    price=price*unit
    return price
Ejemplo n.º 2
0
def pricing(meters_used,meter_list,from_date,to_date,from_time,to_time,user_id_stack,token_data,token_id,pricing_list,unit):
    all_stats=[] 
    time_period=0
    total=[None]*len(meters_used)
    for i in range(len(meters_used)):
        total[i]=0
        for j in range(len(meter_list)):
            if meters_used[i]==meter_list[j]["meter-name"]:
                resource_id=meter_list[j]["resource-id"]
                q=ceilometer_api.set_query(from_date,to_date,from_time,to_time,resource_id,user_id_stack,True)
                status,stat_list=ceilometer_api.meter_statistics(meters_used[i], token_data["metering"],token_id,meter_list,True,q=q)
                if stat_list==[]:
                    total[i]+=0
                else:
                    if meter_list[j]["meter-type"]=="cumulative":
                        total[i]+=stat_list[0]["max"]-stat_list[0]["min"]                                           
                    if meter_list[j]["meter-type"]=="gauge":
                        t1=datetime.datetime.combine(datetime.datetime.strptime(from_date,"%Y-%m-%d").date(),datetime.datetime.strptime(from_time,"%H:%M:%S").time())
                        t2=datetime.datetime.combine(datetime.datetime.strptime(to_date,"%Y-%m-%d").date(),datetime.datetime.strptime(to_time,"%H:%M:%S").time())
                        t=t2-t1
                        time_period=t.total_seconds()
                        total[i]+=stat_list[0]["average"]*time_period
                    if meter_list[j]["meter-type"]=="delta":
                        total[i]+=stat_list[0]["sum"]
        all_stats.append(total[i])
        for s in range(len(pricing_list)):
            if(pricing_list[s]==meters_used[i]):
                pricing_list[s]=total[i]


    for i in range(len(pricing_list)):
        if pricing_list[i]==None:
            pricing_list[i]=0
        price=0.0    
    for i in range(len(pricing_list)):
        if i==0:   
            if periodic.is_number(str(pricing_list[i])):    
                price=price+float(str(pricing_list[i]))
 
        if i%2!=0:
            if pricing_list[i] in ["+","-","*","/","%"]:
                if periodic.is_number(str(pricing_list[i+1])):
                    x=float(str(pricing_list[i+1]))                             
                else:
                    break                          
            if pricing_list[i]=="+":
                price=price+x
            if pricing_list[i]=="-": 
                price=price-x
            if pricing_list[i]=="*":
                price=price*x
            if pricing_list[i]=="/":
                if x!=0:
                    price=price/x
            if pricing_list[i]=="%":
                price=price*x/100.0
                                
    price=price*unit
    return price
Ejemplo n.º 3
0
def get_udr(self,token_id,token_metering,user,meters_used,meter_list,func,web_bool,from_date,from_time,end_date,end_time,user_id_stack):   
    conn = sqlite3.connect(path+'/db.sqlite3',check_same_thread=False)
    date_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    delta_list=[None]*5
    all_stats=[] 
    total=[None]*len(meters_used)
    new_time="/"
    for i in range(len(meters_used)):
        total[i]=0
        for j in range(len(meter_list)):
            if meters_used[i]==meter_list[j]["meter-name"]:
                resource_id=meter_list[j]["resource-id"]
                q=ceilometer_api.set_query(from_date,end_date,from_time,end_time,resource_id,user_id_stack,True)
                status,stat_list=ceilometer_api.meter_statistics(meters_used[i], token_metering,token_id,meter_list,True,q=q)
                unit=meter_list[j]["meter-unit"]
                if stat_list==[]:
                    total[i]+=0
                else:
                    if meter_list[j]["meter-type"]=="cumulative":
                        total[i]+=stat_list[0]["max"]-stat_list[0]["min"]
                    if meter_list[j]["meter-type"]=="gauge":
                        t1=datetime.datetime.combine(datetime.datetime.strptime(from_date,"%Y-%m-%d").date(),datetime.datetime.strptime(from_time,"%H:%M:%S").time())
                        t2=datetime.datetime.combine(datetime.datetime.strptime(end_date,"%Y-%m-%d").date(),datetime.datetime.strptime(end_time,"%H:%M:%S").time())
                        t=t2-t1
                        time_period=t.total_seconds()
                        total[i]+=stat_list[0]["average"]*time_period
                    if meter_list[j]["meter-type"]=="delta":
                        total[i]+=stat_list[0]["sum"]
                    new_time=stat_list[0]["duration-end"]   
        conn.execute("INSERT INTO MAIN_MENU_METERSCOUNTER(METER_NAME,USER_ID_ID,COUNTER_VOLUME,UNIT,TIMESTAMP) \
            VALUES ('"+ str(meters_used[i]) +"' ,' "+ str(user)+"' ,' "+str(total[i])+"' ,' "+str(unit)+"' ,' "+str(date_time)+" ')")
        #meters_counter=MetersCounter(meter_name=meters_used[i],user_id=user,counter_volume=total[i],unit=unit ,timestamp=date_time)
        #meters_counter.save() 
        #delta=get_delta_samples(self,token_metering,token_id,user,meters_used[i])
        #delta_list[i]=delta
        for i in range(len(delta_list)):
            for j in range(len(total)):
                if i==j:
                    delta_list[i]=total[j]
    conn.execute("INSERT INTO MAIN_MENU_UDR(USER_ID_ID,TIMESTAMP,PRICING_FUNC_ID_ID,PARAM1,PARAM2,PARAM3,PARAM4,PARAM5) \
           VALUES ( '"+ str(user) +"', '"+str(date_time)+"', '"+str(func)+"', '"+ str(delta_list[0]) +"','" +str(delta_list[1]) +"','" +str(delta_list[2]) +"','" + str(delta_list[3]) +" ','"+ str(delta_list[4]) +" ')")
#    udr=Udr(user_id=user,timestamp=date_time,pricing_func_id=func, param1=delta_list[0], param2=delta_list[1], param3=delta_list[2], param4=delta_list[3], param5=delta_list[4])
#    udr.save()        
    #return udr,new_time
    conn.commit()
    conn.close()
    udr={'param1':delta_list[0],'param2':delta_list[1],'param3':delta_list[2],'param4':delta_list[3],'param5':delta_list[4]}
    return udr,new_time
Ejemplo n.º 4
0
def main(argv):
    print "Hello There. This is a simple test application making a test API call to OpenStack"
    auth_uri = 'http://160.85.4.64:5000' #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v2(auth_uri)
    if status:
        print 'The authentication was successful, below are the data we got:'
        print '--------------------------------------------------------------------------------------------------------'
        print '%1s %32s %2s %64s %1s' % ('|', 'key', '|', 'value', '|')
        print '--------------------------------------------------------------------------------------------------------'
        for key, value in token_data.iteritems():
            if key not in {'token-id'}:
                print '%1s %32s %2s %64s %1s' % ('|', key, '|', value, '|')
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token-id"]

    else:
        print "Authentication was not successful."
    if status:
        status, server_list = compute_api.get_server_list(token_data["token-id"], token_data["nova"])

        if status:
            print "The list of servers are printed next."
            print server_list
        status, meter_list = ceilometer_api.get_meter_list(token_data["token-id"], token_data["ceilometer"])
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % ('|','meter-name', '|', 'meter-type', '|', 'meter-unit', '|', 'meter-id', '|')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % ('|', meter_list[i]["meter-name"], '|', meter_list[i]["meter-type"], '|', meter_list[i]["meter-unit"], '|', meter_list[i]["meter-id"].strip(), '|')
            print '--------------------------------------------------------------------------------------------------------------------------'

            
            meter_name=raw_input("Enter meter name: ")
            st,stat_list=ceilometer_api.meter_statistics(meter_name, token_data["ceilometer"],token_data["token-id"],meter_list)
            if status:
                print "The statistics for your meters is printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'
            
                for i in range(len(stat_list)):
                    print "Average: " + str(stat_list[i]["average"]) 
                    print "Count: " + str(stat_list[i]["count"])
                    print "Duration: "+ str(stat_list[i]["duration"]) 
                    print "Duration end: " + str(stat_list[i]["duration-end"]) 
                    print "Duration start: "+ str(stat_list[i]["duration-start"]) 
                    print "Max: " + str(stat_list[i]["max"])
                    print "Min: " + str(stat_list[i]["min"]) 
                    print "Period: " + str(stat_list[i]["period"]) 
                    print "Period end: " + str(stat_list[i]["period-end"]) 
                    print "Period start: " + str(stat_list[i]["period-start"]) 
                    print "Sum: " + str(stat_list[i]["sum"]) 
                    print "Unit: " + str(stat_list[i]["unit"]) 
                    print "Group by: " + str(stat_list[i]["group-by"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'
        
            status,sample_list=ceilometer_api.get_meter_samples(meter_name, token_data["ceilometer"],token_data["token-id"],True,meter_list,False)
            if status:
                print '--------------------------------------------------------------------------------------------------------------------------'
                print "The samples for your meter are printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'
            
                for i in range(len(sample_list)):
                    print "Counter name: " + str(sample_list[i]["counter-name"]) 
                    print "Counter unit: " + str(sample_list[i]["counter-unit"])
                    print "Counter volume: "+ str(sample_list[i]["counter-volume"]) 
                    print "Counter type: " + str(sample_list[i]["counter-type"]) 
                    print "Message id: "+ str(sample_list[i]["message-id"]) 
                    print "Project id: " + str(sample_list[i]["project-id"])
                    print "Resource id: " + str(sample_list[i]["resource-id"]) 
                    print "Resource metadata: " 

                    print sample_list[i]["resource-metadata"]
                    print "Source: " + str(sample_list[i]["source"]) 
                    print "Timestamp: " + str(sample_list[i]["timestamp"]) 
                    print "User ID: " + str(sample_list[i]["user-id"]) 
                    print '--------------------------------------------------------------------------------------------------------------------------'    

            status,resources_list=ceilometer_api.get_resources(token_data["ceilometer"], token_data["token-id"],True)
            if status:
                print '--------------------------------------------------------------------------------------------------------------------------'
                print "The resources for your meter are printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'
            
                for i in range(len(resources_list)):
                    print "Resource id: " + str(resources_list[i]["resource-id"])
                    print "Links: " 
                    for j in range(len(resources_list[i]["links"])):
                        print "Href: " + str(resources_list[i]["links"][j]["href"])
                        print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                    print "Project id: " + str(resources_list[i]["project-id"])       
                    print "Resource metadata: " 
                    print resources_list[i]["metadata"]
                    #print "Source: " + str(resources_list[i]["source"]) 
                    
                    print "User ID: " + str(resources_list[i]["user-id"]) 
                    print '--------------------------------------------------------------------------------------------------------------------------'         
  
            resource_id=raw_input("Enter resource id: ")
            status,resources_list=ceilometer_api.get_resources_by_id(token_data["ceilometer"], token_data["token-id"],resource_id)
            if status:
                print '--------------------------------------------------------------------------------------------------------------------------'
                print "The resources for your meter are printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'
            
                for i in range(len(resources_list)):
                    print "Resource id: " + str(resources_list[i]["resource-id"])
                    print "Links: " 
                    for j in range(len(resources_list[i]["links"])):
                        print "Href: " + str(resources_list[i]["links"][j]["href"])
                        print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                        print "Project id: " + str(resources_list[i]["project-id"])
                 
                        print "Resource metadata: " 
                        print resources_list[i]["metadata"]
                        #print "Source: " + str(resources_list[i]["source"]) 
                        #print "First sample timestamp: " + str(resources_list[i]["first-sample-timestamp"]) 
                        #print "Last sample timestamp: " + str(resources_list[i]["last-sample-timestamp"]) 
                        print "User ID: " + str(resources_list[i]["user-id"]) 
                        print '--------------------------------------------------------------------------------------------------------------------------'         
              
    
    return True
Ejemplo n.º 5
0
def main(argv):
    print "Hello There. This is a simple test application making a test API call to OpenStack"
    auth_uri = 'http://160.85.4.64:5000'  #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v3(auth_uri, False)
    if status:
        print 'The authentication was successful, below are the data we got:'
        print '--------------------------------------------------------------------------------------------------------'
        print '%1s %32s %2s %64s %1s' % ('|', 'key', '|', 'value', '|')
        print '--------------------------------------------------------------------------------------------------------'
        for key, value in token_data.iteritems():
            if key not in {'token_id'}:
                print '%1s %32s %2s %64s %1s' % ('|', key, '|', value, '|')
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token_id"]
        pom = token_data["token_id"]

    else:
        print "Authentication was not successful."
    if status:
        status, tenant_list = keystone_api.get_list_tenants(pom, auth_uri)
        status, user_list = keystone_api.get_users_per_tenant(
            pom, auth_uri, '4e9c4e1b93124cdba2a930e98eb26ede')
        for i in range(len(user_list)):
            print '%20s %20s' % (user_list[i]["user_id"],
                                 user_list[i]["user_name"])

        #status, server_list = compute_api.get_server_list(token_data["token-id"], token_data["computev3"])
        #if status:
        #   print "The list of servers are printed next."
        #   print server_list
        status, meter_list = ceilometer_api.get_meter_list(
            pom, token_data["metering"])
        total_price = periodic.daily_count("2014-02-26", 1,
                                           "323936522894416b903d3528fa971537")
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % (
                '|', 'meter-name', '|', 'meter-type', '|', 'meter-unit', '|',
                'meter-id', '|', 'resource-id')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % (
                    '|', meter_list[i]["meter-name"], '|',
                    meter_list[i]["meter-type"], '|',
                    meter_list[i]["meter-unit"], '|',
                    meter_list[i]["meter-id"].strip(), '|',
                    meter_list[i]["resource-id"])
            print '--------------------------------------------------------------------------------------------------------------------------'

        meter_name = raw_input("Enter meter name: ")

        st, stat_list = ceilometer_api.meter_statistics(
            meter_name, token_data["metering"], pom, meter_list, False)
        if st:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The statistics for your meters is printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(stat_list)):
                print "Average: " + str(stat_list[i]["average"])
                print "Count: " + str(stat_list[i]["count"])
                print "Duration: " + str(stat_list[i]["duration"])
                print "Duration end: " + str(stat_list[i]["duration-end"])
                print "Duration start: " + str(stat_list[i]["duration-start"])
                print "Max: " + str(stat_list[i]["max"])
                print "Min: " + str(stat_list[i]["min"])
                print "Period: " + str(stat_list[i]["period"])
                print "Period end: " + str(stat_list[i]["period-end"])
                print "Period start: " + str(stat_list[i]["period-start"])
                print "Sum: " + str(stat_list[i]["sum"])
                print "Unit: " + str(stat_list[i]["unit"])
                print "Group by: " + str(stat_list[i]["group-by"])
                print '--------------------------------------------------------------------------------------------------------------------------'
            print '--------------------------------------------------------------------------------------------------------------------------'

        print "Query initialization for meter samples function."
        status, sample_list = ceilometer_api.get_meter_samples(
            meter_name, token_data["metering"], pom, True, meter_list, False,
            "")
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The samples for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(sample_list)):
                print "Counter name: " + str(sample_list[i]["counter-name"])
                print "Counter unit: " + str(sample_list[i]["counter-unit"])
                print "Counter volume: " + str(
                    sample_list[i]["counter-volume"])
                print "Counter type: " + str(sample_list[i]["counter-type"])
                print "Message id: " + str(sample_list[i]["message-id"])
                print "Project id: " + str(sample_list[i]["project-id"])
                print "Resource id: " + str(sample_list[i]["resource-id"])
                print "Resource metadata: "
                print sample_list[i]["resource-metadata"]
                print "Source: " + str(sample_list[i]["source"])
                print "Timestamp: " + str(sample_list[i]["timestamp"])
                print "User ID: " + str(sample_list[i]["user-id"])
                print '--------------------------------------------------------------------------------------------------------------------------'
            print '--------------------------------------------------------------------------------------------------------------------------'

        print "Query initialization for get resources function."
        status, resources_list = ceilometer_api.get_resources(
            token_data["metering"], pom, True)
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: "
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])

                print "Resource metadata: "
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"])

                print "User ID: " + str(resources_list[i]["user-id"])
                print '--------------------------------------------------------------------------------------------------------------------------'

        print "Query initialization for get resources by id function."
        resource_id = raw_input("Enter resource id: ")
        status, resources_list = ceilometer_api.get_resources_by_id(
            token_data["metering"], pom, resource_id)

        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'

            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: "
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])

                print "Resource metadata: "
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"])
                #print "First sample timestamp: " + str(resources_list[i]["first-sample-timestamp"])
                #print "Last sample timestamp: " + str(resources_list[i]["last-sample-timestamp"])

                print "User ID: " + str(resources_list[i]["user-id"])
                print '--------------------------------------------------------------------------------------------------------------------------'

    return True
Ejemplo n.º 6
0
def main(argv):
    print "Hello There. This is a simple test application making a test API call to OpenStack"
    auth_uri = 'http://160.85.4.64:5000' #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v3(auth_uri,False)
    if status:
        print 'The authentication was successful, below are the data we got:'
        print '--------------------------------------------------------------------------------------------------------'
        print '%1s %32s %2s %64s %1s' % ('|', 'key', '|', 'value', '|')
        print '--------------------------------------------------------------------------------------------------------'
        for key, value in token_data.iteritems():
            if key not in {'token_id'}:
                print '%1s %32s %2s %64s %1s' % ('|', key, '|', value, '|')
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token_id"]
        pom=token_data["token_id"]

    else:
        print "Authentication was not successful."
    if status:
        status,tenant_list=keystone_api.get_list_tenants(pom,auth_uri)
        status,user_list=keystone_api.get_users_per_tenant(pom,auth_uri,'4e9c4e1b93124cdba2a930e98eb26ede')
        for i in range(len(user_list)):
            print '%20s %20s' %(user_list[i]["user_id"], user_list[i]["user_name"])
            
        #status, server_list = compute_api.get_server_list(token_data["token-id"], token_data["computev3"])
        #if status:
         #   print "The list of servers are printed next."
         #   print server_list
        status, meter_list = ceilometer_api.get_meter_list(pom, token_data["metering"])
        total_price=periodic.daily_count("2014-02-26",1, "323936522894416b903d3528fa971537")
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % ('|','meter-name', '|', 'meter-type', '|', 'meter-unit', '|', 'meter-id', '|','resource-id')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %40s %1s %70s' % ('|', meter_list[i]["meter-name"], '|', meter_list[i]["meter-type"], '|', meter_list[i]["meter-unit"], '|', meter_list[i]["meter-id"].strip(), '|',meter_list[i]["resource-id"])
            print '--------------------------------------------------------------------------------------------------------------------------'
 
        meter_name=raw_input("Enter meter name: ")

        st,stat_list=ceilometer_api.meter_statistics(meter_name, token_data["metering"],pom,meter_list,False)
        if st:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The statistics for your meters is printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(stat_list)):
                print "Average: " + str(stat_list[i]["average"]) 
                print "Count: " + str(stat_list[i]["count"])
                print "Duration: "+ str(stat_list[i]["duration"]) 
                print "Duration end: " + str(stat_list[i]["duration-end"]) 
                print "Duration start: "+ str(stat_list[i]["duration-start"]) 
                print "Max: " + str(stat_list[i]["max"])
                print "Min: " + str(stat_list[i]["min"]) 
                print "Period: " + str(stat_list[i]["period"]) 
                print "Period end: " + str(stat_list[i]["period-end"]) 
                print "Period start: " + str(stat_list[i]["period-start"]) 
                print "Sum: " + str(stat_list[i]["sum"]) 
                print "Unit: " + str(stat_list[i]["unit"]) 
                print "Group by: " + str(stat_list[i]["group-by"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
            print '--------------------------------------------------------------------------------------------------------------------------'
        
        print "Query initialization for meter samples function."
        status,sample_list=ceilometer_api.get_meter_samples(meter_name,token_data["metering"],pom,True,meter_list,False,"")
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The samples for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(sample_list)):
                print "Counter name: " + str(sample_list[i]["counter-name"]) 
                print "Counter unit: " + str(sample_list[i]["counter-unit"])
                print "Counter volume: "+ str(sample_list[i]["counter-volume"]) 
                print "Counter type: " + str(sample_list[i]["counter-type"]) 
                print "Message id: "+ str(sample_list[i]["message-id"]) 
                print "Project id: " + str(sample_list[i]["project-id"])
                print "Resource id: " + str(sample_list[i]["resource-id"]) 
                print "Resource metadata: " 
                print sample_list[i]["resource-metadata"]
                print "Source: " + str(sample_list[i]["source"]) 
                print "Timestamp: " + str(sample_list[i]["timestamp"]) 
                print "User ID: " + str(sample_list[i]["user-id"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
            print '--------------------------------------------------------------------------------------------------------------------------'
        
        print "Query initialization for get resources function."
        status,resources_list=ceilometer_api.get_resources(token_data["metering"], pom,True)
        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: " 
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])
                 
                print "Resource metadata: " 
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"]) 

                print "User ID: " + str(resources_list[i]["user-id"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
        
        print "Query initialization for get resources by id function."    
        resource_id=raw_input("Enter resource id: ")
        status,resources_list=ceilometer_api.get_resources_by_id(token_data["metering"], pom,resource_id)

        if status:
            print '--------------------------------------------------------------------------------------------------------------------------'
            print "The resources for your meter are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            
            for i in range(len(resources_list)):
                print "Resource id: " + str(resources_list[i]["resource-id"])
                print "Links: " 
                for j in range(len(resources_list[i]["links"])):
                    print "Href: " + str(resources_list[i]["links"][j]["href"])
                    print "Rel: " + str(resources_list[i]["links"][j]["rel"])
                print "Project id: " + str(resources_list[i]["project-id"])
                 
                print "Resource metadata: " 
                print resources_list[i]["metadata"]
                #print "Source: " + str(resources_list[i]["source"]) 
                #print "First sample timestamp: " + str(resources_list[i]["first-sample-timestamp"]) 
                #print "Last sample timestamp: " + str(resources_list[i]["last-sample-timestamp"]) 

                print "User ID: " + str(resources_list[i]["user-id"]) 
                print '--------------------------------------------------------------------------------------------------------------------------'         
        
        
    return True
Ejemplo n.º 7
0
def main(argv):
    print "Hello There. This is a simple test application making a test API call to OpenStack"
    auth_uri = 'http://160.85.4.64:5000'  #internal test-setup, replace it with your own value
    status, token_data = keystone_api.get_token_v2(auth_uri)
    if status:
        print 'The authentication was successful, below are the data we got:'
        print '--------------------------------------------------------------------------------------------------------'
        print '%1s %32s %2s %64s %1s' % ('|', 'key', '|', 'value', '|')
        print '--------------------------------------------------------------------------------------------------------'
        for key, value in token_data.iteritems():
            if key not in {'token-id'}:
                print '%1s %32s %2s %64s %1s' % ('|', key, '|', value, '|')
        print '--------------------------------------------------------------------------------------------------------'
        print 'The authentication token is: ', token_data["token-id"]

    else:
        print "Authentication was not successful."
    if status:
        status, server_list = compute_api.get_server_list(
            token_data["token-id"], token_data["nova"])

        if status:
            print "The list of servers are printed next."
            print server_list
        status, meter_list = ceilometer_api.get_meter_list(
            token_data["token-id"], token_data["ceilometer"])
        if status:
            print "The list of available meters are printed next."
            print '--------------------------------------------------------------------------------------------------------------------------'
            print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % (
                '|', 'meter-name', '|', 'meter-type', '|', 'meter-unit', '|',
                'meter-id', '|')
            print '--------------------------------------------------------------------------------------------------------------------------'
            for i in range(len(meter_list)):
                print '%1s %16s %2s %10s %2s %10s %2s %70s %1s' % (
                    '|', meter_list[i]["meter-name"], '|',
                    meter_list[i]["meter-type"], '|',
                    meter_list[i]["meter-unit"], '|',
                    meter_list[i]["meter-id"].strip(), '|')
            print '--------------------------------------------------------------------------------------------------------------------------'

            meter_name = raw_input("Enter meter name: ")
            st, stat_list = ceilometer_api.meter_statistics(
                meter_name, token_data["ceilometer"], token_data["token-id"],
                meter_list)
            if status:
                print "The statistics for your meters is printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'

                for i in range(len(stat_list)):
                    print "Average: " + str(stat_list[i]["average"])
                    print "Count: " + str(stat_list[i]["count"])
                    print "Duration: " + str(stat_list[i]["duration"])
                    print "Duration end: " + str(stat_list[i]["duration-end"])
                    print "Duration start: " + str(
                        stat_list[i]["duration-start"])
                    print "Max: " + str(stat_list[i]["max"])
                    print "Min: " + str(stat_list[i]["min"])
                    print "Period: " + str(stat_list[i]["period"])
                    print "Period end: " + str(stat_list[i]["period-end"])
                    print "Period start: " + str(stat_list[i]["period-start"])
                    print "Sum: " + str(stat_list[i]["sum"])
                    print "Unit: " + str(stat_list[i]["unit"])
                    print "Group by: " + str(stat_list[i]["group-by"])
                print '--------------------------------------------------------------------------------------------------------------------------'

            status, sample_list = ceilometer_api.get_meter_samples(
                meter_name, token_data["ceilometer"], token_data["token-id"],
                True, meter_list, False)
            if status:
                print '--------------------------------------------------------------------------------------------------------------------------'
                print "The samples for your meter are printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'

                for i in range(len(sample_list)):
                    print "Counter name: " + str(
                        sample_list[i]["counter-name"])
                    print "Counter unit: " + str(
                        sample_list[i]["counter-unit"])
                    print "Counter volume: " + str(
                        sample_list[i]["counter-volume"])
                    print "Counter type: " + str(
                        sample_list[i]["counter-type"])
                    print "Message id: " + str(sample_list[i]["message-id"])
                    print "Project id: " + str(sample_list[i]["project-id"])
                    print "Resource id: " + str(sample_list[i]["resource-id"])
                    print "Resource metadata: "

                    print sample_list[i]["resource-metadata"]
                    print "Source: " + str(sample_list[i]["source"])
                    print "Timestamp: " + str(sample_list[i]["timestamp"])
                    print "User ID: " + str(sample_list[i]["user-id"])
                    print '--------------------------------------------------------------------------------------------------------------------------'

            status, resources_list = ceilometer_api.get_resources(
                token_data["ceilometer"], token_data["token-id"], True)
            if status:
                print '--------------------------------------------------------------------------------------------------------------------------'
                print "The resources for your meter are printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'

                for i in range(len(resources_list)):
                    print "Resource id: " + str(
                        resources_list[i]["resource-id"])
                    print "Links: "
                    for j in range(len(resources_list[i]["links"])):
                        print "Href: " + str(
                            resources_list[i]["links"][j]["href"])
                        print "Rel: " + str(
                            resources_list[i]["links"][j]["rel"])
                    print "Project id: " + str(resources_list[i]["project-id"])
                    print "Resource metadata: "
                    print resources_list[i]["metadata"]
                    #print "Source: " + str(resources_list[i]["source"])

                    print "User ID: " + str(resources_list[i]["user-id"])
                    print '--------------------------------------------------------------------------------------------------------------------------'

            resource_id = raw_input("Enter resource id: ")
            status, resources_list = ceilometer_api.get_resources_by_id(
                token_data["ceilometer"], token_data["token-id"], resource_id)
            if status:
                print '--------------------------------------------------------------------------------------------------------------------------'
                print "The resources for your meter are printed next."
                print '--------------------------------------------------------------------------------------------------------------------------'

                for i in range(len(resources_list)):
                    print "Resource id: " + str(
                        resources_list[i]["resource-id"])
                    print "Links: "
                    for j in range(len(resources_list[i]["links"])):
                        print "Href: " + str(
                            resources_list[i]["links"][j]["href"])
                        print "Rel: " + str(
                            resources_list[i]["links"][j]["rel"])
                        print "Project id: " + str(
                            resources_list[i]["project-id"])

                        print "Resource metadata: "
                        print resources_list[i]["metadata"]
                        #print "Source: " + str(resources_list[i]["source"])
                        #print "First sample timestamp: " + str(resources_list[i]["first-sample-timestamp"])
                        #print "Last sample timestamp: " + str(resources_list[i]["last-sample-timestamp"])
                        print "User ID: " + str(resources_list[i]["user-id"])
                        print '--------------------------------------------------------------------------------------------------------------------------'

    return True