Skip to content

Instantly share code, notes, and snippets.

@anroots
Created December 13, 2015 13:20
Show Gist options
  • Save anroots/636f71366a21fa62fa90 to your computer and use it in GitHub Desktop.
Save anroots/636f71366a21fa62fa90 to your computer and use it in GitHub Desktop.
Sensu / Nagios check that detects .ee domains that are about to expire
#!/usr/bin/env python
# Sensu / Nagios check for detecting .ee domains that are about to expire
# Uses the JSON HTTP WHOIS API of internet.ee (Estonian TLD registrar).
#
# JSON API: http://rwhois.internet.ee
# Documentation: http://www.eis.ee/registrars/new-registry-system
#
# Note: This is a quick 30-minute script: untested, with bugs.
#
# Author: Ando Roots <ando@sqroot.eu> 2015-12-13
# Licence: MIT
import argparse
import urllib, json
from datetime import datetime
exit_status = 0
def fail(status):
global exit_status
if status > exit_status:
exit_status = status
def query_whois(domain):
url = "http://rwhois.internet.ee/v1/%s.json" % domain
response = urllib.urlopen(url)
return json.loads(response.read())
def get_days_valid(whois_response):
return (datetime.strptime(whois_response['expire'], '%Y-%m-%d') - datetime.now()).days
def is_status_ok(statuses):
return len(statuses) == 1 and statuses[0] == 'ok (paid and in zone)'
def check_domain(domain, args):
whois_response = query_whois(domain)
if whois_response['name'] != domain:
print "CRITICAL: Domain name mismatch: expected %s, was %s" % (domain, whois_response['name'])
fail(2)
num_days_valid = get_days_valid(whois_response)
alert_level = "OK"
if num_days_valid <= args.warning:
fail(1)
alert_level = "WARNING"
if num_days_valid <= args.critical:
fail(2)
alert_level = "CRITICAL"
print "%s: %s expires in %s days; " % (alert_level, domain, num_days_valid)
if not is_status_ok(whois_response['status']):
print "WARNING: %s has invalid status: %s" % (domain, whois_response['status'])
def get_cli_args():
parser = argparse.ArgumentParser(description='Check for .ee (Estonia) domain expiry and meta-info.')
parser.add_argument('domains', metavar='d', nargs='+',
help='a comma-separated list of .ee domain names to check')
parser.add_argument('-w', '--warning', default=30, type=int, help='warning threshold in days')
parser.add_argument('-c', '--critical', default=7, type=int, help='critical threshold in days')
return parser.parse_args()
def run():
args = get_cli_args()
for domain in args.domains:
check_domain(domain, args)
exit(exit_status)
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment