コード例 #1
0
ファイル: CollectLib.py プロジェクト: periscope-ps/periscope
def get_host_ips(host, cache=True):
    """Get a list of IP addresses for a given host.
    """
    ips = DNSCache.objects.filter(hostname=host)
    if len(ips) > 0:
        ret = []
        for i in ips:
            ret.append(i.ip)
        return ret
    
    try:
        info = socket.getaddrinfo(host, None)
    except socket.gaierror:
        logger.warning("Hostname %s doesn't have an IP address" % host)
        info = []
    
    ips = []
    for i in info:
        if i[4][0] not in ips:
            # Ignore IPv6
            if len(i[4][0]) > 16:
                continue
            ips.append(i[4][0])
            d = DNSCache(hostname=host, ip=i[4][0])
            d.save()
    
    return ips
コード例 #2
0
ファイル: CollectLib.py プロジェクト: periscope-ps/periscope
def get_host_ips(host, cache=True):
    """Get a list of IP addresses for a given host.
    """
    ips = DNSCache.objects.filter(hostname=host)
    if len(ips) > 0:
        ret = []
        for i in ips:
            ret.append(i.ip)
        return ret

    try:
        info = socket.getaddrinfo(host, None)
    except socket.gaierror:
        logger.warning("Hostname %s doesn't have an IP address" % host)
        info = []

    ips = []
    for i in info:
        if i[4][0] not in ips:
            # Ignore IPv6
            if len(i[4][0]) > 16:
                continue
            ips.append(i[4][0])
            d = DNSCache(hostname=host, ip=i[4][0])
            d.save()

    return ips
コード例 #3
0
ファイル: helpers.py プロジェクト: periscope-ps/periscope
def forward_dns(host, cache=True):
    """
    Returns list of IP addresses for a given host.
    
    see: L{reverse_dns} for caching information
    """
    ips = DNSCache.objects.filter(hostname=host)
    if len(ips) > 0:
        ret = []
        for i in ips:
            ret.append(i.ip)
        return ret
    
    try:
        info = socket.getaddrinfo(host, None)
    except socket.gaierror:
        logger.warning("Hostname %s doesn't have an IP address" % host)
        info = []
    
    ips = []
    for i in info:
        if i[4][0] not in ips:
            ips.append(i[4][0])
            if cache:
                dns_cache = DNSCache(hostname=host, ip=i[4][0])
                dns_cache.save()
    transaction.commit()
    return ips
コード例 #4
0
def forward_dns(host, cache=True):
    """
    Returns list of IP addresses for a given host.
    
    see: L{reverse_dns} for caching information
    """
    ips = DNSCache.objects.filter(hostname=host)
    if len(ips) > 0:
        ret = []
        for i in ips:
            ret.append(i.ip)
        return ret

    try:
        info = socket.getaddrinfo(host, None)
    except socket.gaierror:
        logger.warning("Hostname %s doesn't have an IP address" % host)
        info = []

    ips = []
    for i in info:
        if i[4][0] not in ips:
            ips.append(i[4][0])
            if cache:
                dns_cache = DNSCache(hostname=host, ip=i[4][0])
                dns_cache.save()
    transaction.commit()
    return ips
コード例 #5
0
ファイル: CollectLib.py プロジェクト: periscope-ps/periscope
def get_host_by_addr(ip, cache=False):
    """Reverse DNS lookup for addresses without hostnames
    """
    cache = DNSCache.objects.filter(ip=ip)
    if len(cache) > 0:
        return cache.values_list('hostname')[0][0]

    try:
        host = socket.gethostbyaddr(ip)[0]
    except socket.herror:
        host = None
    logger.info("Reverse DNS lookup for %s returned '%s'" % (ip, host))

    d = DNSCache(hostname=host, ip=ip)
    d.save()

    return host
コード例 #6
0
ファイル: CollectLib.py プロジェクト: periscope-ps/periscope
def get_host_by_addr(ip, cache=False):
    """Reverse DNS lookup for addresses without hostnames
    """
    cache = DNSCache.objects.filter(ip=ip)
    if len(cache) > 0:
        return cache.values_list('hostname')[0][0]
        
    try:
        host = socket.gethostbyaddr(ip)[0]
    except socket.herror:
        host = None
    logger.info("Reverse DNS lookup for %s returned '%s'" % (ip, host))
    
    d = DNSCache(hostname=host, ip=ip)
    d.save()
        
    return host
