Skip to content

Instantly share code, notes, and snippets.

@leblanc-simon
Last active September 25, 2016 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leblanc-simon/ac67a9e612e6fc3edadbe741691038b3 to your computer and use it in GitHub Desktop.
Save leblanc-simon/ac67a9e612e6fc3edadbe741691038b3 to your computer and use it in GitHub Desktop.

Sonde Nagios de vérification de site web

Cette sonde permet de vérifier des sites web :

  • vérification du HTTP Status Code (2xx)
  • vérification de la présence d'un terme dans la page
  • vérification du temps de chargement

Utilisation du script

./check_url.py -u [url à tester] [-t [terme à rechercher dans la page]] [-s [temps maximal pour un succès]] [-w [temps maximal pour un warning]]

Informations complémentaires

  • Une page est considéré comme OK si :
    • le code HTTP est 2xx
    • le terme recherché est bien présent dans la page
    • le temps de chargement est inférieur au temps maximal de succès (par défaut 1 seconde)
  • Le temps maximal pour le succès est par défaut de 1 seconde
  • Le temps maximal pour le warning est par défaut de 10 secondes
  • Au delà une erreur critique est renvoyée
  • La page est appelée avec en préférence la langue française
#!/usr/bin/env python3
#
# Copyright (c) 2016 Simon Leblanc <contact@leblanc-simon.eu>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
from time import time
import sys
import optparse
VERSION = "0.1"
parser = optparse.OptionParser(version="Check URL %s" % VERSION)
parser.add_option("-u", "--url", dest='url', default=None, help="The URL to check")
parser.add_option("-t", "--term", dest='search_term', default=None, help="The term to search in page")
parser.add_option("-s", "--success-time", dest='success_time', default=1, help="The number of second for a success")
parser.add_option("-w", "--warning-time", dest='warning_time', default=10, help="The number of second for a warning")
opts, args = parser.parse_args()
try:
search_term = opts.search_term
success_time = float(opts.success_time)
warning_time = float(opts.warning_time)
except:
print('UNKNOWN - Exception while parse options', sys.exc_info()[0])
sys.exit(3)
if None == opts.url:
print('UNKNOWN - No URL to check...')
sys.exit(3)
req = Request(url=opts.url, headers={'User-Agent': 'Shinken - check_url' + VERSION, 'Accept-Language': 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2,de;q=0.2'})
try:
start_time = time()
response = urlopen(req)
end_time = time()
page = response.read().decode(response.headers.get_content_charset('utf-8'))
if search_term != None and not search_term in page:
print('CRITICAL - Search term not found ', search_term)
sys.exit(2)
except HTTPError as e:
print('CRITICAL - Bad status code ', e.code)
sys.exit(2)
except URLError as e:
print('UNKNOWN - ', e.reason)
else:
if (end_time - start_time) < success_time:
print('SUCCESS - Website is up|', end_time - start_time)
sys.exit(0)
elif (end_time - start_time) < warning_time:
print('WARNING - Website up but long|', end_time - start_time)
sys.exit(1)
else:
print('CRITICAL - Website up but very long|', end_time - start_time)
sys.exit(2)
define command {
command_name check_url
command_line $PLUGINSDIR$/check_url.py -u $ARG1$ -t $ARG2$ -s $ARG3$ -w $ARG4$
}
define host {
use generic-host,linux-snmp,ssh,check_url
contact_groups admins
host_name localhost
address localhost
_check_url leblancio $(https://leblanc.io)$$(récupérer)$$(1)$$(10)$, markdown $(https://md.leblanc.io/)$$(markdownConvertor)$$(1)$$(10)$
}
define service {
use generic-service
host_name check_url
register 0
service_description Check URL $KEY$
check_command check_url!$VALUE1$!$VALUE2$!$VALUE3$!$VALUE4$
duplicate_foreach _check_url
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment