DNS: Is your domain healthy?
When your website goes down, you risk losing customers, revenue, and credibility. When there is an issue — it is critical to know as quickly as possible in order to resolve the problem. In this post, we’ll build an automated Runbook to check the DNS of our website at regular intervals.
We all know the joke about outages and DNS- we put it on t-shirts, we’ve written poetry about it. Sometimes, no matter what we think the cause of our outage might be, it ends up being a DNS issue.
In this post, we’re going to build a simple DNS health check API using unSkript, using our free Runbook Sandbox.
What is unSkript?
unSkript is a DevOps/SRE automation platform that allows for fast and easy automatic RunBook creation. Built on top of Jupyter Notebooks, it uses Python to automate and simplify your daily DevOps workflows. It’s built on an open source library, and we have hundreds of Actions ready for you to connect into your RunBooks (and more Actions are being created every day!).
In this example, we will build a xRunBook with two steps (or Actions):
1. Check the DNS of a domain,
2. If there is an issue — we’ll send a message on Slack.
If you’d like to build your own by following the steps below, you can sign up for free at unSkript.com.
Creating our DNS check Runbook
- Sign in to the unSkript Sandbox. If you have not used the Sandbox before, walk through the short tutorial on how to use it. It will set up some of the features (like a proxy) that you’ll need.
2. Click xRunBooks from the top nav, and click the
3. This will open a dialogue to name your runbook and select a proxy (that you set up during the onboarding flow).
I named mine `check_dns_runbook`, gave a short description and assigned my proxy.
4. Now, we have our Runbook, and are ready to build. unSkript is built on top of Jupyter Notebooks, so if you are have used Jupyter before, this should look familiar (if not — don’t worry — it’s just Python).
5. In the top menu, click
Parameters. Parameters are the input key:value pairs for the Runbook — much like API inputs to customize each call of the API. Add a Parameter named url, and give it a default value to unskript.com (You can use your own domain, of course).
6. We have our url that we’d like to check — let’s add the code to actually check the DNS. Create an action by clicking Add -> Action from the top menu.
7. Name this action check_dns, and paste in the following Python code:
import dns import dns.resolver def get_dns(url): try: result = dns.resolver.query(url, ‘A’) for ipval in result: print(‘IP’, ipval.to_text()) except: result = “none” print(result) return result result = get_dns(url)
This is a simple DNS checker. If the DNS resolves, the variable result holds the IP address. If it fails, result is “none.”
7. With unSkript, you can test each action individually (even before the full Runbook is completed!). Click “Run Action” at the top of this Action. Assuming unskript.com’s DNS can be resolved (and you can bet we are running this Runbook internally, and working to keep the site up!), you’ll get an IP back.
8. Now we have DNS details, but what we want to do is send a message on Slack that says “hey the website is down!” (when the website is down, of course). To do this, we’ll use an existing Action from the unSkript library. Searching in the Actions navigation on the right, we’ll drag the “Post Slack Message” Action into our xRunbook.
9. This Action requires connecting to Slack. This requires a Slack app. that is connected to the Slack Channel you wish to broadcast the message on. We’ve written up a guide on how to create an app with Slack.
10. Now, we’ll create a Slack Credential at unSkript by clicking Credentials, and adding a Credential. Give it a name, and paste in your OAuth token from the previous step. (You can manage all of your Credentials by clicking “Proxies” and then choosing a proxy.)
11. You’ll need to pick the channel you’re sending the message to, and the message itself. These inputs are Python, so if you are adding a string, add quotes.
If we were to run this Runbook now, every time it runs, we’d get a message that the url is failing (even if everything is fine!). So, we only want the Slack Action to fire when the check_DNS Action fails. We can use the “Start Condition” variable to only run the Slack code when “`result is “none”“`.
Now the Post Slack message will only run, if the DNS Action has failed -removing any noise from the alarm.
Scheduling the RunBook
Having a RunBook that tests DNS of a website is great, but it needs to run on a regular cadence. unSkript can schedule RunBook runs. Close the RunBook (be sure to save it), and choose Events from the top navigation (and then choose schedule).
The following setting will run this xRunBook every 5 minutes:
Choose your url, and your RunBook will monitor the DNS health of your website for you. You can monitor the executions of the RunBook, and after a few hours, our DNS has been holding up quite well (as it should be):
In this post, we have built an alarm for our website’s DNS. Every 5 minutes, our RunBook checks the sites DNS, and if it fails to find an IP address, sends an alert to Slack so that the engineering team is alerted to the issue.
We build a custom Action using Python to check the DNS, but imported an existing Action from the unSkript library to send the Slack message. We have many pre-built actions for many leading platforms, and we love submissions of Actions and xRunBooks to our Open Source Collection. Try our unSkript for free, and feel free to reach out in our Slack Community if you have any questions.