├── whois └── example.host.name.json ├── LICENSE ├── COPYING ├── zabbix-whois-discovery.py ├── zabbix-whois-check.py ├── README.md ├── step-by-step.md └── zabbix-whois-template.xml /whois/example.host.name.json: -------------------------------------------------------------------------------- 1 | { 2 | "domains": [ 3 | "example.com", 4 | "example.org", 5 | "example.net" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2018 Alexander Rennerfelt 2 | This work is free. You can redistribute it and/or modify it under the 3 | terms of the Do What The Fuck You Want To Public License, Version 2, 4 | as published by Sam Hocevar. See the COPYING file for more details. 5 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /zabbix-whois-discovery.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # read domains from a json array 4 | # and feed them into zabbix 5 | 6 | import sys 7 | import os 8 | import json 9 | from pyzabbix import ZabbixMetric, ZabbixSender 10 | 11 | wwwhost = sys.argv[1] 12 | asdf = '' 13 | idx = 0 14 | dir_path = os.path.dirname(os.path.realpath(__file__)) 15 | 16 | with open(dir_path+'/whois/'+wwwhost+'.json', 'r') as read_file: 17 | domains = json.load(read_file) 18 | 19 | for idx, thing in enumerate(domains['domains']): 20 | if idx == 0: 21 | asdf += '{"{#WHOISDOMAIN}":"'+thing+'"}' 22 | else: 23 | asdf += ',{"{#WHOISDOMAIN}":"'+thing+'"}' 24 | 25 | done = '{"data":['+asdf+']}' 26 | 27 | 28 | packet = ZabbixMetric(wwwhost, 'domain.expiry.item', done), 29 | result = ZabbixSender(use_config=True).send(packet) 30 | 31 | ## some tests 32 | #print(done) 33 | #print(packet) 34 | #print(result) 35 | 36 | ## another test 37 | #f = open('/tmp/zabbix-whois.txt', 'w') 38 | #f.write(done) 39 | #f.close() 40 | -------------------------------------------------------------------------------- /zabbix-whois-check.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | # checks when a domain is going to expire 4 | # and sends the time to zabbix 5 | # new version that reads from a json file 6 | 7 | import sys 8 | import os 9 | import json 10 | import whois 11 | from pyzabbix import ZabbixMetric, ZabbixSender 12 | 13 | wwwhost = sys.argv[1] 14 | dir_path = os.path.dirname(os.path.realpath(__file__)) 15 | 16 | with open(dir_path+'/whois/'+wwwhost+'.json', 'r') as read_file: 17 | domains = json.load(read_file) 18 | 19 | for domain in domains['domains']: 20 | try: 21 | w = whois.whois(domain) 22 | if isinstance(w.expiration_date, list): 23 | packet = ZabbixMetric(wwwhost, 'domain.expiry['+domain+']', w.expiration_date[0].timestamp()), 24 | else: 25 | if w.expiration_date: 26 | packet = ZabbixMetric(wwwhost, 'domain.expiry['+domain+']', w.expiration_date.timestamp()), 27 | result = ZabbixSender(use_config=True).send(packet) 28 | except: 29 | print("Error occurred while executing whois() for %s." % domain) 30 | 31 | ## some tests 32 | #print(w) 33 | #print(w.domain_name, w.expiration_date) 34 | #print(packet) 35 | #print(result) 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zabbix whois expiry auto-discovery and check 2 | 3 | ## Requirements 4 | 5 | - Python **3** 6 | - `pip3 install py-zabbix python-whois` 7 | 8 | ## Known issues 9 | 10 | All TLDs don't necessarily have whois servers. Please verify that it is possible to retrieve data directly in the terminal with the regular `whois` command before using this script. 11 | 12 | ## Confused? 13 | If the instructions below are too confusing, check the new [step by step instructions](step-by-step.md). 14 | 15 | ## Description 16 | 17 | This Zabbix template and associated scripts will automatically create and monitor items and triggers for domain expiry. 18 | 19 | It uses trapper items so the scheduling is handled entirely independently from Zabbix Server and can run from anywhere as long as it is able to send values to Zabbix Server. 20 | 21 | If `zabbix_sender` works for a host then this script should too. The py-zabbix module uses the configuration file for zabbix_agent to figure out where to send the trapper items to and such. That means the `ServerActive=`-parameter in *zabbix_agentd.conf* is used to figure out where to send the item. 22 | 23 | The default is `ServerActive=127.0.0.1` so this should work without any config changes needed if you run it directly on your zabbix server. 24 | 25 | `zabbix_sender` manpage: 26 | 27 | ## Installation 28 | 29 | Put the scripts on the host you want to perform the whois-checks from. Remember that you need Python 3 and to install the modules mentioned further up the readme. 30 | 31 | In the same folder as the scripts, create a folder called `whois` and create a json-file containing the domains you want to check. There's an example included in this repo. Make sure you name the file the same as the hostname in Zabbix because it is used as an argument by the scripts both to pick which json-file to read domains from and which host the data belongs to when sent to the Zabbix server. 32 | 33 | Add the Zabbix template to the host and schedule both the discovery and check scripts to run every day or however often you think is approperiate. 34 | 35 | Example with cron: 36 | 37 | ```crontab 38 | 39 | 20 12 * * * zabbix-whois-discovery.py example.host.name 40 | 30 12 * * * zabbix-whois-check.py example.host.name 41 | 42 | ``` 43 | 44 | ## Adding domains to existing host 45 | 46 | Just add more domains to the approperiate json-files and either run the scripts manually or wait for the cronjob. 47 | -------------------------------------------------------------------------------- /step-by-step.md: -------------------------------------------------------------------------------- 1 | # Step by step instructions 2 | Let's assume the following things: 3 | - You do this directly on your Zabbix server 4 | - You are running Ubuntu 20.04 LTS 5 | - The domains you want to monitor are __example.com__, __example.org__, and __example.net__ 6 | - The host inside of Zabbix that you want the items and triggers to be displayed under is called __server1.example.com__ 7 | 8 | Preparation: 9 | - Install whois by typing `sudo apt install whois`. Then type `whois example.com` to check if it even is possible to automate this for the domain you want, as we concluded could be a problem in issue number #4 10 | - Install pip by typing `sudo apt install python3-pip` 11 | - Install the dependencies this script has by then typing `sudo pip install py-zabbix python-whois` 12 | 13 | Instructions: 14 | - Import the file _zabbix-whois-template.xml_ in the Zabbix GUI and then add the template on the __server1.example.com__ host 15 | - Put the two .py-files from this repository anywhere on your server you want. For this example, let's just use your own users home directory _~/zabbix-whois-expiry/_ 16 | - Make the .py-files executable with `chmod +x zabbix-whois-discovery.py` and `chmod +x zabbix-whois-check.py` 17 | - Create the folder _~/zabbix-whois-expiry/whois_ and inside that folder create a file called _server1.example.com.json_ 18 | - Put the following inside of that .json-file: 19 | ``` 20 | { 21 | "domains": [ 22 | "example.com", 23 | "example.org", 24 | "example.net" 25 | ] 26 | } 27 | ``` 28 | 29 | Testing: 30 | - You can now test all of this manually by first typing `./zabbix-whois-discovery.py server1.example.com` to do the discovery, this will create the items and triggers in zabbix. 31 | - Then type `./zabbix-whois-check.py server1.example.com` to actually run the whois check and add data to the zabbix items. 32 | 33 | Scheduling: 34 | - Assuming the above tests worked we will now use cron to schedule your server to automatically check the domains once each day. 35 | - Type `crontab -e` to edit your users cron-file (this is often online just called your users crontab instead). 36 | - Insert the following at the bottom of the editor that will appear in your terminal: 37 | ``` 38 | 20 12 * * * $HOME/zabbix-whois-expiry/zabbix-whois-discovery.py server1.example.com 39 | 30 12 * * * $HOME/zabbix-whois-expiry/zabbix-whois-check.py server1.example.com 40 | ``` 41 | - This means that 20 minutes past 12 on every day the file _~/zabbix-whois-expiry/zabbix-whois-discovery.py_ with the argument _server1.example.com_ will automatically run. 42 | - Then at 30 minutes past 12 on every day the file _~/zabbix-whois-expiry/zabbix-whois-check.py_ with the argument _server1.example.com_ will automatically run. 43 | - (Look up "crontab syntax" on a search engine if you're curious what the three asterisk symbols mean) 44 | 45 | If you want to do this again for other hosts in zabbix just create new appropriately named .json-files and lines in your crontab and wait. If you no longer want to monitor a domain just remove it from the .json-file and wait, it will disappear automatically after a day or two. "Discovered" items in zabbix have an expiry time on them so this is why we run the discovery every day to remind zabbix of them as long as the domains exist in the .json-files. 46 | -------------------------------------------------------------------------------- /zabbix-whois-template.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 3.4 4 | 2018-06-04T11:01:41Z 5 | 6 | 7 | Templates 8 | 9 | 10 | 11 | 130 | 131 | 132 | --------------------------------------------------------------------------------