Created
October 20, 2014 20:21
-
-
Save twexler/0e400e545296bcd13a51 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import boto | |
import sys | |
#standard nagios exit codes | |
EXIT_OK = 0 | |
EXIT_WARN = 1 | |
EXIT_CRIT = 2 | |
EXIT_UNKN = 3 | |
def main(): | |
instances = [] | |
try: | |
conn = boto.connect_ec2() | |
instance_statuses = conn.get_all_instance_status() | |
except Exception as e: | |
_print("CRITICAL: Exception caught: %s" % type(e)) | |
sys.exit(EXIT_UNKN) | |
for status in instance_statuses: | |
if status.events: | |
if 'completed' not in status.events[0].description.lower(): | |
instances.append(status.id) | |
if len(instances) != 0: | |
_print("WARNING: %d instance(s) with scheduled maintenance: %s" % (len(instances), ', '.join(instances))) | |
sys.exit(EXIT_WARN) | |
else: | |
_print("OK: No instances with scheduled mainteance") | |
sys.exit(EXIT_OK) | |
def get_all_instance_status(conn, instance_ids=None, | |
max_results=None, next_token=None, | |
filters=None): | |
"""see function documentation at | |
http://docs.pythonboto.org/en/latest/ref/ec2.html#boto.ec2.connection.EC2Connection.get_all_instance_status""" | |
old_api_version = conn.APIVersion | |
conn.APIVersion = '2012-06-01' | |
params = {} | |
if instance_ids: | |
conn.build_list_params(params, instance_ids, 'InstanceId') | |
if max_results: | |
params['MaxResults'] = max_results | |
if next_token: | |
params['NextToken'] = next_token | |
if filters: | |
conn.build_filter_params(params, filters) | |
result = conn.get_object('DescribeInstanceStatus', params, | |
InstanceStatusSet, verb='POST') | |
conn.APIVersion = old_api_version | |
return result | |
def _print(s): | |
"""Wrapper for existing print statement, flushes stdout""" | |
print s | |
sys.stdout.flush() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment