Since the site in operation was occasionally down, It's simple, but I prepared a site monitor. (There are various generous monitoring services in the world, including acquisition of server metrics, but this time we are making it as a low-cost and easy way to link with Slack.)
Now, let's write the code to check the site. The general flow is to check the HTTP status of the specified URL and notify Slack if there are sites other than 200. (On the contrary, if there is no problem, Slack will not be notified of anything. Please include the URL of page 404 etc. when testing.) The whole is as follows.
health_checker.py
# -*- coding: utf-8 -*-
import requests
import json
#Specify the URL of the site you want to monitor as an array
urls = ['https://www.rakuten.co.jp/']
def post_slack_h(c):
#Describe the Webhook URL obtained by enabling Incoming Webhooks in Slack
post_url = 'Slack webhook URL'
requests.post(post_url, data=json.dumps({
"username": "Watching over the site",
'text': c
}))
targets = []
def status_check(a="", b=""):
content = ""
for url in urls:
try:
s = requests.get(url, timeout=10).status_code
except requests.exceptions.ReadTimeout:
content += url + " Time out(10s)\n"
targets.append(url)
except requests.exceptions.ConnectionError:
content += url + " Connection error\n"
targets.append(url)
else:
if s == 200:
content += url + " " + str(s) + "\n"
else:
content += url + " " + str(s) + "\n"
targets.append(url)
if not targets:
print("All Green!!")
pass
else:
post_slack_h(content)
if __name__ == '__main__':
status_check()
Please check the official support below for the procedure to use Incoming webhook. https://slack.com/intl/ja-jp/help/articles/115005265063
To import an external package on AWS Lambda, you need to upload the code of the external package as well. Since the requests used this time are external packages, they will be put together in a zip file together with the main code.
#Save the package file in the directory containing the main code
$ pip install requests -t ./
#Compress to a zip file
$ zip -r health_checker ./*
This will create a zip file called "health_checker.zip" that contains the code and external packages.
Clicking the Lambda link on AWS will take you to the function list screen. From this screen, click "Create Function".
On the initial setting screen at the time of creation, set as follows
Optional: ** Create from scratch ** Function name: ** Optional ** Runtime: ** Python 3.7 **
Next, upload the file from the item "Function code" on the function edit screen. Make the settings as shown in the screen below. The point to pay particular attention to is the "handler". The handler is where you specify the function to execute in Lambda. The way to specify is ** file name (without extension). Function name (in the specified file) **. So, here it is "health_checker.status_check".
You've got the code you've created so far to run on Lambda. Finally, set the code to run on a regular basis.
Click "** + Add Trigger **" from the function edit screen. (The image below is the completed form that has already been added)
On the trigger creation screen, set as follows.
As for the schedule, let's specify that it will be executed every hour in Cron.
cron(0 0/1 * * ? *)
If you change the 0/1 part to 0/2, 0/5, etc., you can change the interval to every 2 hours or every 5 hours.
Monitoring multiple sites or sites that take a long time to load can exceed Lambda's default timeout of 3 seconds.
To avoid that, adjust the timeout time from "Basic settings" slightly below the middle of the Lambda function edit screen.
For those who want to be particular about the appearance of Slack notifications, you can use the attachment format to expand the range of expressions!
Please for reference https://qiita.com/m-nakada/items/643909ca14f306a74999
Thank you for your hard work
Recommended Posts