-
Notifications
You must be signed in to change notification settings - Fork 3
Manage CloudWatch status-check alarms with VM lifecycle #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
frank-veriff
wants to merge
3
commits into
markokr:master
Choose a base branch
from
frank-veriff:cloudwatch-status-alarms
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -358,6 +358,11 @@ def get_route53(self): | |
| """ | ||
| return self.get_boto3_client('route53') | ||
|
|
||
| def get_cloudwatch(self): | ||
| """Get cached CloudWatch connection. | ||
| """ | ||
| return self.get_boto3_client('cloudwatch') | ||
|
|
||
| def get_ec2_client(self, region=None): | ||
| return self.get_boto3_client('ec2', region) | ||
|
|
||
|
|
@@ -2306,9 +2311,117 @@ def vm_create_start(self): | |
|
|
||
| return ids | ||
|
|
||
| # CloudWatch status-check alarms, attached to VMs like DNS/EIP. | ||
| # No-op unless cloudwatch_alarm_checks is set. AWS evaluates the | ||
| # metrics and pushes state changes; vmtool only manages the objects. | ||
| CW_ALARM_SPECS = { | ||
| 'system': ('StatusCheckFailed_System', 1), | ||
| 'instance': ('StatusCheckFailed_Instance', 2), | ||
| 'ebs': ('StatusCheckFailed_AttachedEBS', 1), | ||
| } | ||
| CW_ALARM_PAGES = ('system', 'ebs') | ||
|
|
||
| def assign_status_alarms(self, vm_id): | ||
| """Create status-check alarms for VM. Idempotent, non-fatal. | ||
| """ | ||
| checks = self.cf.getlist('cloudwatch_alarm_checks', []) | ||
| if not checks: | ||
| return | ||
| for check in checks: | ||
| if check not in self.CW_ALARM_SPECS: | ||
| eprintf("WARNING: cloudwatch_alarm_checks: unknown check, skipping alarms: %s", check) | ||
| return | ||
| prefix = self.cf.get('cloudwatch_alarm_prefix', 'vm-status-') | ||
| topic = self.cf.get('cloudwatch_alarm_topic', '') | ||
| client = self.get_cloudwatch() | ||
| for done, check in enumerate(checks): | ||
| metric, evals = self.CW_ALARM_SPECS[check] | ||
| desc = self.cf.get('cloudwatch_alarm_desc_%s' % check, | ||
| '%s status check failed' % check) | ||
| actions = [topic] if topic and check in self.CW_ALARM_PAGES else [] | ||
| try: | ||
| client.put_metric_alarm( | ||
| AlarmName='%s%s-%s' % (prefix, check, vm_id), | ||
| AlarmDescription='%s - %s' % (self.full_role, desc), | ||
| Namespace='AWS/EC2', | ||
| MetricName=metric, | ||
| Dimensions=[{'Name': 'InstanceId', 'Value': vm_id}], | ||
| Statistic='Maximum', | ||
| Period=60, | ||
| EvaluationPeriods=evals, | ||
| Threshold=1, | ||
| ComparisonOperator='GreaterThanOrEqualToThreshold', | ||
| AlarmActions=actions, | ||
| OKActions=actions) | ||
| except Exception as ex: | ||
| eprintf("WARNING: alarm create failed (%s/%s), %d/%d alarms set, fix with alarm-sync: %s", | ||
| vm_id, check, done, len(checks), ex) | ||
| return | ||
| time_printf("Status alarms set: %s", vm_id) | ||
|
|
||
| def drop_status_alarms(self, *vm_ids): | ||
| """Delete status-check alarms of VMs. Non-fatal. | ||
| """ | ||
| if not vm_ids or not self.cf.getlist('cloudwatch_alarm_checks', []): | ||
| return | ||
| prefix = self.cf.get('cloudwatch_alarm_prefix', 'vm-status-') | ||
| names = ['%s%s-%s' % (prefix, check, vm_id) | ||
| for vm_id in vm_ids for check in self.CW_ALARM_SPECS] | ||
| client = self.get_cloudwatch() | ||
| try: | ||
| for pos in range(0, len(names), 100): | ||
| client.delete_alarms(AlarmNames=names[pos:pos + 100]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while it's unlikely, with large volumes this may get throttled |
||
| if self.options.verbose: | ||
| printf("Status alarms dropped: %s", " ".join(vm_ids)) | ||
| except Exception as ex: | ||
| eprintf("WARNING: alarm delete failed: %s", ex) | ||
|
|
||
| def prune_status_alarms(self): | ||
| """Delete status-check alarms whose VM no longer exists. Non-fatal. | ||
| """ | ||
| if not self.cf.getlist('cloudwatch_alarm_checks', []): | ||
| return | ||
| prefix = self.cf.get('cloudwatch_alarm_prefix', 'vm-status-') | ||
| client = self.get_cloudwatch() | ||
| try: | ||
| alive = set() | ||
| for vm in self.ec2_iter_instances(): | ||
| alive.add(vm['InstanceId']) | ||
| orphans = [] | ||
| for page in client.get_paginator('describe_alarms').paginate(AlarmNamePrefix=prefix): | ||
| for alarm in page['MetricAlarms']: | ||
| # alarm name is <prefix><check>-<instance-id> | ||
| name = alarm['AlarmName'] | ||
| pos = name.find('-i-') | ||
| if pos < 0 or name[pos + 1:] in alive: | ||
| continue | ||
| orphans.append(name) | ||
| for pos in range(0, len(orphans), 100): | ||
| client.delete_alarms(AlarmNames=orphans[pos:pos + 100]) | ||
| if orphans: | ||
| printf("Pruned %d orphaned status alarms", len(orphans)) | ||
| except Exception as ex: | ||
| eprintf("WARNING: alarm prune failed: %s", ex) | ||
|
|
||
| def cmd_alarm_sync(self, *vm_ids): | ||
| """Create/update status-check alarms for VMs (all running role VMs | ||
| by default). Does not remove alarms of terminated VMs. | ||
|
|
||
| Group: vm | ||
| """ | ||
| for check in self.cf.getlist('cloudwatch_alarm_checks', []): | ||
| if check not in self.CW_ALARM_SPECS: | ||
| raise UsageError('cloudwatch_alarm_checks: unknown check: %s' % check) | ||
| if not vm_ids: | ||
| vm_ids = [vm['InstanceId'] for vm in self.get_running_vms()] | ||
| for vm_id in vm_ids: | ||
| self.assign_status_alarms(vm_id) | ||
|
|
||
| def vm_create_finish(self, ids): | ||
| for vm_id in ids: | ||
| self.new_ssh_key(vm_id) | ||
| for vm_id in ids: | ||
| self.assign_status_alarms(vm_id) | ||
| time_printf("Instances are ready") | ||
| time.sleep(10) | ||
| self._vm_map = {} | ||
|
|
@@ -2455,6 +2568,7 @@ def cmd_terminate(self, *ids): | |
| raise UsageError("Instances not stopped: %s" % " ".join(bad)) | ||
| else: | ||
| client.terminate_instances(InstanceIds=ids) | ||
| self.drop_status_alarms(*ids) | ||
|
|
||
| def cmd_gc(self): | ||
| """Terminate all stopped vms. | ||
|
|
@@ -2496,10 +2610,12 @@ def cmd_gc(self): | |
| if garbage: | ||
| printf("Terminating: %s", " ".join(garbage)) | ||
| client.terminate_instances(InstanceIds=garbage) | ||
| self.drop_status_alarms(*garbage) | ||
| elif keep_count > 0: | ||
| printf("Keeping stopped instances") | ||
| else: | ||
| printf("No stopped instances") | ||
| self.prune_status_alarms() | ||
|
|
||
| def cmd_ssh(self, *args): | ||
| """SSH to VM and run command (optional). | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should continue instead of returning?