Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 1 | import socket |
| 2 | import requests |
| 3 | import urllib2 |
| 4 | import json |
| 5 | import msgpack |
| 6 | import collections |
| 7 | import time, thread, threading |
| 8 | |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 9 | from flask import request, Request, jsonify |
| 10 | from flask import Flask |
| 11 | from flask import make_response |
| 12 | app = Flask(__name__) |
| 13 | |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 14 | projects_map = {} |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 15 | xos_tenant_info_map = {} |
| 16 | xos_instances_info_map = {} |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 17 | |
| 18 | UDP_IP = "0.0.0.0" |
| 19 | UDP_PORT = 12346 |
| 20 | |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 21 | @app.route('/autoscaledata',methods=['GET']) |
| 22 | def autoscaledata(): |
teone | 46c13ab | 2015-12-16 18:28:14 -0500 | [diff] [blame] | 23 | response = app.make_response(json.dumps(projects_map.values())) |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 24 | response.mimetype="application/json" |
| 25 | return response |
| 26 | |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 27 | def acquire_xos_monitoring_channel(): |
| 28 | url = "http://ctl:9999/xoslib/monitoringchannel/" |
| 29 | admin_auth=("padmin@vicci.org", "letmein") # use your XOS username and password |
| 30 | monitoring_channels = requests.get(url, auth=admin_auth).json() |
| 31 | ceilometer_url = None |
| 32 | if not monitoring_channels: |
| 33 | print 'SRIKANTH: No monitoring channels for this user...' |
| 34 | return None |
| 35 | else: |
| 36 | monitoring_channel = monitoring_channels[0] |
| 37 | while not monitoring_channel['ceilometer_url']: |
| 38 | print 'SRIKANTH: Waiting for monitoring channel create' |
| 39 | sleep(0.5) |
| 40 | monitoring_channel = requests.get(url, auth=admin_auth).json()[0] |
| 41 | #TODO: Wait until URL is completely UP |
| 42 | while True: |
| 43 | print 'SRIKANTH: Waiting for ceilometer proxy URL %s is available' % monitoring_channel['ceilometer_url'] |
| 44 | try: |
| 45 | response = urllib2.urlopen(monitoring_channel['ceilometer_url'],timeout=1) |
| 46 | break |
| 47 | except urllib2.HTTPError, e: |
| 48 | print 'SRIKANTH: HTTP error %s' % e.reason |
| 49 | break |
| 50 | except urllib2.URLError, e: |
Srikanth Vavilapalli | e1a34e8 | 2015-12-17 16:58:09 -0600 | [diff] [blame^] | 51 | print 'SRIKANTH: URL error %s' % e.reason |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 52 | pass |
| 53 | return monitoring_channel |
| 54 | |
| 55 | def print_samples(): |
| 56 | print "" |
| 57 | print "" |
| 58 | for project in projects_map.keys(): |
teone | 46c13ab | 2015-12-16 18:28:14 -0500 | [diff] [blame] | 59 | print "service=%s slice=%s, alarm_state=%s" % (projects_map[project]['service'], projects_map[project]['slice'] if projects_map[project]['slice'] else project, projects_map[project]['alarm']) |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 60 | for resource in projects_map[project]['resources'].keys(): |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 61 | print "resource=%s" % (projects_map[project]['resources'][resource]['xos_instance_info']['instance_name'] if projects_map[project]['resources'][resource]['xos_instance_info'] else resource) |
| 62 | for i in projects_map[project]['resources'][resource]['queue']: |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 63 | print " time=%s val=%s" % ( i['timestamp'],i['counter_volume']) |
| 64 | |
| 65 | def periodic_print(): |
| 66 | print_samples() |
| 67 | #Print every 1minute |
Srikanth Vavilapalli | e1a34e8 | 2015-12-17 16:58:09 -0600 | [diff] [blame^] | 68 | threading.Timer(20, periodic_print).start() |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 69 | |
| 70 | |
| 71 | CPU_UPPER_THRESHOLD = 80 #80% |
| 72 | CPU_LOWER_THRESHOLD = 30 #30% |
| 73 | CPU_THRESHOLD_REPEAT = 3 |
| 74 | INITIAL_STATE = 'normal_config' |
| 75 | SCALE_UP_EVALUATION = 'scale_up_eval' |
| 76 | SCALE_DOWN_EVALUATION = 'scale_down_eval' |
| 77 | SCALE_UP_ALARM = 'scale_up' |
| 78 | SCALE_DOWN_ALARM = 'scale_down' |
| 79 | |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 80 | def loadAllXosTenantInfo(): |
| 81 | print "SRIKANTH: Loading all XOS tenant info" |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 82 | url = "http://ctl:9999/xos/controllerslices/" |
| 83 | admin_auth=("padmin@vicci.org", "letmein") # use your XOS username and password |
| 84 | controller_slices = requests.get(url, auth=admin_auth).json() |
| 85 | for cslice in controller_slices: |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 86 | slice = requests.get(cslice['slice'], auth=admin_auth).json() |
| 87 | slice_name = slice['humanReadableName'] |
| 88 | if slice['service']: |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 89 | service = requests.get(slice['service'], auth=admin_auth).json() |
| 90 | service_name = service['humanReadableName'] |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 91 | else: |
| 92 | service_name = None |
| 93 | xos_tenant_info_map[cslice['tenant_id']] = {'service':service_name, 'slice':slice_name} |
| 94 | print "SRIKANTH: Project: %s Service:%s Slice:%s" % (cslice['tenant_id'],service_name,slice_name) |
| 95 | |
| 96 | def loadAllXosInstanceInfo(): |
| 97 | print "SRIKANTH: Loading all XOS instance info" |
teone | 46c13ab | 2015-12-16 18:28:14 -0500 | [diff] [blame] | 98 | url = "http://ctl:9999/xos/instances/" |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 99 | admin_auth=("padmin@vicci.org", "letmein") # use your XOS username and password |
| 100 | xos_instances = requests.get(url, auth=admin_auth).json() |
| 101 | for instance in xos_instances: |
| 102 | xos_instances_info_map[instance['instance_uuid']] = {'instance_name':instance['instance_name']} |
| 103 | |
| 104 | def getXosTenantInfo(project): |
| 105 | xos_tenant_info = xos_tenant_info_map.get(project, None) |
| 106 | if xos_tenant_info: |
| 107 | return xos_tenant_info |
| 108 | else: |
| 109 | loadAllXosTenantInfo() |
| 110 | xos_tenant_info = xos_tenant_info_map.get(project, None) |
| 111 | if not xos_tenant_info: |
| 112 | print "SRIKANTH: Project %s has no associated XOS slice" % project |
| 113 | return xos_tenant_info |
| 114 | |
| 115 | def getXosInstanceInfo(resource): |
| 116 | xos_instance_info = xos_instances_info_map.get(resource, None) |
| 117 | if xos_instance_info: |
| 118 | return xos_instance_info |
| 119 | else: |
| 120 | loadAllXosInstanceInfo() |
| 121 | xos_instance_info = xos_instances_info_map.get(resource, None) |
| 122 | if not xos_instance_info: |
| 123 | print "SRIKANTH: Resource %s has no associated XOS instance" % project |
| 124 | return xos_instance_info |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 125 | |
| 126 | def handle_adjust_scale(project, adjust): |
| 127 | if (adjust != 'up') and (adjust != 'down'): |
| 128 | print "SRIKANTH: Invalid adjust value %s " % adjust |
| 129 | return |
| 130 | current_instances = len(projects_map[project]['resources'].keys()) |
| 131 | if (current_instances <=1 and adjust == 'down'): |
| 132 | print "SRIKANTH: %s is running with already minimum instances and can not scale down further " % project |
| 133 | return |
| 134 | if (current_instances >=2 and adjust == 'up'): |
| 135 | print "SRIKANTH: %s is running with already maximum instances and can not scale up further " % project |
| 136 | return |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 137 | #xos_tenant = getXosTenantInfo(project) |
teone | 46c13ab | 2015-12-16 18:28:14 -0500 | [diff] [blame] | 138 | xos_service = projects_map[project]['service'] |
| 139 | xos_slice = projects_map[project]['slice'] |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 140 | if not xos_service or not xos_slice: |
| 141 | print "SRIKANTH: Can not handle adjust_scale for Project %s because not associated with any service or slice" % project |
| 142 | return |
| 143 | print "SRIKANTH: SCALE %s for Project %s, Slice=%s, Service=%s from current=%d to new=%d" % (adjust, project, xos_slice, xos_service, current_instances, current_instances+1 if (adjust=='up') else current_instances-1) |
| 144 | query_params = {'service':xos_service, 'slice_hint':xos_slice, 'scale':current_instances+1 if (adjust=='up') else current_instances-1} |
| 145 | url = "http://ctl:9999/xoslib/serviceadjustscale/" |
| 146 | admin_auth=("padmin@vicci.org", "letmein") # use your XOS username and password |
| 147 | response = requests.get(url, params=query_params, auth=admin_auth).json() |
| 148 | print "SRIKANTH: XOS adjust_scale response: %s" % response |
| 149 | |
| 150 | def periodic_cpu_threshold_evaluator(): |
| 151 | for project in projects_map.keys(): |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 152 | aggregate_cpu_util = sum([resource['queue'][-1]['counter_volume'] \ |
| 153 | for resource in projects_map[project]['resources'].values()]) \ |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 154 | /len(projects_map[project]['resources'].keys()) |
| 155 | |
| 156 | if (projects_map[project]['alarm'] == INITIAL_STATE or |
| 157 | projects_map[project]['alarm'] == SCALE_UP_ALARM or |
| 158 | projects_map[project]['alarm'] == SCALE_DOWN_ALARM): |
| 159 | if aggregate_cpu_util > CPU_UPPER_THRESHOLD: |
| 160 | projects_map[project]['uthreadshold_count'] = 1 |
| 161 | projects_map[project]['alarm'] = SCALE_UP_EVALUATION |
| 162 | if projects_map[project]['uthreadshold_count'] >= CPU_THRESHOLD_REPEAT: |
| 163 | projects_map[project]['alarm'] = SCALE_UP_ALARM |
| 164 | handle_adjust_scale(project, 'up') |
| 165 | elif aggregate_cpu_util < CPU_LOWER_THRESHOLD: |
| 166 | projects_map[project]['lthreadshold_count'] = 1 |
| 167 | projects_map[project]['alarm'] = SCALE_DOWN_EVALUATION |
| 168 | if projects_map[project]['lthreadshold_count'] >= CPU_THRESHOLD_REPEAT: |
| 169 | projects_map[project]['alarm'] = SCALE_DOWN_ALARM |
| 170 | handle_adjust_scale(project, 'down') |
| 171 | else: |
| 172 | projects_map[project]['uthreadshold_count'] = 0 |
| 173 | projects_map[project]['lthreadshold_count'] = 0 |
| 174 | projects_map[project]['alarm'] = INITIAL_STATE |
| 175 | elif projects_map[project]['alarm'] == SCALE_UP_EVALUATION: |
| 176 | if aggregate_cpu_util > CPU_UPPER_THRESHOLD: |
| 177 | projects_map[project]['uthreadshold_count'] += 1 |
| 178 | if projects_map[project]['uthreadshold_count'] >= CPU_THRESHOLD_REPEAT: |
| 179 | projects_map[project]['alarm'] = SCALE_UP_ALARM |
| 180 | handle_adjust_scale(project, 'up') |
| 181 | elif aggregate_cpu_util < CPU_LOWER_THRESHOLD: |
| 182 | projects_map[project]['lthreadshold_count'] += 1 |
| 183 | projects_map[project]['alarm'] = SCALE_DOWN_EVALUATION |
| 184 | else: |
| 185 | projects_map[project]['uthreadshold_count'] = 0 |
| 186 | projects_map[project]['alarm'] = INITIAL_STATE |
| 187 | elif projects_map[project]['alarm'] == SCALE_DOWN_EVALUATION: |
| 188 | if aggregate_cpu_util < CPU_LOWER_THRESHOLD: |
| 189 | projects_map[project]['lthreadshold_count'] += 1 |
| 190 | if projects_map[project]['lthreadshold_count'] >= CPU_THRESHOLD_REPEAT: |
| 191 | projects_map[project]['alarm'] = SCALE_DOWN_ALARM |
| 192 | handle_adjust_scale(project, 'down') |
| 193 | elif aggregate_cpu_util > CPU_UPPER_THRESHOLD: |
| 194 | projects_map[project]['uthreadshold_count'] += 1 |
| 195 | projects_map[project]['alarm'] = SCALE_UP_EVALUATION |
| 196 | else: |
| 197 | projects_map[project]['lthreadshold_count'] = 0 |
| 198 | projects_map[project]['alarm'] = INITIAL_STATE |
Srikanth Vavilapalli | e1a34e8 | 2015-12-17 16:58:09 -0600 | [diff] [blame^] | 199 | threading.Timer(20, periodic_cpu_threshold_evaluator).start() |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 200 | |
| 201 | def read_notification_from_ceilometer(host,port): |
| 202 | udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP |
| 203 | udp.bind((host, port)) |
| 204 | |
| 205 | while True: |
| 206 | data, source = udp.recvfrom(64000) |
| 207 | try: |
| 208 | sample = msgpack.loads(data, encoding='utf-8') |
Srikanth Vavilapalli | e1a34e8 | 2015-12-17 16:58:09 -0600 | [diff] [blame^] | 209 | if sample['counter_name'] == 'instance': |
| 210 | if 'delete' in sample['resource_metadata']['event_type']: |
| 211 | xosTenantInfo = getXosTenantInfo(sample['project_id']) |
| 212 | xosResourceInfo = getXosInstanceInfo(sample['resource_id']) |
| 213 | print "SRIKANTH: Project %s Instance %s is getting deleted" % (xosTenantInfo['slice'] if xosTenantInfo['slice'] else sample['project_id'],xosResourceInfo) |
| 214 | if sample['project_id'] not in projects_map.keys(): |
| 215 | continue |
| 216 | if sample['resource_id'] not in projects_map[sample['project_id']]['resources'].keys(): |
| 217 | continue |
| 218 | projects_map[sample['project_id']]['resources'].pop(sample['resource_id'], None) |
| 219 | continue |
| 220 | elif sample['counter_name'] != 'cpu_util': |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 221 | continue |
| 222 | if sample['project_id'] not in projects_map.keys(): |
| 223 | projects_map[sample['project_id']] = {} |
teone | 46c13ab | 2015-12-16 18:28:14 -0500 | [diff] [blame] | 224 | xosTenantInfo = getXosTenantInfo(sample['project_id']) |
| 225 | projects_map[sample['project_id']]['project_id'] = sample['project_id'] |
| 226 | projects_map[sample['project_id']]['slice'] = xosTenantInfo['slice'] |
| 227 | projects_map[sample['project_id']]['service'] = xosTenantInfo['service'] |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 228 | projects_map[sample['project_id']]['resources'] = {} |
| 229 | projects_map[sample['project_id']]['uthreadshold_count'] = 0 |
| 230 | projects_map[sample['project_id']]['lthreadshold_count'] = 0 |
| 231 | projects_map[sample['project_id']]['alarm'] = INITIAL_STATE |
| 232 | resource_map = projects_map[sample['project_id']]['resources'] |
| 233 | if sample['resource_id'] not in resource_map.keys(): |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 234 | resource_map[sample['resource_id']] = {} |
| 235 | resource_map[sample['resource_id']]['xos_instance_info'] = getXosInstanceInfo(sample['resource_id']) |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 236 | resource_map[sample['resource_id']]['queue'] = [] |
| 237 | samples_queue = resource_map[sample['resource_id']]['queue'] |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 238 | sample = {'counter_name':sample['counter_name'], |
| 239 | 'project_id':sample['project_id'], |
| 240 | 'resource_id':sample['resource_id'], |
| 241 | 'timestamp':sample['timestamp'], |
| 242 | 'counter_unit':sample['counter_unit'], |
| 243 | 'counter_volume':sample['counter_volume']} |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 244 | deque = collections.deque(samples_queue, maxlen=10) |
| 245 | deque.append(sample) |
| 246 | resource_map[sample['resource_id']]['queue'] = list(deque) |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 247 | except Exception as e: |
| 248 | print e |
| 249 | |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 250 | def setup_webserver(): |
| 251 | try: |
| 252 | #config = ConfigParser.ConfigParser() |
| 253 | #config.read('pub_sub.conf') |
| 254 | #webserver_host = config.get('WEB_SERVER','webserver_host') |
| 255 | #webserver_port = int (config.get('WEB_SERVER','webserver_port')) |
| 256 | #client_host = config.get('CLIENT','client_host') |
| 257 | #client_port = int (config.get('CLIENT','client_port')) |
| 258 | |
| 259 | #log_level = config.get('LOGGING','level') |
| 260 | #log_file = config.get('LOGGING','filename') |
| 261 | |
| 262 | #level = LEVELS.get(log_level, logging.NOTSET) |
| 263 | #logging.basicConfig(filename=log_file,format='%(asctime)s %(levelname)s %(message)s',\ |
| 264 | # datefmt=_DEFAULT_LOG_DATE_FORMAT,level=level) |
| 265 | webserver_host = '0.0.0.0' |
| 266 | webserver_port = 9991 |
| 267 | |
| 268 | except Exception as e: |
| 269 | print("* Error in config file:",e.__str__()) |
| 270 | logging.error("* Error in confing file:%s",e.__str__()) |
| 271 | else: |
| 272 | app.run(host=webserver_host,port=webserver_port,debug=True, use_reloader=False) |
| 273 | |
| 274 | |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 275 | def main(): |
| 276 | monitoring_channel = acquire_xos_monitoring_channel() |
| 277 | if not monitoring_channel: |
| 278 | print 'SRIKANTH: XOS monitoring_channel is not created... Create it before using this app' |
| 279 | return |
Srikanth Vavilapalli | 0183a9f | 2015-12-14 17:35:04 -0500 | [diff] [blame] | 280 | loadAllXosTenantInfo() |
| 281 | loadAllXosInstanceInfo() |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 282 | thread.start_new(read_notification_from_ceilometer,(UDP_IP,UDP_PORT,)) |
| 283 | ceilometer_url = monitoring_channel['ceilometer_url'] |
| 284 | subscribe_data = {"sub_info":"cpu_util","app_id":"xos_auto_scale","target":"udp://10.11.10.1:12346"} |
| 285 | subscribe_url = ceilometer_url + 'v2/subscribe' |
| 286 | response = requests.post(subscribe_url, data=json.dumps(subscribe_data)) |
Srikanth Vavilapalli | e1a34e8 | 2015-12-17 16:58:09 -0600 | [diff] [blame^] | 287 | print 'SRIKANTH: Ceilometer meter "cpu_util" Subscription status:%s' % response.text |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 288 | #TODO: Fix the typo in 'sucess' |
| 289 | if (not 'sucess' in response.text) and (not 'already exists' in response.text): |
Srikanth Vavilapalli | e1a34e8 | 2015-12-17 16:58:09 -0600 | [diff] [blame^] | 290 | print 'SRIKANTH: Ceilometer meter "cpu_util" Subscription unsuccessful...Exiting' |
| 291 | return |
| 292 | subscribe_data = {"sub_info":"instance","app_id":"xos_auto_scale2","target":"udp://10.11.10.1:12346"} |
| 293 | subscribe_url = ceilometer_url + 'v2/subscribe' |
| 294 | response = requests.post(subscribe_url, data=json.dumps(subscribe_data)) |
| 295 | print 'SRIKANTH: Ceilometer meter "instance" Subscription status:%s' % response.text |
| 296 | #TODO: Fix the typo in 'sucess' |
| 297 | if (not 'sucess' in response.text) and (not 'already exists' in response.text): |
| 298 | print 'SRIKANTH: Ceilometer meter "instance"Subscription unsuccessful...Exiting' |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 299 | return |
| 300 | periodic_cpu_threshold_evaluator() |
| 301 | periodic_print() |
Srikanth Vavilapalli | 8d363f1 | 2015-12-16 14:55:30 -0500 | [diff] [blame] | 302 | setup_webserver() |
Srikanth Vavilapalli | 7da4b71 | 2015-12-14 00:52:57 -0500 | [diff] [blame] | 303 | |
| 304 | if __name__ == "__main__": |
| 305 | main() |