-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathcontent_check.py
executable file
·74 lines (62 loc) · 2.39 KB
/
content_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
#
#
# usage: content_check.py [-h] [--timeout TIMEOUT] url pattern
#
# Rackspace Monitoring plugin to check a URL for a regular expression. Useful if
# the URL you need to check is not publicly accessible, but can be reached by
# another entity. Returns the metric 'found' with a value of 'yes' or 'no'.
#
# positional arguments:
# url url to check
# pattern regex to check for
#
# optional arguments:
# -h, --help show this help message and exit
# --timeout TIMEOUT timeout in seconds (default 5)
#
#
# content_check.py - Rackspace Cloud Monitoring plugin
# Copyright (C) 2015 Carl George
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Rackspace Monitoring plugin to check a URL for a regular expression. Useful
if the URL you need to check is not publicly accessible, but can be reached by
another entity. Returns the metric 'found' with a value of 'yes' or 'no'.
"""
from __future__ import print_function
import argparse
import re
try:
from urllib.request import urlopen
from urllib.error import HTTPError
except ImportError:
from urllib2 import urlopen, HTTPError
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('url', help='url to check')
parser.add_argument('pattern', help='regex to check for')
parser.add_argument('--timeout', type=int, default=5, help='timeout in seconds (default 5)')
args = parser.parse_args()
if not args.url.startswith('http'):
args.url = 'http://{0}'.format(args.url)
try:
request = urlopen(args.url, timeout=args.timeout)
page = request.read().decode('utf-8')
except HTTPError as e:
raise SystemExit('{0} {1} ({2})'.format(e.code, e.reason, args.url))
m = re.search(args.pattern, page)
if m:
print('status ok\nmetric found string yes')
else:
print('status err\nmetric found string no')