Site monitoring and alert notification with AWS Lambda + Python + Slack

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.)

Check site status and notify 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()

Use Incoming webhook to notify Slack

Please check the official support below for the procedure to use Incoming webhook. https://slack.com/intl/ja-jp/help/articles/115005265063

Prepare a zip file of code

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.

Create a function in AWS Lambda

Clicking the Lambda link on AWS will take you to the function list screen. From this screen, click "Create Function". スクリーンショット 2020-03-23 0.05.13.png

On the initial setting screen at the time of creation, set as follows

Optional: ** Create from scratch ** Function name: ** Optional ** Runtime: ** Python 3.7 **

スクリーンショット 2020-03-23 0.06.59.png

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".

スクリーンショット 2020-03-23 0.31.15.png

Scheduled execution settings

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) スクリーンショット 2020-03-23 0.42.36.png

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. スクリーンショット 2020-03-23 0.52.30.png

Remarks: Countermeasures for the error "time out after 3.00 seconds"

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. スクリーンショット 2020-03-23 1.02.24.png

Finally

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

Site monitoring and alert notification with AWS Lambda + Python + Slack
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Sample to send slack notification with python lambda
Dynamic HTML pages made with AWS Lambda and Python
Make ordinary tweets fleet-like with AWS Lambda and Python
[Python x Zapier] Get alert information and notify with Slack
Install pip in Serverless Framework and AWS Lambda with Python environment
[AWS] Using ini files with Lambda [Python]
Traffic monitoring with Kibana, ElasticSearch and Python
[AWS] Link Lambda and S3 with boto3
Connect to s3 with AWS Lambda Python
Touch AWS with Serverless Framework and Python
Python + Selenium + Headless Chromium with aws lambda
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
LINE BOT with Python + AWS Lambda + API Gateway
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
Amazon API Gateway and AWS Lambda Python version
AWS CDK with Python
Deploy Python3 function with Serverless Framework on AWS Lambda
Create a Layer for AWS Lambda Python with Docker
I want to AWS Lambda with Python on Mac!
Put TensorFlow in P2 instance with pip3
Easy server monitoring with AWS Lambda (Python) and result notification in Slack
Sample to send slack notification with python lambda
Stop an instance with a specific tag in Boto3
Programming with Python and Tkinter
Encryption and decryption with Python
Operate TwitterBot with Lambda, Python
Python and hardware-Using RS232C with Python-
[Python] Scraping in AWS Lambda
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Post to slack with Python 3
Send experiment results (text and images) to slack with Python
[Python] Folder monitoring with watchdog
AWS Lambda with PyTorch [Lambda import]
Post images of Papillon regularly on Python + AWS Lambda + Slack
Create API with Python, lambda, API Gateway quickly using AWS SAM
python with pyenv and venv
Easily build network infrastructure and EC2 with AWS CDK Python
Works with Python and R
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
Manipulating kintone data with Python & C Data ODBC Driver from AWS Lambda
I compared Node.js and Python in creating thumbnails using AWS Lambda
Make a scraping app with Python + Django + AWS and change jobs
Prepare an environment to use OpenCV and Pillow with AWS Lambda
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
[Python] I wrote a REST API using AWS API Gateway and Lambda.
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
Summary if using AWS Lambda (Python)
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
[AWS] Create API with API Gateway + Lambda
Scraping with Python, Selenium and Chromedriver
Face detection with Lambda (Python) + Rekognition
Scraping with Python and Beautiful Soup
Write AWS Lambda function in Python
Run Python on Schedule on AWS Lambda
JSON encoding and decoding with python