import boto3 import urllib2 def write_metric(value, metric): d = boto3.client('cloudwatch') d.put_metric_data(Namespace='Website Status', MetricData=[ { 'MetricName':metric, 'Dimensions':[ { 'Name': 'Status', 'Value': 'WebsiteStatusCode', }, ], 'Value': value, }, ] ) def check_site(url, metric): STAT = 1 print("Checking %s " % url) # Para HTTP, altere onde agora esta https request = urllib2.Request("https://" +url) try: response = urllib2.urlopen(request) response.close() except urllib2.URLError as e: if hasattr(e, 'code'): print ("[Error:] Connection to %s failed with code: " %url +str(e.code)) STAT = 100 write_metric(STAT, metric) if hasattr(e, 'reason'): print ("[Error:] Connection to %s failed with code: " % url +str(e.reason)) STAT = 100 write_metric(STAT, metric) except urllib2.HTTPError as e: if hasattr(e, 'code'): print ("[Error:] Connection to %s failed with code: " % url + str(e.code)) STAT = 100 write_metric(STAT, metric) if hasattr(e, 'reason'): print ("[Error:] Connection to %s failed with code: " % url + str(e.reason)) STAT = 100 write_metric(STAT, metric) print('HTTPError!!!') if STAT != 100: STAT = response.getcode() return STAT def lambda_handler(event, context): # Aqui voce coloca sua lista de sites # Quanto maior for a lista, mais demorada será a execução websiteurls = [ "site1.com", "site2.com", "site3.com" ] metricname = 'Site Availability' for site in websiteurls: r = check_site(site,metricname) if r == 200 or r == 304: print("Site %s is up" %site) write_metric(200, metricname) else: print("[Error:] Site %s down" %site) write_metric(50, metricname)