Skip to content
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

WIP: Added DNS check. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
configuration.yaml
env/
98 changes: 98 additions & 0 deletions ping_exporter/dns_ping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import types
import dns.resolver

wanted_attributes = ['dns_server', 'status_code', 'ok']

def _example_hook(resp, *args, **kwargs):
resp.example = 'example'


def _exception_handler(request, exception):
obj = types.SimpleNamespace()
for attr in wanted_attributes:
obj.__setattr__(attr, None)
if attr == 'url':
obj.url = request.url
if attr == 'exception':
obj.exception = exception.__doc__
if attr == 'ok':
obj.ok = False
return obj


def _serialize_return_values(blub):
for wauw in blub:
for attr in wanted_attributes:
if wauw[attr] is None:
wauw[attr] = 'Nan'
if wauw[attr] is True:
wauw[attr] = 1
if wauw[attr] is False:
wauw[attr] = 0


def dns_ping(dnsendpoints, all=True, serialize=True):
from ping_exporter.main import get_config_from_argv
data = []

for server,domain in dnsendpoints.items():
response_data = {}
query = dns.message.make_query(domain,dns.rdatatype.A)
res = dns.query.tcp(query,server,timeout=2)
response_data['dns_server'] = server
response_data['status_code'] = res.rcode()
response_data['ok'] = True if res.rcode() == 0 else False

data.append(response_data)

if all:
response_data = {}
for item in wanted_attributes:
response_data.update({item: None})
if item == 'dns_server':
response_data[item] = 'all'
if item == 'status_code':
response_data[item] = 'NaN'
if item == 'ok':
response_data[item] = sum([x['ok'] for x in data])
data.append(response_data)

if serialize:
_serialize_return_values(data)
return data

# for response in grequests.map(
# rs,
# exception_handler=_exception_handler,
# size=config.get('pool_size', 2)):
# response_data = {}
# if response is not None:
# for item in wanted_attributes:
# try:
# if item == 'elapsed':
# response_data.update(
# {item:
# response.__getattribute__(item).total_seconds()})
# continue
# response_data.update(
# {item: response.__getattribute__(item)})
# except AttributeError:
# response_data.update({item: None})
#
# data.append(response_data)
#
# if all:
# response_data = {}
# for item in wanted_attributes:
# response_data.update({item: None})
# if item == 'url':
# response_data[item] = 'all'
# if item == 'elapsed':
# response_data[item] = time
# if item == 'ok':
# response_data[item] = any([x['ok'] for x in data])
# data.append(response_data)
#
# if serialize:
# _serialize_return_values(data)
# return data
3 changes: 2 additions & 1 deletion ping_exporter/prometheus_metrics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ping_exporter.ping import pong, wanted_attributes
from ping_exporter.dns_ping import dns_ping


def generate_name(prefix, extra_tags=None):
Expand Down Expand Up @@ -68,6 +69,6 @@ def make_prometheus_text(return_values):


def prometheus_text(config):
return_values = pong(config['endpoints'])
return_values = pong(config['endpoints']) + dns_ping(config['dns'])
lines = make_prometheus_text(return_values)
return "\n".join(lines)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ flask==1.1.1
grequests==0.4.0
pyyaml==5.1.2
gunicorn==19.9.0
dnspython==2.1.0
22 changes: 22 additions & 0 deletions tests/test_dnsping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from ping_exporter.dns_ping import dns_ping


def test_dns_ping_without_endpoints_returns_empty_all_object(mocker):
mocker_argv = mocker.patch('ping_exporter.main.get_config_from_argv')
mocker_argv.return_value = ({}, None)

expected = [{'dns_server': 'all', 'status_code': 'NaN', 'ok': 0}]

assert expected == dns_ping({})

expected = []
assert expected == dns_ping({}, all=False)

def test_dns_ping_with_endpoints_returns_empty_all_object(mocker):
mocker_argv = mocker.patch('ping_exporter.main.get_config_from_argv')
mocker_argv.return_value = ({}, None)

rv = dns_ping({'8.8.8.8': 'google.com', '1.1.1.1': 'cloudflare.com'})

assert rv[0]['ok'] is 1
assert 0 == rv[0]['status_code']
2 changes: 1 addition & 1 deletion tests/test_prometheus_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_prometheus_text(mocker):
mock_main_get_config_from_argv = mocker.patch('ping_exporter.main.get_config_from_argv')
mock_main_get_config_from_argv.return_value = ({}, None)

text_lines = prometheus_text({'endpoints': []}).splitlines()
text_lines = prometheus_text({'endpoints': [], 'dns': {}}).splitlines()
assert text_lines[0].startswith('# TYPE')
assert text_lines[1].startswith('# HELP')
assert text_lines[2].startswith('probe_')