Skip to content

Commit 8a87e95

Browse files
committed
update script
1 parent 32ad221 commit 8a87e95

File tree

2 files changed

+48
-54
lines changed

2 files changed

+48
-54
lines changed

monitor/monitor/requirements.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
urllib2==20.6.21
1+
urllib5==5.0.0
2+
certifi==2020.4.5.1
3+
chardet==3.0.4
4+
idna==2.9
5+
requests==2.23.0

monitor/src/index.py

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python
22

33
import boto3
4-
import urllib.request as urllib2
4+
# import urllib.request as urllib2
55
import os
66
import json
77
import threading
8+
import requests
9+
from requests.exceptions import HTTPError
810

911
website_url = json.loads(os.environ['Website_URL'])
1012
metricname = os.environ['metricname']
@@ -14,73 +16,59 @@ def write_metric(value, metric, site):
1416
print(value)
1517
d = boto3.client('cloudwatch')
1618
d.put_metric_data(Namespace='Website Status',
17-
MetricData=[
18-
{
19-
'MetricName':metric,
20-
'Dimensions':[
21-
{
22-
'Name': 'Status',
23-
'Value': 'WebsiteStatusCode',
24-
},
25-
{
26-
'Name': 'Website',
27-
'Value': site,
28-
},
29-
],
30-
'Value': value,
19+
MetricData=[
20+
{
21+
'MetricName':metric,
22+
'Dimensions':[
23+
{
24+
'Name': 'Status',
25+
'Value': 'WebsiteStatusCode',
26+
},
27+
{
28+
'Name': 'Website',
29+
'Value': site,
30+
},
31+
],
32+
'Value': value,
3133
},
3234
]
33-
)
35+
)
3436

3537
def check_site(url, metric):
3638

3739
STAT = 1
3840
print("Checking %s " % url)
39-
request = urllib2.Request(url)
41+
# request = urllib2.Request(url)
4042
try:
41-
response = urllib2.urlopen(request,timeout=timeout)
42-
response.close()
43-
except urllib2.URLError as e:
44-
if hasattr(e, 'code'):
45-
print("1st if")
46-
print ("[Error:] Connection to %s failed with code: " %url +str(e.code))
47-
STAT = int(e.code)
48-
# write_metric(STAT, metric, url)
49-
elif hasattr(e, 'reason'):
50-
print("2nd if")
51-
print ("[Error:] Connection to %s failed with code: " % url +str(e.reason))
52-
STAT = 501
53-
# write_metric(STAT, metric, url)
54-
except urllib2.HTTPError as e:
55-
if hasattr(e, 'code'):
56-
print("3rd if")
57-
print ("[Error:] Connection to %s failed with code: " % url + str(e.code))
58-
STAT = int(e.code)
59-
# write_metric(STAT, metric, url)
60-
elif hasattr(e, 'reason'):
61-
print("4th if")
62-
print ("[Error:] Connection to %s failed with code: " % url + str(e.reason))
63-
STAT = 501
64-
# write_metric(STAT, metric, url)
65-
print(STAT)
43+
response = requests.get(url,timeout=timeout)
44+
# response.raise_for_status()
45+
except HTTPError as http_err:
46+
print(http_err)
47+
STAT = 501
48+
except Exception as err:
49+
print(err)
50+
STAT = 501
51+
6652
if STAT != 501 and STAT ==1:
67-
STAT = response.getcode()
53+
print('Success!')
54+
print(response.status_code)
55+
STAT = response.status_code
6856

6957
return STAT
7058

7159
def run_thread(site):
72-
r = check_site(site,metricname)
73-
if r == 200 or r == 304 or r == 400:
74-
print("Site %s is up" %site)
75-
write_metric(200, metricname, site)
76-
else:
77-
print("[Error:] Site %s down" %site)
78-
write_metric(int(r), metricname, site)
60+
r = check_site(site,metricname)
61+
if r == 200 or r == 304 or r == 400:
62+
print("Site %s is up" %site)
63+
write_metric(200, metricname, site)
64+
else:
65+
print("[Error:] Site %s down" %site)
66+
write_metric(int(r), metricname, site)
7967

80-
def handler(event, context):
68+
def handler():
8169

8270
# Change these to your actual websites. Remember, the more websites you list
83-
# the longer the lambda function will run
71+
# the longer the lambda function will run
8472
websiteurls = website_url
8573
t = [0]*len(website_url)
8674
for i in range(len(websiteurls)):
@@ -89,4 +77,6 @@ def handler(event, context):
8977

9078
for i in range(len(t)):
9179
t[i].join()
92-
print("Done!")
80+
print("Done!")
81+
82+
handler()

0 commit comments

Comments
 (0)