Skip to content

Commit

Permalink
Merge pull request #3069 from DataDog/oceane/http_check_reverse_conte…
Browse files Browse the repository at this point in the history
…nt_match

[HTTP CHECK] Add an Option to mark check as DOWN if a content is found
  • Loading branch information
oceanef authored Jan 24, 2017
2 parents 4d273b6 + 8d335a1 commit 7fe1b3d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 20 deletions.
53 changes: 33 additions & 20 deletions checks.d/http_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def _load_conf(self, instance):
headers.update(config_headers)
url = instance.get('url')
content_match = instance.get('content_match')
reverse_content_match = _is_affirmative(instance.get('reverse_content_match', False))
response_time = _is_affirmative(instance.get('collect_response_time', True))
if not url:
raise Exception("Bad configuration. You must specify a url")
Expand All @@ -186,15 +187,32 @@ def _load_conf(self, instance):
allow_redirects = _is_affirmative(instance.get('allow_redirects', True))

return url, username, password, method, data, http_response_status_code, timeout, include_content,\
headers, response_time, content_match, tags, ssl, ssl_expire, instance_ca_certs,\
headers, response_time, content_match, reverse_content_match, tags, ssl, ssl_expire, instance_ca_certs,\
weakcipher, ignore_ssl_warning, skip_proxy, allow_redirects

def _check(self, instance):
addr, username, password, method, data, http_response_status_code, timeout, include_content, headers,\
response_time, content_match, tags, disable_ssl_validation,\
response_time, content_match, reverse_content_match, tags, disable_ssl_validation,\
ssl_expire, instance_ca_certs, weakcipher, ignore_ssl_warning, skip_proxy, allow_redirects = self._load_conf(instance)
start = time.time()

def send_status_up(logMsg):
self.log.debug(logMsg)
service_checks.append((
self.SC_STATUS, Status.UP, "UP"
))

def send_status_down(loginfo, message):
self.log.info(loginfo)
self.log.debug("Content returned:\n%s" % content)
if include_content:
message += '\nContent: {}'.format(content[:CONTENT_LENGTH])
service_checks.append((
self.SC_STATUS,
Status.DOWN,
message
))

service_checks = []
try:
parsed_uri = urlparse(addr)
Expand Down Expand Up @@ -296,26 +314,21 @@ def _check(self, instance):
# r.text is the response content decoded by `requests`, of type `unicode`
content = r.text if type(content_match) is unicode else r.content
if re.search(content_match, content, re.UNICODE):
self.log.debug("%s is found in return content" % content_match)
service_checks.append((
self.SC_STATUS, Status.UP, "UP"
))
if reverse_content_match:
send_status_down("%s is found in return content with the reverse_content_match option" % content_match,
'Content "%s" found in response with the reverse_content_match' % content_match)
else:
send_status_up("%s is found in return content" % content_match)

else:
self.log.info("%s not found in content" % content_match)
self.log.debug("Content returned:\n%s" % content)
message = 'Content "%s" not found in response.' % content_match
if include_content:
message += '\nContent: {}'.format(content[:CONTENT_LENGTH])
service_checks.append((
self.SC_STATUS,
Status.DOWN,
message
))
if reverse_content_match:
send_status_up("%s is not found in return content with the reverse_content_match option" % content_match)
else:
send_status_down("%s is not found in return content" % content_match,
'Content "%s" not found in response.' % content_match)

else:
self.log.debug("%s is UP" % addr)
service_checks.append((
self.SC_STATUS, Status.UP, "UP"
))
send_status_up("%s is UP" % addr)

if ssl_expire and parsed_uri.scheme == "https":
status, msg = self.check_cert_expiration(instance, timeout, instance_ca_certs)
Expand Down
5 changes: 5 additions & 0 deletions conf.d/http_check.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ instances:
# content_match: 'In Stock'
# content_match: '^(Bread|Apples|Very small rocks|Cider|Gravy|Cherries|Mud|Churches|Lead) float(s)? in water'

# The (optional) reverse_content_match parameter will allow the content_match
# to work the other way around. That means that the check will report
# as DOWN if the string is found.
# reverse_content_match: false

# If your service uses basic authentication, you can optionally
# specify a username and password that will be used in the check.
#
Expand Down
47 changes: 47 additions & 0 deletions tests/checks/integration/test_http_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,40 @@
'timeout': 1,
'check_certificate_expiration': False,
'content_match': u'メインページ'
}, {
'name': 'cnt_mismatch_unicode',
'url': 'https://ja.wikipedia.org/',
'timeout': 1,
'check_certificate_expiration': False,
'content_match': u'メインペーー'
}, {
'name': 'cnt_mismatch_reverse',
'url': 'https://github.com',
'timeout': 1,
'reverse_content_match': True,
'check_certificate_expiration': False,
'content_match': 'thereisnosuchword'
}, {
'name': 'cnt_match_reverse',
'url': 'https://github.com',
'timeout': 1,
'reverse_content_match': True,
'check_certificate_expiration': False,
'content_match': '(thereisnosuchword|github)'
}, {
'name': 'cnt_mismatch_unicode_reverse',
'url': 'https://ja.wikipedia.org/',
'timeout': 1,
'reverse_content_match': True,
'check_certificate_expiration': False,
'content_match': u'メインペーー'
}, {
'name': 'cnt_match_unicode_reverse',
'url': 'https://ja.wikipedia.org/',
'timeout': 1,
'reverse_content_match': True,
'check_certificate_expiration': False,
'content_match': u'メインページ'
}
]
}
Expand Down Expand Up @@ -227,6 +261,19 @@ def test_check(self):
self.assertServiceCheckOK("http.can_connect", tags=tags)
tags = ['url:https://ja.wikipedia.org/', 'instance:cnt_match_unicode']
self.assertServiceCheckOK("http.can_connect", tags=tags)
tags = ['url:https://ja.wikipedia.org/', 'instance:cnt_mismatch_unicode']
self.assertServiceCheckCritical("http.can_connect", tags=tags)
self.assertServiceCheckOK("http.can_connect", tags=tags, count=0)
tags = ['url:https://github.com', 'instance:cnt_mismatch_reverse']
self.assertServiceCheckOK("http.can_connect", tags=tags)
self.assertServiceCheckCritical("http.can_connect", tags=tags, count=0)
tags = ['url:https://github.com', 'instance:cnt_match_reverse']
self.assertServiceCheckCritical("http.can_connect", tags=tags)
tags = ['url:https://ja.wikipedia.org/', 'instance:cnt_mismatch_unicode_reverse']
self.assertServiceCheckOK("http.can_connect", tags=tags)
tags = ['url:https://ja.wikipedia.org/', 'instance:cnt_match_unicode_reverse']
self.assertServiceCheckCritical("http.can_connect", tags=tags)
self.assertServiceCheckOK("http.can_connect", tags=tags, count=0)

self.coverage_report()

Expand Down

0 comments on commit 7fe1b3d

Please sign in to comment.