banshee is under active development: collectors will keep changing, and a version you publish will likely need adjustments. For stable use, wait for version 1.0.0.

Network

banshee/network-collection/network/dns/

resolution

Whether a name resolves by a DNS server, what it answers and how fast, to see a name lost, a record changed or a slow server.

  • network
  • dns
  • resolution
  • name
  • record
  • latency

by banshee1 installation2 versions

Keen 1.0.0 · 67 lines
// Whether a name resolves, and how fast: one DNS query of the type chosen, sent to the server of "DNS server"
// (protocol:dns:server), or to the resolver of the poller's system when it is unset.
// A name that does not exist (NXDOMAIN), or that has no record of this type, is an answer: "Name does not
// resolve" raises on it. A server that refuses the query or does not answer in time is down: critical, rather
// than leaving the probe unreachable.
need('protocol:dns');

$name = variable('dns:name', 'text', array(
    'label' => 'Name to resolve',
    'help'  => 'The name the server must resolve, for instance www.example; for the type PTR, an IP address.',
));
$type = variable('dns:type', 'choice', array(
    'label'   => 'Record type',
    'help'    => 'The type of record asked for: A for an IPv4 address, AAAA for an IPv6 one, MX for the mail servers of a domain, PTR for the name of an address.',
    'choices' => array('A', 'AAAA', 'CNAME', 'MX', 'TXT', 'NS', 'SOA', 'PTR'),
    'default' => 'A',
));

output('resolved', 'text', array('label' => 'Name resolves'));
output('answer', 'text', array('label' => 'Answer'));
output('response-time', 'number', array('label' => 'Response time', 'unit' => 'ms'));

watch.state('not-resolved', 'Name does not resolve', 'resolved', array('critical' => array('no')));
watch.custom('unexpected-answer', 'Unexpected answer', array(
    'help'     => 'Critical when the answer you give is not among those the server returns: an address changed, a record taken over.',
    'settings' => array(
        'expected' => array('label' => 'Expected answer', 'form' => 'text'),
    ),
    'enabled'  => false,
));
watch.above('slow', 'Slow resolution', 'response-time', array('warning' => 500, 'critical' => 2000, 'for' => '3 runs'));

$found = dns.resolve($name, $type, errors=$err);
if ($err['code'] == 'connection_refused' || $err['code'] == 'timeout') {
    fail('critical', 'DNS server down', $err['text']);
}
if ($err['code'] != '') {
    fail('unreachable', 'DNS server unreachable', $err['text']);
}

$resolved = 'no';
if ($found['rcode'] == 'NOERROR' && list.count($found['answers']) > 0) {
    $resolved = 'yes';
}
// The answers as the server orders them; a refusal of the server by its code (NXDOMAIN, SERVFAIL).
$answer = text.join($found['answers'], ', ');
if ($found['rcode'] != 'NOERROR') {
    $answer = $found['rcode'];
}
value('resolved', $resolved);
value('answer', $answer);
value('response-time', $found['ms']);

// The expected answer is one of the answers, in any order: a name behind several addresses keeps them in
// turn. Names are compared in lower case, as DNS does.
$expected = text.lower(text.trim(watch.setting('unexpected-answer', 'expected')));
if ($expected != '') {
    $seen = false;
    foreach ($found['answers'] as $one) {
        if (text.lower($one) == $expected) {
            $seen = true;
        }
    }
    if (!$seen) {
        watch.raise('unexpected-answer', 'critical', $name . ' answers ' . $answer . ', not ' . $expected);
    }
}