Relic Solution: Monitor Domain Expiration Dates with Synthetics

Monitoring Domain Expiration with Synthetics

You may have seen the excellent and very popular script used to monitor SSL certificate expiration dates over here:

This script is written with the goal of working side by side with that, but you are welcome to join the two into one script though (if you do, please post that in a comment below)…

Almost if not as important as monitoring SSL expiration, is monitoring Domain Expiration. My hope is that you don’t need this script. That you already know your domains are expiring before this script will tell you. But just in case you don’t - the script below can help!

Requirements

  • To run this script you do need an API key from jsonwhois.io - You can set up a free account with access to 250 calls to the API per month.
    • This should be fine, my recommendation is to have this script run once per day, so 30 times per month.
  • A Synthetics Pro subscription. This script runs in an API Test monitor, you will need access to advanced monitor types to run this.

The Script

var assert = require("assert");

var domain = "{YOUR DOMAIN HERE}";
var apiKey = "{YOUR JSONWHOIS.IO API KEY}";

var options = {
  uri:
    "https://api.jsonwhois.io/whois/domain?key=" + apiKey + "&domain=" + domain,

  headers: {
    Accept: "application/json",
  },
};


function callback(err, response, body) {
  var result = JSON.parse(body);
  var expiration = result.result.expires;

//convert string to expiration date in ms
  var newExp = Date.parse(expiration);
  var now = Date.now();

//Calculate time between now & expiration date + convert to days count.
  var daysToExp = (newExp - now) / 86400000; 

  $util.insights.set('checkedDomain', domain);
  $util.insights.set("domainExpirationDate", newExp);
  $util.insights.set("daysToExpiration", daysToExp);
}
$http.get(options, callback);

Finding the results

This script uses the built in Insights Utility to add custom attributes to the checks running it.

The 3 attributes sent are: custom.checkedDomain, custom.domainExpirationDate, custom.daysToExpiration.

  • Checked Domain - Posts to Insights the domain for which this monitor run is monitoring.
  • Domain Expiration Date - Posts to Insights the epoch time in milliseconds at which the domain name is expiring. Whether you view this in Insights or New Relic One, this should auto-format to a readable date rather than epoch timestamp.
  • Days to Expiration - Posts to Insights the result of some in-script math to determine how many days are between now and the domain name’s expiration.

SELECT latest(custom.daysToExpiration) FROM SyntheticCheck WHERE monitorName in ('Personal Domain Exp', 'New Relic Domain Exp') SINCE 1 HOUR AGO FACET custom.checkedDomain 

As you can tell by the query - I have 2 monitors set up with this script, one hitting a personal test site, the other hitting newrelic.com - you are welcome to adapt this script to run multiple domains per monitor

Actioning on these results

As you saw above, this data is all queryable in NRQL - so you could pull this out via the Query API, you could build a dashboard (like below), you could set up some alerting on the data. How you action this info is entirely up to you, depending on the needs you have.

A sample dashboard showing the data inserted to Insights can be seen here:

Most importantly highlighting in the top left how many domain names are expiring in the next 30 days.

Expand…

This is a simple script put together in an hour, I am not a programmer, you are likely going to be able to smooth this out more than I could, and I’d love to see what you come up with.

If you do take this script and edit it, do post your updates back here to let us all know how you are using it :smiley:

4 Likes

I :heart: the way we use information from other scripts to build out new solutions @RyanVeitch. Shows how powerful the forum can be :+1:

3 Likes