|
#!/usr/bin/env python3 |
|
# |
|
# Copyright (c) 2016 Simon Leblanc <[email protected]> |
|
# |
|
# 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) |