Logo : un manchot sur fond d'étoile rouge
Alexandre Norman
de choses et d’autres
  • Blog
  • Projets
  • CV
Projets Python
  • python-gantt
  • python-nmap
  • pyClamd
Catégories
  • Asterisk
  • django
  • Outils
  • Poules
  • Raspberry Pi
  • Archives
Tags
  • asterisk (1)
  • bash (2)
  • Bricolage (1)
  • debian (6)
  • django (2)
  • dovecot (1)
  • GPS (1)
  • LDAP (1)
  • lighttpd (1)
  • méthode (3)
  • mpd (1)
  • Nagios (1)
  • openvpn (1)
  • OSM (3)
  • outils (6)
  • Palettes (1)
  • postfix (1)
  • postgrey (1)
  • Poules (1)
  • Python (6)
  • Raspberry Pi (6)
  • raspbian (6)
  • selenium (1)
  • shellscript (2)
  • voip (1)
  • wazo (1)
  • wifi (1)

Surveillance de l’expiration d’un nom de domaine avec Nagios

Publication : 2016.03.20
Catégorie : Outils

Tags: Nagios outils Python

Le script

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import dateutil
import dateutil.parser
import dateutil.relativedelta
import subprocess
import sys

domain = sys.argv[1]

cmd = "whois  {0} | egrep 'Expiry Date|Expiration Date' ".format(domain)

output = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()

expirydate = bytes.decode(output.split(':')[1].strip())
try:
    expirydate = dateutil.parser.parse(expirydate)
except:
    print("Could not determine date {0}".format(expirydate))
    sys.exit(1)

today = datetime.datetime.now()

remaining = dateutil.relativedelta.relativedelta(expirydate, today)

if today + dateutil.relativedelta.relativedelta(months=+1) > expirydate:
    print('ALERT: Expiring domain name {0} expire on {1} (left: {2})'.format(domain, expirydate, remaining))
    sys.exit(2)
else:
    print('OK: domain name {0} expire on {1} (left: {2})'.format(domain, expirydate, remaining))
    sys.exit(0)

Pour l’installation, il faut ajouter la librairie dateutils :

pip install dateutils

Rendre le script exécutable :

chmod 755 check_DNS_expiry.py

L’utilisation est ensuite simple

check_DNS_expiry.py example.com

Selon l’état, le code de retour varie entre 0 (tout va bien), 1 (attention, problème potentiel) et 2 (critique, le nom de domaine expire dans moins d’un mois)

Intégration dans Nagios

Copier le fichier =check_DNS_expiry.py= dans le répertoire des plugins de Nagios (=libexec=). Dans le fichier de définition des commandes, ajouter :

define command{
        command_name    check_DNS_expiry
        command_line    $USER1$/check_DNS_expiry.py $HOSTNAME$
        }

Dans les définitions de service, ajouter :

define service{
        use                             generic-service
        hostgroup_name                  Webservers
        service_description             DN
        check_command                   check_DNS_expiry
        check_interval                  43200 # twice a day is enought
        }

Ajouter les hôtes à surveiller :

define host{
    use                     webserver
    host_name               example.com
    address                 example.com
    notes_url               http://example.com
    action_url              http://example.com
}
define host{
    use                     webserver
    host_name               example.org
    address                 example.org
    notes_url               http://example.org
    action_url              http://example.org
}

Et faire un groupe pour ajouter le service à tous :

define servicegroup{
    servicegroup_name       Webservers_DN
    alias                   Services web
    members                 example.com,DN,example.org,DN
}

Relancer Nagios, attendre que les services soit vérifiés et c’est bon  !

define host{ name webserver use generic-host parents Hespul_WAN register 0 max_check_attempts 2 }

Site géré avec Pelican - thème personnel
font dyslexia Increase text size Decrease text size Flux RSS Twitter