Create Your Own Free Website Checker

Do you have 1 more websites that you want to check to see if they are up? Sick of paid services or of the limited capability of free services? Well this article will teach you how to create your own free website checker.

It personally has been incredibly useful for me to be able to check my own websites, as often as I want, for free. I have many domains and many subdomains, and while I to maintain the sites well, occasionally they will go down, or have their SSL certificates expire etc, which will prevent users from seeing the site.

This article will teach you how to check your own websites using a little bit of Python code.

Step 1: Register For a free SendGrid account

You will need a way to message yourself. I personally prefer email. Unless you already have your own email server set up, you’ll likely need to use an API service to send emails. But also, even if you do have an email server set up, unless you are sending emails from a well known service, your emails might get sent to spam. So to avoid all these hassles, get a free account with an email sending API. I recommend SendGrid for this article, because I used Python to write the code for this article, and because SendGrid maintains free Python code to use their services. Go to sendgrid.com, and create your own free account.

Step 2: Get an API key from SendGrid

After signing up for a free account from SendGrid, you will still need to get an API key to use their services. This is easy to do. After registering, just search for something like generate API key, and then copy that key.

Step 3: Install SendGrid Pip Code

Most likely, sendgrid code won’t be installed by default in your Linux distro. But thankfully, SendGrid maintains free code with Pip. To install the necessary code, do:
sudo pip3 install sendgrid

Step 4: The Python Code To Check Websites And Send Email Alerts.

As I’ve mentioned, I have several domains. I use a modified version of the following code, to check my websites every hour from one of my VPSes. Make sure you have installed the sendgrid package as described in Step 3, otherwise the following code won’t work. Note also that in the following code, your ‘from_email’ must be the same email that you registered with for your SendGrid account, otherwise, I think SendGrid will refuse to send out emails. We will use the Python package requests. If you don’t have requests installed, you can install it with sudo pip3 install requests

Next, I will show you a function that I wrote, that will send out an email alert, about some domain which is down, and will also send the error status_code.

import requests
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

def website_down_send_email(domain, status_code):
    message = Mail(
        from_email='YOUR_EMAIL_ADDRESS',
        # for example
        # from_email='[email protected]'
        to_emails='EMAIL_ADDRESS_TO_SEND_ALERTS_TO',
        # for example
        # to_emails='[email protected]'
        subject=f'{domain} down with status code {status_code}!!!',
        html_content=f'<strong>{domain} down with status code {status_code}!!!</strong>')
    try:
        sg = SendGridAPIClient('SENDGRID_API_KEY')
        # for example, if your api key were, 'SG.exampleAPIkey' your sendgrid code might be something like
        # sg = 'SG.exampleAPIkey'
        # the following line actually sends out emails assuming that you used a proper API key
        response = sg.send(message)
        # the following 3 lines are optional. If you are interested, you can see various output about the email that was sent.
        # print(response.status_code)
        # print(response.body)
        # print(response.headers)
    except Exception as e:
        # if you like, you can print out the error message if there is one.
        # print(e.message)
        # Let's just assume there won't be an error, so skip this except block
        pass

You will need to decide which domains to check. Let’s assume you own 4 domains: example.com example2.com, example3.com, and example4.com. And let’s assume you have the subdomains sub1.example.com and sub2.example.com.

Usually, a successfully transmitted website page would mean we got a 200 OK status code. Though, I have some password protected subdomains, and any message code other than 401 would mean there was a problem with that subdomain (I wouldn’t want 200, because first the user would need to type in the appropriate username and password).

Let’s assume for the domains we want a status code of 200, and for the protected subdomains, we want a status code of 401.

To actually check the status of a domain, you will use requests. And if there are any errors (status code other than 200 for the domains and status other different than 401 for the protected domains, it will send out an email.)


domains_owned = [
    'example.com',
    'example2.com',
    'example3.com',
    'example4.com',
]

subdomains_protected_by_login = [
    'sub1.example.com',
    'sub2.example.com'
]

print('\n##############################')
for domain in domains_owned:
    domain_data = requests.get('https://' + domain)
    if domain_data.status_code != 200:
        status_code = domain_data.status_code
        website_down_send_email(domain, status_code)
    else:
        print('all is good with', domain)

print('\n##############################')
for domain in subdomains_protected_by_login:
    domain_data = requests.get('https://' + domain)
    if domain_data.status_code != 401:
        status_code = domain_data.status_code
        website_down_send_email(domain, status_code)
    else:
        print('all is good with', domain)

As I mentioned, the code presented in this article is something I use for nearly all of my domains and main subdomains. Below is the complete Python code for checking domains and sending out an alert if there is a problem with a domain.


import requests
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

def website_down_send_email(domain, status_code):
    message = Mail(
        from_email='YOUR_EMAIL_ADDRESS',
        # for example
        # from_email='[email protected]'
        to_emails='EMAIL_ADDRESS_TO_SEND_ALERTS_TO',
        # for example
        # to_emails='[email protected]'
        subject=f'{domain} down with status code {status_code}!!!',
        html_content=f'{domain} down with status code {status_code}!!!')
    try:
        sg = SendGridAPIClient('SENDGRID_API_KEY')
        # for example, if your api key were, 'SG.exampleAPIkey' your sendgrid code might be something like
        # sg = 'SG.exampleAPIkey'
        # the following line actually sends out emails assuming that you used a proper API key
        response = sg.send(message)
        # the following 3 lines are optional. If you are interested, you can see various output about the email that was sent.
        # print(response.status_code)
        # print(response.body)
        # print(response.headers)
    except Exception as e:
        # if you like, you can print out the error message if there is one.
        # print(e.message)
        # Let's just assume there won't be an error, so skip this except block
        pass

domains_owned = [
    'example.com',
    'example2.com',
    'example3.com',
    'example4.com',
]

subdomains_protected_by_login = [
    'sub1.example.com',
    'sub2.example.com'
]

print('\n##############################')
for domain in domains_owned:
    domain_data = requests.get('https://' + domain)
    if domain_data.status_code != 200:
        status_code = domain_data.status_code
        website_down_send_email(domain, status_code)
    else:
        print('all is good with', domain)

print('\n##############################')
for domain in subdomains_protected_by_login:
    domain_data = requests.get('https://' + domain)
    if domain_data.status_code != 401:
        status_code = domain_data.status_code
        website_down_send_email(domain, status_code)
    else:
        print('all is good with', domain)

Step 5: Set Up a cron job to check your sites every hour

I’m assuming you are going to use this code on a server like a VPS. But you can also use it on your own computer.
Let’s assume the username on your computer or server is exampleuser, and you saved the above Python code in a directory called checking_websites_are_up, in your ~/ directory. Furthermore, let’s assume you saved the above Python code as get_urls.py.

You will use the command crontab, to make a cron job to check your sites every hour

First type
crontab -e

Then at the bottom of the file, paste the following

# check to make sure websites are up
# once every hour at minute 10
10 * * * * python3 /home/exampleuser/checking_websites_are_up/get_urls.py
Edit the file with the Python code and directory name as you like. Just make sure that the command python3 and the full working directory there. Make sure to save the file after editing to make sure that the cron job will be created.

The above code means to run the website checking script every hour at the 10th minute. For me, checking once every hour is enough. But, if you’d liked to check every 5 minutes, that’s also possible. To make more customised cron jobs, I recommend visiting crontab.guru.

What did you think of this article? Do you have anything to add? Let’s discuss it in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *