Exemplo n.º 1
0
AvailabilityZone = urllib2.urlopen('http://169.254.169.254/latest/meta-data/placement/availability-zone').read()
LocalIp = urllib2.urlopen('http://169.254.169.254/latest/meta-data/local-ipv4').read()

def state_check():
    try:
        Peer = aws.get_instance(PeerAz,'nat',arg.vpc_id)
        if 'running' not in Peer.get('State', {}).get('Name', None):
            return str('FAIL')
        elif 'running' in Peer.get('State', {}).get('Name', None):
            PeerId = Peer.get('Id')
            PeerIp = aws.instance_ip(PeerId)
            request = urllib2.Request('http://' + PeerIp + '/index.html')
            response = urllib2.urlopen(request, timeout=4)
            content = response.read()
            if 'OK' in content:
                syslog.syslog(str('Response from host is OK'))
                return content
    except:
    	return str('FAIL')

aws = cmd.aws(arg.region_name)
shell = cmd.bash()

PeerAz = aws.get_peer_az(arg.vpc_id,InstanceId)

while True:
    if "OK" not in state_check():
        syslog.syslog(str('Peer has failed, running the nat.py function'))
        shell.cmd(str('/usr/local/bin/take_nat.sh'))
    time.sleep(10)
Exemplo n.º 2
0

''' Collect all of the command line variables '''
parser = argparse.ArgumentParser(
prog='alarm creater',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''This program will build an alarm stack.''')
parser.add_argument('-rgn','--region_name', required=True)
parser.add_argument('-tpn','--topic_name', required=True)
arg = parser.parse_args()

ec2 = boto3.client('elb',arg.region_name)
sns = boto3.client('sns',arg.region_name)
cfn = boto3.client('cloudformation',arg.region_name)
elbs = ec2.describe_load_balancers()
awscmd = cmd.aws(arg.region_name)


''' Define all functions here '''
def id_gen(size=8, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))

def topic_arn():
    arn = sns.list_topics()
    for item in arn['Topics']:
        if arg.topic_name in item['TopicArn']:
            return item['TopicArn']

def param(name, param_type):
    res_name = str(name).replace('-','')
    name = t.add_parameter(Parameter(