Exemple #1
0
    from ringtools import ring, result


def callback(result):
    # callback to analyse the results: this function is called after running
    # the command on each hos. If the command exited ok we select the average
    # pingtime from the result and add it to the dataset
    if result.get_exitcode() == 0:
        # quick 'n dirty ;-)
        avg = result.get_stdout()[-1].split(" ")[3].split("/")[1]
        # add the value we grabbed from the output to the node's result
        result.add_value("avg", float(avg))


# pick 10 random nodes
nodes = ring.pick_nodes(count=10)
print "picked nodes: %s" % ", ".join(nodes)

# run the ping command on the selected hosts with 'callback' as the analyse function
cmd_result = ring.run_command("ping -c1 -q ring.nlnog.net", nodes, analyse=callback)

# get the succesful and failed results
s_res = cmd_result.get_successful_results()
f_res = cmd_result.get_failed_results()

# print the number and names of succesful and failed hosts
print "results: %d nodes ok (%s), %d nodes failed (%s)." % (
    len(s_res.get_results()),
    ", ".join(s_res.get_hostnames()),
    len(f_res.get_results()),
    ", ".join(f_res.get_hostnames()),
    sys.path.append('..')
    from ringtools import ring


def callback(result):
    # callback to analyse the results: this function is called after running
    # the command on each hos. If the command exited ok we select the average 
    # pingtime from the result and add it to the dataset
    if result.get_exitcode() == 0 and len(result.get_stdout()) > 0:
        # add the value we grabbed from the output to the node's result
        result.add_value('dig', result.get_stdout()[-1])


# the command to be executed
COMMAND = "dig +short -t a www.facebook.com"

# pick random nodes
nodes = ring.pick_nodes(50)

# run the 'dig' command on the selected hosts with 'callback' as the analyse function
# using www.facebook.com as a target since it has many A labels
cmd_result = ring.run_command(COMMAND, nodes, analyse=callback)

# get the data for the 'dig' field grouped by value
grouped = cmd_result.get_value_grouped('dig', True)
print "results for '%s' grouped by return value:" % COMMAND
print " #  result            hosts"
for (value, hosts) in grouped:
    print "%2d: %-16s %s" % (len(hosts), value, ", ".join(hosts))
    
Exemple #3
0
def main():
    parser = argparse.ArgumentParser(
        description="Run ping on the NLNOG ring.",
        epilog="Visit https://ring.nlnog.net for more information",
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument(
        "-6", "--ipv6", help="enforce IPv6", 
        action="store_const", dest="ipv6", 
        const=True, default=False)

    parser.add_argument(
        "-c", "--count", 
        help="the number of nodes to ping from", 
        action="store", dest="count", 
        default=10, type=int)
    
    parser.add_argument(
        "-C", "--pingcount", 
        help="the number of ping requests", 
        action="store", dest="pingcount", 
        default=1, type=int)

    parser.add_argument(
        "-e", "--errors", 
        help="detailed error reporting", 
        action="store_const", dest="errors", 
        default=False, const=True)
    
    parser.add_argument(
        "-n", "--include-node", help="include specific node", 
        action="append", dest="in_nodes",
        default=None, metavar="node")
   
    parser.add_argument(
        "-N", "--exclude-node", help="exclude specific node", 
        action="append", dest="ex_nodes", 
        default=None, metavar="node")
    
    parser.add_argument(
        "-o", "--include-country", help="include specific country", 
        action="append", dest="in_countries",
        default=None, metavar="country")

    parser.add_argument(
        "-oo", "--only-country", help="only specific country", 
        action="append", dest="only_countries",
        default=None, metavar="country")

    parser.add_argument(
        "-O", "--exclude-country", help="exclude specific country", 
        action="append", dest="ex_countries",
        default=None, metavar="country")
    
    parser.add_argument(
        "-q", "--quiet", help="quiet mode, print only results", 
        action="store_const", dest="quiet", 
        default=False, const=True)
    
    parser.add_argument(
        "-r", "--country", help="group results by country", 
        action="store_const", dest="country", 
        default=False, const=True)

    parser.add_argument(
        "-t", "--threads", 
        help="the number of concurrent ping threads", 
        action="store", dest="threads", 
        default=25, type=int)

    parser.add_argument(
        "-w", "--include-network", help="include specific network", 
        action="append", dest="in_networks",
        default=None, metavar="network")
    
    parser.add_argument(
        "-ww", "--only-network", help="only specific network", 
        action="append", dest="only_networks",
        default=None, metavar="network")

    parser.add_argument(
        "-W", "--exclude-network", help="exclude specific network", 
        action="append", dest="ex_networks",
        default=None, metavar="network")
    

    parser.add_argument("destination", help="target of the ping command")

    ns = parser.parse_args()

    in_nodes = split_args(ns.in_nodes)
    ex_nodes = split_args(ns.ex_nodes)
    in_countries = split_args(ns.in_countries)
    ex_countries = split_args(ns.ex_countries)
    only_countries = split_args(ns.only_countries)
    in_networks = split_args(ns.in_networks)
    ex_networks = split_args(ns.ex_networks)
    only_networks = split_args(ns.only_networks)

    nodes = ring.pick_nodes(count=ns.count, 
        inc_hosts=in_nodes, ex_hosts=ex_nodes,
        inc_countries=in_countries, ex_countries=ex_countries, 
        only_countries=only_countries, inc_networks=in_networks, 
        ex_networks=ex_networks, only_networks=only_networks)

    if not ns.quiet:
        print "ring-ping v%s written by Teun Vink <*****@*****.**>\n" % VERSION
        print "pinging %s from %d nodes:" % (ns.destination, len(nodes))

    cmd_result = ring.run_command('ping%s -c%s -q %s' % ("6" if ns.ipv6 else "", ns.pingcount, ns.destination), 
        nodes, max_threads=ns.threads, analyse=analyzer)
    ok = cmd_result.get_successful_results()
    fail = cmd_result.get_failed_results(include_ssh_problems=False)
    conn = cmd_result.get_failed_results(only_ssh_problems=True)

    sort = ok.get_value_sorted("avg")
    cbn = ring.get_countries_by_node()

    if ns.country:
        countries = ring.get_ring_countries()
        countries.sort()
        nbc = ring.get_countries_by_node()
        res = {}
        for (host, val) in sort:
            if res.has_key(nbc[host]):
                res[nbc[host]].append(val)
            else:
                res[nbc[host]] = [val]
        avg = {}
        for country in countries:
            if res.has_key(country):
                avg[country] = sum(res[country])/len(res[country])
        sort = sorted(avg.iteritems(), key=itemgetter(1))
        print "Average ping time per country:"
        for (c, a) in sort:
            print "{0:3s}: {1:6.2f}ms".format(c, a)
    elif not ns.quiet:
        for (host, val) in sort:
            hostname = "%s (%s):" % (host, cbn[host].upper())
            v = "%.2fms" % val
            print "%-28s %8s   " % (hostname, v)

        if len(conn.get_results()) > 0 and ns.errors:
            print "\nconnection failures:"
            for r in conn.get_results():
                hostname = "%s (%s):" % (r.get_hostname(), cbn[r.get_hostname()].upper())
                print "%-28s %s" % (hostname, r.get_ssh_errormsg())

        if len(fail.get_results()) > 0 and ns.errors:
            print "\ncommand execution problems:"
            for r in fail.get_results():
                hostname = "%s (%s):" % (r.get_hostname(), cbn[r.get_hostname()].upper())
                print "%-28s exitcode = %s" % (hostname, r.get_exitcode())
                if r.get_stderr():
                    print ", message: %s" % r.get_stderr()[0]

        print

    print "%d nodes ok (%.2fms avg), %d nodes failed to ping, failed to connect to %d nodes." % (
        len(ok.get_results()),
        ok.get_value_avg("avg"),
        len(fail.get_results()),
        len(conn.get_results()))
Exemple #4
0
# examples/04-pick.py
#
# show/test some uses of the pick_nodes() 

import sys

try:
    from ringtools import ring
except ImportError:
    # ringtools probaly isn't installed yet
    sys.path.append('..')
    from ringtools import ring


# let's pick nodes
print "max 5 nodes: %s " % ring.pick_nodes(5) 
print "max 5 nodes, including bit01: %s" % ring.pick_nodes(5, inc_hosts="bit01") 
print "max 5 nodes, including one from amazon: %s" % ring.pick_nodes(5, inc_networks="amazon") 
print "max 5 nodes, including one in France: %s" % ring.pick_nodes(5, inc_countries="fr") 
print "max 5 nodes, only from atrato network: %s" % ring.pick_nodes(5, only_networks="atrato")
print "max 5 nodes, only from .nl: %s" % ring.pick_nodes(5, only_countries="nl")
print "max 5 nodes, all ipv6 only: %s " % ring.pick_nodes(5, support_ipv6_only=True) 
print "max 5 nodes, don't care about IPv6: %s " % ring.pick_nodes(5, support_ipv6_only=False) 
print "max 5 nodes, all dual stack: %s " % ring.pick_nodes(5, support_ipv4=True)
print "max 5 nodes, no IPv4 support: %s " % ring.pick_nodes(5, support_ipv4=False)

# this one should never give any results
print "max 5 nodes, ipv6 only + dual stack: %s " % ring.pick_nodes(5, support_ipv4=True, support_ipv6_only=True) 

# and we can combine these
print "max 5 nodes, ipv6 only and not in .nl: %s" % ring.pick_nodes(5, support_ipv6_only=True, ex_countries='nl')
Exemple #5
0
def main():
    parser = argparse.ArgumentParser(
        description="Run curl requests on the NLNOG ring.",
        epilog="Visit https://ring.nlnog.net for more information",
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument(
        "-6", "--ipv6", help="enforce IPv6",
        action="store_const", dest="ipv6",
        const=True, default=False)

    parser.add_argument(
        "-c", "--count",
        help="the number of nodes to do requests from",
        action="store", dest="count",
        default=10, type=int)
    
    parser.add_argument(
        "-e", "--errors",
        help="detailed error reporting",
        action="store_const", dest="errors",
        default=False, const=True)

    parser.add_argument(
        "-n", "--include-node", help="include specific node",
        action="append", dest="in_nodes",
        default=None, metavar="node")

    parser.add_argument(
        "-N", "--exclude-node", help="exclude specific node",
        action="append", dest="ex_nodes",
        default=None, metavar="node")

    parser.add_argument(
        "-o", "--include-country", help="include specific country",
        action="append", dest="in_countries",
        default=None, metavar="country")

    parser.add_argument(
        "-oo", "--only-country", help="only specific country",
        action="append", dest="only_countries",
        default=None, metavar="country")

    parser.add_argument(
        "-O", "--exclude-country", help="exclude specific country",
        action="append", dest="ex_countries",
        default=None, metavar="country")

    parser.add_argument(
        "-q", "--quiet", help="quiet mode, print only results",
        action="store_const", dest="quiet",
        default=False, const=True)

    parser.add_argument(
        "-t", "--threads",
        help="the number of concurrent ping threads",
        action="store", dest="threads",
        default=25, type=int)
    
    parser.add_argument(
        "-w", "--include-network", help="include specific network",
        action="append", dest="in_networks",
        default=None, metavar="network")

    parser.add_argument(
        "-ww", "--only-network", help="only specific network",
        action="append", dest="only_networks",
        default=None, metavar="network")

    parser.add_argument(
        "-W", "--exclude-network", help="exclude specific network",
        action="append", dest="ex_networks",
        default=None, metavar="network")

    parser.add_argument("destination", help="target URL")

    ns = parser.parse_args()

    in_nodes = split_args(ns.in_nodes)
    ex_nodes = split_args(ns.ex_nodes)
    in_countries = split_args(ns.in_countries)
    ex_countries = split_args(ns.ex_countries)
    only_countries = split_args(ns.only_countries)
    in_networks = split_args(ns.in_networks)
    ex_networks = split_args(ns.ex_networks)
    only_networks = split_args(ns.only_networks)

    nodes = ring.pick_nodes(count=ns.count,
        inc_hosts=in_nodes, ex_hosts=ex_nodes,
        inc_countries=in_countries, ex_countries=ex_countries,
        only_countries=only_countries, inc_networks=in_networks,
        ex_networks=ex_networks, only_networks=only_networks, 
        active_only=True)

    if not ns.quiet:
        print "ring-curl v%s written by Teun Vink <*****@*****.**>\n" % VERSION
        print "opening URL '%s' from %d nodes:" % (ns.destination, len(nodes))

    cmd = 'curl --connect-timeout 15 -L -i -A "%s" -s %s' % (AGENT, ns.destination)
    cmd_result = ring.run_command(cmd, nodes, analyse=analyzer)

    s_res = cmd_result.get_successful_results()
    s_res_t = s_res.get_value_sorted('runtime')
    s_res_h = s_res.get_value('HTTP-code')
    fail = cmd_result.get_failed_results(include_ssh_problems=False)
    conn = cmd_result.get_failed_results(only_ssh_problems=True)

    cbn = ring.get_countries_by_node()

    if not ns.quiet:
        for (host, val) in s_res_t:
            hostname = "%s (%s):" % (host, cbn[host].upper())
            v = "%.2fs" % val
            print "%-28s %8s  %s" % (hostname, v, s_res_h[host])

        if len(conn.get_results()) > 0 and ns.errors:
            print "\nconnection failures:"
            for r in conn.get_results():
                hostname = "%s (%s):" % (r.get_hostname(), cbn[r.get_hostname()].upper())
                print "%-28s %s" % (hostname, r.get_ssh_errormsg())

        if len(fail.get_results()) > 0 and ns.errors:
            print "\ncommand execution problems:"
            for r in fail.get_results():
                hostname = "%s (%s):" % (r.get_hostname(), cbn[r.get_hostname()].upper())
                print "%-28s exitcode %s: %s" % (hostname, r.get_exitcode(), CURL_ERRORS.get(r.get_exitcode(), "unknown"))

        print

    print "%d nodes succeeded, %d nodes failed to retrieve the URL, failed to connect to %d nodes." % (
        len(s_res.get_results()),
        len(fail.get_results()),
        len(conn.get_results()))