def main(): global configuration WatchData.stats_period = 60 # Show just last measure data = WatchData() data.connect(configuration.group) data.get_instances_info() """ Check if we must change the desired instances """ if configuration.instances > 1: desired = configuration.instances if desired > 0 and abs(data.instances - desired) < 3: data.set_desired(desired) else: print "You can specify up to +-2 instances more of currently running (%d)" % (data.instances,) exit(0) if configuration.kill: if configuration.kill in [x.id for x in data.instances_info]: data.kill_instance(configuration.kill) else: print "Instance", configuration.kill, "doesn't exist" exit(0) data.get_CPU_loads() print "Group values: instances: %d min: %d max: %d desired: %d Launch config: %s" % (data.instances, data.group.min_size, data.group.max_size, data.group.desired_capacity, data.group.launch_config_name) for instance in data.instances_info: if instance.id in data.loads: load = data.loads[instance.id] else: load = 0 print "%s %5.2f%% %s %s" % (instance.id, load, instance._state.name, instance.image_id), if configuration.all: print "%s %s %-15s %s" % (instance.instance_type, instance._placement, instance.private_ip_address, instance.dns_name, ) else: print print "Average load: %5.2f%%" % (data.avg_load,) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % (data.instances-1, data.total_load/(data.instances-1))
def main(): global configuration now = int(time.time()) data = WatchData() """ Set default class values """ if configuration.dry: WatchData.dry = True if configuration.low: WatchData.low_limit = configuration.low if configuration.high: WatchData.high_limit = configuration.high if configuration.high_urgent: WatchData.high_urgent = configuration.high_urgent if configuration.history: WatchData.history_size = configuration.history if configuration.low_counter: WatchData.low_counter_limit = configuration.low_counter if configuration.low_counter: WatchData.high_counter_limit = configuration.high_counter try: data.connect(configuration.group) data.get_CPU_loads() except boto.exception.BotoServerError: print("Error in Boto") return prev_data = WatchData.from_file() """ Retrieve and calculate previous values in the current instance """ data.action_ts = int(prev_data.action_ts) data.action = prev_data.action data.up_ts = int(prev_data.up_ts) data.down_ts = int(prev_data.down_ts) data.history = prev_data.history try: data.low_counter = int(prev_data.low_counter) except AttributeError: data.low_counter = 0 try: data.high_counter = int(prev_data.high_counter) except AttributeError: data.high_counter = 0 """ Calculate the trend, increasing or decreasing CPU usage """ alpha = min((data.ts - prev_data.ts) / 60.0 * 0.3, 1) data.exponential_average = alpha * data.avg_load + (1 - alpha) * prev_data.exponential_average data.trend = 2 * data.exponential_average - prev_data.exponential_average print prev_data.exponential_average, data.exponential_average, data.trend if data.instances != prev_data.instances: data.previous_instances = prev_data.instances if data.instances > prev_data.instances: data.up_ts = int(time.time()) else: data.down_ts = int(time.time()) else: data.previous_instances = prev_data.previous_instances if data.instances != prev_data.instances or data.desired != prev_data.desired: data.changed_ts = int(time.time()) else: data.changed_ts = int(prev_data.changed_ts) print "%s values: instances: %d min: %d max: %d desired: %d" % (configuration.group, data.instances, data.group.min_size, data.group.max_size, data.group.desired_capacity) print "Average load: %5.2f%% Trend: %5.2f" % (data.avg_load,data.trend) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % (data.instances-1, data.total_load/(data.instances-1)) print "Last change: %s last action: %s (%s)" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.changed_ts)), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.action_ts)), data.action) print "Last up: %s last down: %s" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.up_ts)), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.down_ts))) if now - data.changed_ts > 600 and now - data.action_ts > 600: if not data.check_too_low(): data.check_too_high() if now - data.changed_ts > 300 and now - data.action_ts > 300: data.check_avg_high() if now - data.changed_ts > 300 and now - data.action_ts > 300 and now - data.up_ts > 1800: data.check_avg_low() data.store(configuration.annotation) if configuration.mail and data.emergency: sendmail(data, configuration.mail)
def main(): global configuration now = int(time.time()) data = WatchData() """ Set default class values """ if configuration.dry: WatchData.dry = True if configuration.low: WatchData.low_limit = configuration.low if configuration.high: WatchData.high_limit = configuration.high if configuration.high_urgent: WatchData.high_urgent = configuration.high_urgent if configuration.history: WatchData.history_size = configuration.history data.connect(configuration.group) data.get_CPU_loads() prev_data = WatchData.from_file() """ Retrieve and calculate previous values in the current instance """ data.action_ts = int(prev_data.action_ts) data.action = prev_data.action data.up_ts = int(prev_data.up_ts) data.down_ts = int(prev_data.down_ts) data.history = prev_data.history if data.instances != prev_data.instances: data.previous_instances = prev_data.instances if data.instances > prev_data.instances: data.up_ts = int(time.time()) else: data.down_ts = int(time.time()) else: data.previous_instances = prev_data.previous_instances if data.instances != prev_data.instances or data.desired != prev_data.desired: data.changed_ts = int(time.time()) else: data.changed_ts = int(prev_data.changed_ts) print "%s values: instances: %d min: %d max: %d desired: %d" % ( configuration.group, data.instances, data.group.min_size, data.group.max_size, data.group.desired_capacity) print "Average load: %5.2f%%" % (data.avg_load, ) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % ( data.instances - 1, data.total_load / (data.instances - 1)) print "Last change: %s last action: %s (%s)" % ( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.changed_ts)), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime( data.action_ts)), data.action) print "Last up: %s last down: %s" % ( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.up_ts)), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.down_ts))) if now - data.changed_ts > 600 and now - data.action_ts > 600: if not data.check_too_low(): data.check_too_high() if now - data.changed_ts > 300 and now - data.action_ts > 300: data.check_avg_high() if now - data.changed_ts > 300 and now - data.action_ts > 300 and now - data.up_ts > 1800: data.check_avg_low() data.store(configuration.annotation) if configuration.mail and data.emergency: sendmail(data, configuration.mail)
def main(): global configuration now = int(time.time()) data = WatchData() """ Set default class values """ if configuration.dry: WatchData.dry = True if configuration.low: WatchData.low_limit = configuration.low if configuration.high: WatchData.high_limit = configuration.high if configuration.high_urgent: WatchData.high_urgent = configuration.high_urgent if configuration.history: WatchData.history_size = configuration.history data.connect(configuration.group) data.get_CPU_loads() prev_data = WatchData.from_file() """ Retrieve and calculate previous values in the current instance """ data.action_ts = int(prev_data.action_ts) data.action = prev_data.action data.up_ts = int(prev_data.up_ts) data.down_ts = int(prev_data.down_ts) data.history = prev_data.history if data.instances != prev_data.instances: data.previous_instances = prev_data.instances if data.instances > prev_data.instances: data.up_ts = int(time.time()) else: data.down_ts = int(time.time()) else: data.previous_instances = prev_data.previous_instances if data.instances != prev_data.instances or data.desired != prev_data.desired: data.changed_ts = int(time.time()) else: data.changed_ts = int(prev_data.changed_ts) print "%s values: instances: %d min: %d max: %d desired: %d" % (configuration.group, data.instances, data.group.min_size, data.group.max_size, data.group.desired_capacity) print "Average load: %5.2f%%" % (data.avg_load,) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % (data.instances-1, data.total_load/(data.instances-1)) print "Last change: %s last action: %s (%s)" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.changed_ts)), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.action_ts)), data.action) print "Last up: %s last down: %s" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.up_ts)), time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data.down_ts))) if now - data.changed_ts > 600 and now - data.action_ts > 600: if not data.check_too_low(): data.check_too_high() if now - data.changed_ts > 300 and now - data.action_ts > 300: data.check_avg_high() if now - data.changed_ts > 300 and now - data.action_ts > 300 and now - data.up_ts > 1800: data.check_avg_low() data.store(configuration.annotation) if configuration.mail and data.emergency: sendmail(data, configuration.mail)
def main(): global configuration now = int(time.time()) data = WatchData() """ Set default class values """ if configuration.dry: WatchData.dry = True if configuration.low: WatchData.low_limit = configuration.low if configuration.high: WatchData.high_limit = configuration.high if configuration.high_urgent: WatchData.high_urgent = configuration.high_urgent if configuration.history: WatchData.history_size = configuration.history if configuration.low_counter: WatchData.low_counter_limit = configuration.low_counter if configuration.low_counter: WatchData.high_counter_limit = configuration.high_counter data.connect(configuration.group) data.get_CPU_loads() prev_data = WatchData.from_file() """ Retrieve and calculate previous values in the current instance """ data.action_ts = int(prev_data.action_ts) data.action = prev_data.action data.up_ts = int(prev_data.up_ts) data.down_ts = int(prev_data.down_ts) data.history = prev_data.history try: data.low_counter = int(prev_data.low_counter) except AttributeError: data.low_counter = 0 try: data.high_counter = int(prev_data.high_counter) except AttributeError: data.high_counter = 0 """ Calculate the trend, increasing or decreasing CPU usage """ alpha = min((data.ts - prev_data.ts) / 60.0 * 0.3, 1) data.exponential_average = alpha * data.avg_load + (1 - alpha) * prev_data.exponential_average data.trend = 2 * data.exponential_average - prev_data.exponential_average print prev_data.exponential_average, data.exponential_average, data.trend if data.instances != prev_data.instances: data.previous_instances = prev_data.instances if data.instances > prev_data.instances: data.up_ts = int(time.time()) else: data.down_ts = int(time.time()) else: data.previous_instances = prev_data.previous_instances if data.instances != prev_data.instances or data.desired != prev_data.desired: data.changed_ts = int(time.time()) else: data.changed_ts = int(prev_data.changed_ts) print "%s values: instances: %d min: %d max: %d desired: %d" % ( configuration.group, data.instances, data.group.min_size, data.group.max_size, data.group.desired_capacity, ) print "Average load: %5.2f%% Trend: %5.2f" % (data.avg_load, data.trend) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % (data.instances - 1, data.total_load / (data.instances - 1)) print "Last change: %s last action: %s (%s)" % ( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data.changed_ts)), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data.action_ts)), data.action, ) print "Last up: %s last down: %s" % ( time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data.up_ts)), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data.down_ts)), ) if now - data.changed_ts > 600 and now - data.action_ts > 600: if not data.check_too_low(): data.check_too_high() if now - data.changed_ts > 300 and now - data.action_ts > 300: data.check_avg_high() if now - data.changed_ts > 300 and now - data.action_ts > 300 and now - data.up_ts > 1800: data.check_avg_low() data.store(configuration.annotation) if configuration.mail and data.emergency: sendmail(data, configuration.mail)
def main(): global configuration WatchData.stats_period = 60 # Show just last measure data = WatchData() data.connect(configuration.group) data.get_instances_info() """ Check if we must change the desired instances """ if configuration.instances > 1: desired = configuration.instances if desired > 0 and abs(data.instances - desired) < 3: data.set_desired(desired) else: print "You can specify up to +-2 instances more of currently running (%d)" % ( data.instances, ) exit(0) if configuration.kill: if configuration.kill in [x.id for x in data.instances_info]: data.kill_instance(configuration.kill) else: print "Instance", configuration.kill, "doesn't exist" exit(0) data.get_CPU_loads() print "Group values: instances: %d min: %d max: %d desired: %d Launch config: %s" % ( data.instances, data.group.min_size, data.group.max_size, data.group.desired_capacity, data.group.launch_config_name) for instance in data.instances_info: if instance.id in data.loads: load = data.loads[instance.id] else: load = 0 print "%s %5.2f%% %s %s" % (instance.id, load, instance._state.name, instance.image_id), if configuration.all: print "%s %s %-15s %s" % ( instance.instance_type, instance._placement, instance.private_ip_address, instance.dns_name, ) else: print print "Average load: %5.2f%%" % (data.avg_load, ) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % ( data.instances - 1, data.total_load / (data.instances - 1))
def main(): global configuration WatchData.stats_period = 60 # Show just last measure data = WatchData(configuration.group) data.connect() data.get_instances_info() """ Check if we must change the desired instances """ if configuration.instances > 1: desired = configuration.instances if desired > 0 and abs(data.instances - desired) < 3: data.set_desired(desired) else: print "You can specify up to +-2 instances more of currently running (%d)" % ( data.instances, ) exit(0) if configuration.kill: if configuration.kill in [x.id for x in data.instances_info]: data.kill_instance(configuration.kill, True) else: print "Instance", configuration.kill, "doesn't exist" exit(0) data.get_CPU_loads() print "Group values: instances: %d min: %d max: %d desired: %d Launch config: %s" % ( data.instances, data.min_size, data.max_size, data.desired, data.group['LaunchConfigurationName']) for i, d in data.instances_info.iteritems(): if i in data.loads: load = data.loads[i] else: load = 0 print "%s %5.2f%% %s %s" % (i, load, d['State']['Name'], d['ImageId']), print "%s %s %-15s" % (d['InstanceType'], d['Placement']['AvailabilityZone'], d['NetworkInterfaces'][0]['PrivateIpAddress']), if configuration.all: print "%-15s" % (d['NetworkInterfaces'][0]['PrivateDnsName']) else: print print "Average load: %5.2f%%" % (data.avg_load, ) if data.instances > 1: print "Average load with %d instances: %5.2f%%" % ( data.instances - 1, data.total_load / (data.instances - 1))