Skip to content

Commit 11d6c2f

Browse files
committed
implement multithread
1 parent 279f0b4 commit 11d6c2f

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ module "cloudtrail-slack-notification" {
8282
variables = {
8383
Website_URL = jsonencode(["https://google.com"]),
8484
metricname = "Site Availability"
85+
timeout = 5
8586
}
8687
slack_variables = {
87-
SLACK_WEBHOOK = "https://hooks.slack.com/services/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r1Y"
88+
SLACK_WEBHOOK = "https://hooks.slack.com/services/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yrrh08dJo5r1Y"
8889
}
8990
}
9091
```

README.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ usage : |-
4646
variables = {
4747
Website_URL = jsonencode(["https://google.com"]),
4848
metricname = "Site Availability"
49+
timeout = 5
4950
}
5051
slack_variables = {
51-
SLACK_WEBHOOK = "https://hooks.slack.com/services/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r1Y"
52+
SLACK_WEBHOOK = "https://hooks.slack.com/services/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yrrh08dJo5r1Y"
5253
}
5354
}
5455
```

_example/example.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ module "site-monitor" {
1414
variables = {
1515
Website_URL = jsonencode(["https://google.com"]),
1616
metricname = "Site Availability"
17+
timeout = 5
1718
}
1819
slack_variables = {
19-
SLACK_WEBHOOK = "https://hooks.slack.com/services/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r1Y"
20+
SLACK_WEBHOOK = "https://hooks.slack.com/services/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yrrh08dJo5r1Y"
2021
}
2122
}
2223

@@ -40,7 +41,7 @@ module "alarm" {
4041
alarm_actions = [module.site-monitor.sns_arn]
4142

4243
actions_enabled = true
43-
insufficient_data_actions = []
44+
insufficient_data_actions = [module.site-monitor.sns_arn]
4445
ok_actions = [module.site-monitor.sns_arn]
4546
dimensions = {
4647
Website = "https://google.com",

monitor/src/index.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import urllib.request as urllib2
55
import os
66
import json
7+
import threading
78

89
website_url = json.loads(os.environ['Website_URL'])
910
metricname = os.environ['metricname']
@@ -36,17 +37,16 @@ def check_site(url, metric):
3637
STAT = 1
3738
print("Checking %s " % url)
3839
request = urllib2.Request(url)
39-
4040
try:
41-
response = urllib2.urlopen(request, timeout=timeout)
41+
response = urllib2.urlopen(request,timeout=timeout)
4242
response.close()
4343
except urllib2.URLError as e:
4444
if hasattr(e, 'code'):
4545
print("1st if")
4646
print ("[Error:] Connection to %s failed with code: " %url +str(e.code))
4747
STAT = int(e.code)
4848
# write_metric(STAT, metric, url)
49-
if hasattr(e, 'reason'):
49+
elif hasattr(e, 'reason'):
5050
print("2nd if")
5151
print ("[Error:] Connection to %s failed with code: " % url +str(e.reason))
5252
STAT = 501
@@ -57,27 +57,36 @@ def check_site(url, metric):
5757
print ("[Error:] Connection to %s failed with code: " % url + str(e.code))
5858
STAT = int(e.code)
5959
# write_metric(STAT, metric, url)
60-
if hasattr(e, 'reason'):
60+
elif hasattr(e, 'reason'):
6161
print("4th if")
6262
print ("[Error:] Connection to %s failed with code: " % url + str(e.reason))
6363
STAT = 501
6464
# write_metric(STAT, metric, url)
65-
if STAT != 501:
65+
print(STAT)
66+
if STAT != 501 and STAT ==1:
6667
STAT = response.getcode()
6768

6869
return STAT
6970

71+
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)
79+
7080
def handler(event, context):
7181

7282
# Change these to your actual websites. Remember, the more websites you list
7383
# the longer the lambda function will run
7484
websiteurls = website_url
85+
t = [0]*len(website_url)
86+
for i in range(len(websiteurls)):
87+
t[i] = threading.Thread(target=run_thread, args=(website_url[i],))
88+
t[i].start()
7589

76-
for site in websiteurls:
77-
r = check_site(site,metricname)
78-
if r == 200 or r == 304 or r == 400:
79-
print("Site %s is up" %site)
80-
write_metric(200, metricname, site)
81-
else:
82-
print("[Error:] Site %s down" %site)
83-
write_metric(int(r), metricname, site)
90+
for i in range(len(t)):
91+
t[i].join()
92+
print("Done!")

slack/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ var handleCloudWatch = function(event, context) {
5555

5656
if (message.NewStateValue === "ALARM") {
5757
color = "danger";
58+
message = "Monitor: endpoint is down (!200)";
5859
} else if (message.NewStateValue === "OK") {
5960
color = "good";
61+
message = "Monitor: endpoint is up (200)";
6062
}
6163

6264
var slackMessage = {
@@ -68,6 +70,7 @@ var handleCloudWatch = function(event, context) {
6870
{ "title": "URL", "value": resource, "short": true },
6971
{ "title": "status", "value": newState, "short": true },
7072
{ "title": "Time", "value": new Date(event.Records[0].Sns.Timestamp), "short": true},
73+
{ "title": "Message", "value": message, "short": true},
7174
{
7275
"title": "Link to Alarm",
7376
"value": "https://console.aws.amazon.com/cloudwatch/home?region=" + region + "#alarm:alarmFilter=ANY;name=" + encodeURIComponent(alarmName),

0 commit comments

Comments
 (0)