-
Notifications
You must be signed in to change notification settings - Fork 814
[dns] Allow expected address to be checked in dns_check. #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5e3ceaa
5f6a781
aa11e01
65787fe
474a1f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,23 +52,30 @@ def query_dns(self, instance, timeout): | |
resolver.nameservers = [nameserver] | ||
|
||
record_type = instance.get('record_type', 'A') | ||
expected_results = instance.get('addresses', []) | ||
|
||
status = AgentCheck.CRITICAL | ||
start_time = time.time() | ||
try: | ||
self.log.debug('Querying "%s" record for hostname "%s"...' % (record_type, hostname)) | ||
answer = resolver.query(hostname, rdtype=record_type) | ||
assert(answer.rrset.items[0].to_text()) | ||
resolved_results = map(lambda x: x.to_text(), answer.rrset.items) | ||
end_time = time.time() | ||
|
||
except dns.exception.Timeout: | ||
self.log.error('DNS resolution of %s timed out' % hostname) | ||
self.service_check(self.SERVICE_CHECK_NAME, status, tags=self._get_tags(instance)) | ||
raise | ||
except Exception: | ||
self.log.exception('DNS resolution of %s has failed.' % hostname) | ||
self.service_check(self.SERVICE_CHECK_NAME, status, tags=self._get_tags(instance)) | ||
except Exception as err: | ||
self.log.exception(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing the exception is not needed, |
||
self.service_check(self.SERVICE_CHECK_NAME, status, tags=self._get_tags(instance), message=err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know what kind of exception is going to be raised ? I'm wondering if seeing the python exception is useful. |
||
raise | ||
else: | ||
if len(expected_results) > 0: | ||
missing_values = list(set(expected_results, resolved_results)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing_values = [r for r in expected_results if r not in resolved_results] maybe ? Unless you have a better way to do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your method is also way easier to read/understand. 👍 |
||
if len(missing_values) > 0: | ||
self.log.error('DNS resolution of %s did not contain expected address(es) %s.' % (hostname, ", ".join(missing_values))) | ||
self.service_check(self.SERVICE_CHECK_NAME, status, tags=self._get_tags(instance)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably remove the if len(missing_values) > 0:
self.log.error('DNS resolution of %s did not contain expected address(es) %s.' % (hostname, ", ".join(missing_values)))
self.service_check(self.SERVICE_CHECK_NAME, status, tags=self._get_tags(instance))
else:
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=self._get_tags(instance))
self.gauge('dns.response_time', end_time - start_time, tags=tags) |
||
if end_time - start_time > 0: | ||
self.gauge('dns.response_time', end_time - start_time, tags=tags) | ||
self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=self._get_tags(instance)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't fail when the first item is the empty string, whereas it was failing previously. Maybe filter this resolved_results to remove empty strings and later check that resolved_results != [] ?