<s> import socket <EOL> import requests <EOL> import urllib2 <EOL> import json <EOL> import msgpack <EOL> import collections <EOL> import time , thread , threading <EOL> from flask import request , Request , jsonify <EOL> from flask import Flask <EOL> from flask import make_response <EOL> app = Flask ( __name__ ) <EOL> projects_map = { } <EOL> xos_tenant_info_map = { } <EOL> xos_instances_info_map = { } <EOL> use_kafka = True <EOL> if use_kafka : <EOL> import kafka <EOL> from kafka import TopicPartition <EOL> else : <EOL> UDP_IP = "<STR_LIT>" <EOL> UDP_PORT = <NUM_LIT> <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> def autoscaledata ( ) : <EOL> response = app . make_response ( json . dumps ( projects_map . values ( ) ) ) <EOL> response . mimetype = "<STR_LIT:application/json>" <EOL> return response <EOL> def acquire_xos_monitoring_channel ( ) : <EOL> url = "<STR_LIT>" <EOL> admin_auth = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> monitoring_channels = requests . get ( url , auth = admin_auth ) . json ( ) <EOL> ceilometer_url = None <EOL> if not monitoring_channels : <EOL> print '<STR_LIT>' <EOL> return None <EOL> else : <EOL> monitoring_channel = monitoring_channels [ <NUM_LIT:0> ] <EOL> while not monitoring_channel [ '<STR_LIT>' ] : <EOL> print '<STR_LIT>' <EOL> sleep ( <NUM_LIT:0.5> ) <EOL> monitoring_channel = requests . get ( url , auth = admin_auth ) . json ( ) [ <NUM_LIT:0> ] <EOL> while True : <EOL> print '<STR_LIT>' % monitoring_channel [ '<STR_LIT>' ] <EOL> try : <EOL> response = urllib2 . urlopen ( monitoring_channel [ '<STR_LIT>' ] , timeout = <NUM_LIT:1> ) <EOL> break <EOL> except urllib2 . HTTPError , e : <EOL> print '<STR_LIT>' % e . reason <EOL> break <EOL> except urllib2 . URLError , e : <EOL> print '<STR_LIT>' % e . reason <EOL> pass <EOL> return monitoring_channel <EOL> def print_samples ( ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for project in projects_map . keys ( ) : <EOL> print "<STR_LIT>" % ( projects_map [ project ] [ '<STR_LIT>' ] , projects_map [ project ] [ '<STR_LIT>' ] if projects_map [ project ] [ '<STR_LIT>' ] else project , projects_map [ project ] [ '<STR_LIT>' ] ) <EOL> for resource in projects_map [ project ] [ '<STR_LIT>' ] . keys ( ) : <EOL> print "<STR_LIT>" % ( projects_map [ project ] [ '<STR_LIT>' ] [ resource ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] if projects_map [ project ] [ '<STR_LIT>' ] [ resource ] [ '<STR_LIT>' ] else resource ) <EOL> for i in projects_map [ project ] [ '<STR_LIT>' ] [ resource ] [ '<STR_LIT>' ] : <EOL> print "<STR_LIT>" % ( i [ '<STR_LIT>' ] , i [ '<STR_LIT>' ] ) <EOL> def periodic_print ( ) : <EOL> print_samples ( ) <EOL> threading . Timer ( <NUM_LIT:20> , periodic_print ) . start ( ) <EOL> CPU_UPPER_THRESHOLD = <NUM_LIT> <EOL> CPU_LOWER_THRESHOLD = <NUM_LIT:30> <EOL> CPU_THRESHOLD_REPEAT = <NUM_LIT:3> <EOL> INITIAL_STATE = '<STR_LIT>' <EOL> SCALE_UP_EVALUATION = '<STR_LIT>' <EOL> SCALE_DOWN_EVALUATION = '<STR_LIT>' <EOL> SCALE_UP_ALARM = '<STR_LIT>' <EOL> SCALE_DOWN_ALARM = '<STR_LIT>' <EOL> def loadAllXosTenantInfo ( ) : <EOL> print "<STR_LIT>" <EOL> url = "<STR_LIT>" <EOL> admin_auth = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> controller_slices = requests . get ( url , auth = admin_auth ) . json ( ) <EOL> for cslice in controller_slices : <EOL> slice = requests . get ( cslice [ '<STR_LIT>' ] , auth = admin_auth ) . json ( ) <EOL> slice_name = slice [ '<STR_LIT>' ] <EOL> if slice [ '<STR_LIT>' ] : <EOL> service = requests . get ( slice [ '<STR_LIT>' ] , auth = admin_auth ) . json ( ) <EOL> service_name = service [ '<STR_LIT>' ] <EOL> else : <EOL> service_name = None <EOL> xos_tenant_info_map [ cslice [ '<STR_LIT>' ] ] = { '<STR_LIT>' : service_name , '<STR_LIT>' : slice_name } <EOL> print "<STR_LIT>" % ( cslice [ '<STR_LIT>' ] , service_name , slice_name ) <EOL> def loadAllXosInstanceInfo ( ) : <EOL> print "<STR_LIT>" <EOL> url = "<STR_LIT>" <EOL> admin_auth = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> xos_instances = requests . get ( url , auth = admin_auth ) . json ( ) <EOL> for instance in xos_instances : <EOL> xos_instances_info_map [ instance [ '<STR_LIT>' ] ] = { '<STR_LIT>' : instance [ '<STR_LIT>' ] } <EOL> def getXosTenantInfo ( project ) : <EOL> xos_tenant_info = xos_tenant_info_map . get ( project , None ) <EOL> if xos_tenant_info : <EOL> return xos_tenant_info <EOL> else : <EOL> loadAllXosTenantInfo ( ) <EOL> xos_tenant_info = xos_tenant_info_map . get ( project , None ) <EOL> if not xos_tenant_info : <EOL> print "<STR_LIT>" % project <EOL> return xos_tenant_info <EOL> def getXosInstanceInfo ( resource ) : <EOL> xos_instance_info = xos_instances_info_map . get ( resource , None ) <EOL> if xos_instance_info : <EOL> return xos_instance_info <EOL> else : <EOL> loadAllXosInstanceInfo ( ) <EOL> xos_instance_info = xos_instances_info_map . get ( resource , None ) <EOL> if not xos_instance_info : <EOL> print "<STR_LIT>" % project <EOL> return xos_instance_info <EOL> def handle_adjust_scale ( project , adjust ) : <EOL> if ( adjust != '<STR_LIT>' ) and ( adjust != '<STR_LIT>' ) : <EOL> print "<STR_LIT>" % adjust <EOL> return <EOL> current_instances = len ( projects_map [ project ] [ '<STR_LIT>' ] . keys ( ) ) <EOL> if ( current_instances <= <NUM_LIT:1> and adjust == '<STR_LIT>' ) : <EOL> print "<STR_LIT>" % project <EOL> return <EOL> if ( current_instances >= <NUM_LIT:2> and adjust == '<STR_LIT>' ) : <EOL> print "<STR_LIT>" % project <EOL> return <EOL> xos_service = projects_map [ project ] [ '<STR_LIT>' ] <EOL> xos_slice = projects_map [ project ] [ '<STR_LIT>' ] <EOL> if not xos_service or not xos_slice : <EOL> print "<STR_LIT>" % project <EOL> return <EOL> print "<STR_LIT>" % ( adjust , project , xos_slice , xos_service , current_instances , current_instances + <NUM_LIT:1> if ( adjust == '<STR_LIT>' ) else current_instances - <NUM_LIT:1> ) <EOL> query_params = { '<STR_LIT>' : xos_service , '<STR_LIT>' : xos_slice , '<STR_LIT>' : current_instances + <NUM_LIT:1> if ( adjust == '<STR_LIT>' ) else current_instances - <NUM_LIT:1> } <EOL> url = "<STR_LIT>" <EOL> admin_auth = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> response = requests . get ( url , params = query_params , auth = admin_auth ) . json ( ) <EOL> print "<STR_LIT>" % response <EOL> def periodic_cpu_threshold_evaluator ( ) : <EOL> for project in projects_map . keys ( ) : <EOL> aggregate_cpu_util = sum ( [ resource [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] [ '<STR_LIT>' ] for resource in projects_map [ project ] [ '<STR_LIT>' ] . values ( ) ] ) / len ( projects_map [ project ] [ '<STR_LIT>' ] . keys ( ) ) <EOL> if ( projects_map [ project ] [ '<STR_LIT>' ] == INITIAL_STATE or <EOL> projects_map [ project ] [ '<STR_LIT>' ] == SCALE_UP_ALARM or <EOL> projects_map [ project ] [ '<STR_LIT>' ] == SCALE_DOWN_ALARM ) : <EOL> if aggregate_cpu_util > CPU_UPPER_THRESHOLD : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_UP_EVALUATION <EOL> if projects_map [ project ] [ '<STR_LIT>' ] >= CPU_THRESHOLD_REPEAT : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_UP_ALARM <EOL> handle_adjust_scale ( project , '<STR_LIT>' ) <EOL> elif aggregate_cpu_util < CPU_LOWER_THRESHOLD : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_DOWN_EVALUATION <EOL> if projects_map [ project ] [ '<STR_LIT>' ] >= CPU_THRESHOLD_REPEAT : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_DOWN_ALARM <EOL> handle_adjust_scale ( project , '<STR_LIT>' ) <EOL> else : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = INITIAL_STATE <EOL> elif projects_map [ project ] [ '<STR_LIT>' ] == SCALE_UP_EVALUATION : <EOL> if aggregate_cpu_util > CPU_UPPER_THRESHOLD : <EOL> projects_map [ project ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if projects_map [ project ] [ '<STR_LIT>' ] >= CPU_THRESHOLD_REPEAT : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_UP_ALARM <EOL> handle_adjust_scale ( project , '<STR_LIT>' ) <EOL> elif aggregate_cpu_util < CPU_LOWER_THRESHOLD : <EOL> projects_map [ project ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_DOWN_EVALUATION <EOL> else : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = INITIAL_STATE <EOL> elif projects_map [ project ] [ '<STR_LIT>' ] == SCALE_DOWN_EVALUATION : <EOL> if aggregate_cpu_util < CPU_LOWER_THRESHOLD : <EOL> projects_map [ project ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if projects_map [ project ] [ '<STR_LIT>' ] >= CPU_THRESHOLD_REPEAT : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_DOWN_ALARM <EOL> handle_adjust_scale ( project , '<STR_LIT>' ) <EOL> elif aggregate_cpu_util > CPU_UPPER_THRESHOLD : <EOL> projects_map [ project ] [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = SCALE_UP_EVALUATION <EOL> else : <EOL> projects_map [ project ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> projects_map [ project ] [ '<STR_LIT>' ] = INITIAL_STATE <EOL> threading . Timer ( <NUM_LIT:20> , periodic_cpu_threshold_evaluator ) . start ( ) <EOL> def read_notification_from_ceilometer_over_kafka ( host , port , topic ) : <EOL> print "<STR_LIT>" , host , port , topic <EOL> try : <EOL> consumer = kafka . KafkaConsumer ( bootstrap_servers = [ "<STR_LIT>" % ( host , port ) ] ) <EOL> consumer . assign ( [ TopicPartition ( topic , <NUM_LIT:0> ) ] ) <EOL> consumer . seek_to_end ( ) <EOL> for message in consumer : <EOL> process_notification_from_ceilometer ( json . loads ( message . value ) ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" , e <EOL> def read_notification_from_ceilometer ( host , port ) : <EOL> udp = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> udp . bind ( ( host , port ) ) <EOL> while True : <EOL> data , source = udp . recvfrom ( <NUM_LIT> ) <EOL> try : <EOL> sample = msgpack . loads ( data , encoding = '<STR_LIT:utf-8>' ) <EOL> process_notification_from_ceilometer ( sample ) <EOL> except Exception as e : <EOL> print e <EOL> def process_notification_from_ceilometer ( sample ) : <EOL> if sample [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if '<STR_LIT>' in sample [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> xosTenantInfo = getXosTenantInfo ( sample [ '<STR_LIT>' ] ) <EOL> xosResourceInfo = getXosInstanceInfo ( sample [ '<STR_LIT>' ] ) <EOL> print "<STR_LIT>" % ( xosTenantInfo [ '<STR_LIT>' ] if xosTenantInfo [ '<STR_LIT>' ] else sample [ '<STR_LIT>' ] , xosResourceInfo ) <EOL> if sample [ '<STR_LIT>' ] not in projects_map . keys ( ) : <EOL> return <EOL> if sample [ '<STR_LIT>' ] not in projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] . keys ( ) : <EOL> return <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] . pop ( sample [ '<STR_LIT>' ] , None ) <EOL> return <EOL> elif sample [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> return <EOL> if sample [ '<STR_LIT>' ] not in projects_map . keys ( ) : <EOL> projects_map [ sample [ '<STR_LIT>' ] ] = { } <EOL> xosTenantInfo = getXosTenantInfo ( sample [ '<STR_LIT>' ] ) <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = sample [ '<STR_LIT>' ] <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = xosTenantInfo [ '<STR_LIT>' ] <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = xosTenantInfo [ '<STR_LIT>' ] <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = { } <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = INITIAL_STATE <EOL> resource_map = projects_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] <EOL> if sample [ '<STR_LIT>' ] not in resource_map . keys ( ) : <EOL> resource_map [ sample [ '<STR_LIT>' ] ] = { } <EOL> resource_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = getXosInstanceInfo ( sample [ '<STR_LIT>' ] ) <EOL> resource_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = [ ] <EOL> samples_queue = resource_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] <EOL> sample = { '<STR_LIT>' : sample [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : sample [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : sample [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : sample [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : sample [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : sample [ '<STR_LIT>' ] } <EOL> deque = collections . deque ( samples_queue , maxlen = <NUM_LIT:10> ) <EOL> deque . append ( sample ) <EOL> resource_map [ sample [ '<STR_LIT>' ] ] [ '<STR_LIT>' ] = list ( deque ) <EOL> def setup_webserver ( ) : <EOL> try : <EOL> webserver_host = '<STR_LIT>' <EOL> webserver_port = <NUM_LIT> <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" , e . __str__ ( ) ) <EOL> logging . error ( "<STR_LIT>" , e . __str__ ( ) ) <EOL> else : <EOL> app . run ( host = webserver_host , port = webserver_port , debug = True , use_reloader = False ) <EOL> def main ( ) : <EOL> monitoring_channel = acquire_xos_monitoring_channel ( ) <EOL> if not monitoring_channel : <EOL> print '<STR_LIT>' <EOL> return <EOL> loadAllXosTenantInfo ( ) <EOL> loadAllXosInstanceInfo ( ) <EOL> ceilometer_url = monitoring_channel [ '<STR_LIT>' ] <EOL> if use_kafka : <EOL> thread . start_new ( read_notification_from_ceilometer_over_kafka , ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , ) ) <EOL> subscribe_data = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:target>" : "<STR_LIT>" } <EOL> else : <EOL> thread . start_new ( read_notification_from_ceilometer , ( UDP_IP , UDP_PORT , ) ) <EOL> subscribe_data = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:target>" : "<STR_LIT>" } <EOL> subscribe_url = ceilometer_url + '<STR_LIT>' <EOL> response = requests . post ( subscribe_url , data = json . dumps ( subscribe_data ) ) <EOL> print '<STR_LIT>' % response . text <EOL> if ( not '<STR_LIT>' in response . text ) and ( not '<STR_LIT>' in response . text ) : <EOL> print '<STR_LIT>' <EOL> return <EOL> subscribe_data = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:target>" : "<STR_LIT>" } <EOL> subscribe_url = ceilometer_url + '<STR_LIT>' <EOL> response = requests . post ( subscribe_url , data = json . dumps ( subscribe_data ) ) <EOL> print '<STR_LIT>' % response . text <EOL> if ( not '<STR_LIT>' in response . text ) and ( not '<STR_LIT>' in response . text ) : <EOL> print '<STR_LIT>' <EOL> return <EOL> periodic_cpu_threshold_evaluator ( ) <EOL> periodic_print ( ) <EOL> setup_webserver ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
