def TestCDNAnswers(self, record_type, record, timeout=None):
    """Test to see that an answer returns correct IP's.

    Args:
      record_type: text record type for NS query (A, CNAME, etc)
      record: string to query for
      timeout: timeout for query in seconds (int)

    Returns:
      (is_broken, error_msg, duration)
    """
    is_broken = False
    error_msg = ''
    duration = -1
    host = ''
    if not timeout:
      timeout = self.health_timeout
    (response, duration, error_msg) = self.TimedRequest(record_type, record, timeout)
    if response and not rcode.to_text(response.rcode()) in FATAL_RCODES and response.answer:
      for answer in response.answer:
        for rdata in answer:
          if rdata.rdtype == 1:
            host = str(rdata.address)
            # ip, time_min, time_avg, time_max, lost
            duration = shell_ping.ping(host, times=5)[1] # the min ping time among the results
            break
    if duration == -1:
        is_broken = True
        error_msg = '%s is failed to resolve' % record.rstrip('.')
    else:
        error_msg = '%dms ping time to CDN host %s(%s)' % (duration, record.rstrip('.'), host)
    return (is_broken, error_msg, duration)
Exemple #2
0
        # read from file and remove duplicates
        hosts = list(set(get_hosts(filename)))
    else:
        # remove duplicates
        hosts = list(set(hosts))
        # append additional host from command line
        if append:
            hosts = list(set(get_hosts(filename) + hosts))

    if not hosts:
        sys.exit('No host to ping.')

    print 'host(ip)'.rjust(34), ' min_time'.rjust(8), 'avg_time'.rjust(8), 'max_time'.rjust(8), 'lost'.rjust(8)

    for x in hosts:
        ip, time_min, time_avg, time_max, lost = shell_ping.ping(x, times=5)
        result_time.update({x: [time_min, time_avg, time_max]})
        print ('%s(%s): ' % (x, ip)).rjust(35),\
              ('% 3sms' % (time_min)).rjust(8),\
              ('% 3sms' % (time_avg)).rjust(8),\
              ('% 3sms' % (time_max)).rjust(8),\
              ('% 2s%%' % (lost)).rjust(8)

    times = sorted(result_time.itervalues())
    # remove no reaching results
    times = [i[0] for i in times[:] if i[0] > 0]

    if times:
        for k, v in result_time.iteritems():
            if v[0] == times[0]:
                print '%s has min ping time: %s ms' % (k, v[0])