コード例 #7
0
def topology_get_user_transfers(request):
    from cStringIO import StringIO

    user = request.GET['user']

    #print >> sys.stderr, user

    json_xfers = StringIO()
    #json_xfers.write('[');

    user_xfers = GridFTPTransfer.objects.filter(user=user)

    json_xfers.write('\n{"xfers": [')
    
    for xfer in user_xfers:
        # need to do some lookups/matching in here
        json_xfers.write('\n {"t_id":"%s",\n "status":"%s",\n "src":"%s",\n "dst":"%s",\n "sport":"%s",\n'
                       ' "dport":"%s",\n "misc":"%s",\n },' % \
                       (xfer.transfer_id, xfer.status, xfer.src, xfer.dst, xfer.src_port, \
                        xfer.dst_port, xfer.misc))
        
    json_xfers.write('],\npaths: [')

    for xfer in user_xfers:        
        if validIP4(xfer.src):
            src = xfer.src
        else:
            try:
                src = DNSCache.objects.get(hostname=xfer.src).ip
            except:
                src = get_host_ips(xfer.src)
                if (len(src) == 0):
                    continue
                else:
                    src = src[0]
                    d = DNSCache(hostname=xfer.src, ip=src)
                    d.save()

        if validIP4(xfer.dst):
            dst = xfer.dst
        else:
            try:
                dst = DNSCache.objects.get(hostname=xfer.dst).ip
            except:
                dst = get_host_ips(xfer.dst)
                if (len(dst) == 0):
                    continue
                else:
                    dst = dst[0]
                    d = DNSCache(hostname=xfer.dst, ip=dst)
                    d.save()

        src_node = find_cloud(src)
        dst_node = find_cloud(dst)    

        if (src_node is None or dst_node is None):
            continue
        
        path = find_path(src_node, dst_node)

        if (path):
            json_xfers.write('\n {"t_id": "%s", "src_id": %s, "dst_id": %s, "link_ids": [' %
                             (xfer.transfer_id, src_node.id, dst_node.id))
            
            hops = path.hops.all()
            for h in hops:
                # we just assume each hop is a link right now
                link = h.target.toRealType()
                json_xfers.write('%s,' % (link.id))

            json_xfers.write(']},')
            
    json_xfers.write(']\n}')
    
    return HttpResponse(json_xfers.getvalue(), mimetype="application/json")
コード例 #8
0
ファイル: views.py プロジェクト: periscope-ps/periscope
def topology_get_user_transfers(request):
    from cStringIO import StringIO

    user = request.GET['user']

    #print >> sys.stderr, user

    json_xfers = StringIO()
    #json_xfers.write('[');

    user_xfers = GridFTPTransfer.objects.filter(user=user)

    json_xfers.write('\n{"xfers": [')

    for xfer in user_xfers:
        # need to do some lookups/matching in here
        json_xfers.write('\n {"t_id":"%s",\n "status":"%s",\n "src":"%s",\n "dst":"%s",\n "sport":"%s",\n'
                       ' "dport":"%s",\n "misc":"%s",\n },' % \
                       (xfer.transfer_id, xfer.status, xfer.src, xfer.dst, xfer.src_port, \
                        xfer.dst_port, xfer.misc))

    json_xfers.write('],\npaths: [')

    for xfer in user_xfers:
        if validIP4(xfer.src):
            src = xfer.src
        else:
            try:
                src = DNSCache.objects.get(hostname=xfer.src).ip
            except:
                src = get_host_ips(xfer.src)
                if (len(src) == 0):
                    continue
                else:
                    src = src[0]
                    d = DNSCache(hostname=xfer.src, ip=src)
                    d.save()

        if validIP4(xfer.dst):
            dst = xfer.dst
        else:
            try:
                dst = DNSCache.objects.get(hostname=xfer.dst).ip
            except:
                dst = get_host_ips(xfer.dst)
                if (len(dst) == 0):
                    continue
                else:
                    dst = dst[0]
                    d = DNSCache(hostname=xfer.dst, ip=dst)
                    d.save()

        src_node = find_cloud(src)
        dst_node = find_cloud(dst)

        if (src_node is None or dst_node is None):
            continue

        path = find_path(src_node, dst_node)

        if (path):
            json_xfers.write(
                '\n {"t_id": "%s", "src_id": %s, "dst_id": %s, "link_ids": [' %
                (xfer.transfer_id, src_node.id, dst_node.id))

            hops = path.hops.all()
            for h in hops:
                # we just assume each hop is a link right now
                link = h.target.toRealType()
                json_xfers.write('%s,' % (link.id))

            json_xfers.write(']},')

    json_xfers.write(']\n}')

    return HttpResponse(json_xfers.getvalue(), mimetype="application/json")