def check_and_expire( ip, ipm_list ):
   ''' given an IP address and a list of IPMeta objects for that IP, expire all info in db that is not up to date '''
   print "%s %s" %  (ip, len( ipm_list ) )
   if len( ipm_list ) > 1:
      ### pick one, rest should have been invalidated anyways
      for ipm in ipm_list[1:]:
         ipm.invalidated=datetime.now(pytz.utc)
         ipm.last_updated=datetime.now(pytz.utc)
         ipm.save()
   ipm = ipm_list[0] # this is the chosen ipmeta object to validate against
   now_dnshost = None
   now_dnsloc = None
   now_dnshost = do_dns_host_lookup( ip )
   if now_dnshost != None:
      now_dnsloc = do_dns_loc_lookup( now_dnshost )
   if ipm.hostname != now_dnshost or ipm.dnsloc != now_dnsloc:
      # invalidate old and create new
      ipm.invalidated = datetime.now(pytz.utc)
      ipm.last_updated=datetime.now(pytz.utc)
      ipm.save()
      # clone didn't work ( https://docs.djangoproject.com/en/1.7/topics/db/queries/#copying-model-instances )
      # so doing a new object for now
      print "creating new! for %s/%s" % ( ipm.ip , ipm.hostname )
      ipm_new = IPMeta(
         ip= ipm.ip
      )
      ipm_new.save() ## this will do another DNS lookup (which should be in cache anyways)
   else: #save the last time this info was checked
      ipm.last_updated=datetime.now(pytz.utc)
      ipm.save()
def check_and_expire(ip, ipm_list):
    ''' given an IP address and a list of IPMeta objects for that IP, expire all info in db that is not up to date '''
    print "%s %s" % (ip, len(ipm_list))
    if len(ipm_list) > 1:
        ### pick one, rest should have been invalidated anyways
        for ipm in ipm_list[1:]:
            ipm.invalidated = datetime.now(pytz.utc)
            ipm.last_updated = datetime.now(pytz.utc)
            ipm.save()
    ipm = ipm_list[0]  # this is the chosen ipmeta object to validate against
    now_dnshost = None
    now_dnsloc = None
    now_dnshost = do_dns_host_lookup(ip)
    if now_dnshost != None:
        now_dnsloc = do_dns_loc_lookup(now_dnshost)
    if ipm.hostname != now_dnshost or ipm.dnsloc != now_dnsloc:
        # invalidate old and create new
        ipm.invalidated = datetime.now(pytz.utc)
        ipm.last_updated = datetime.now(pytz.utc)
        ipm.save()
        # clone didn't work ( https://docs.djangoproject.com/en/1.7/topics/db/queries/#copying-model-instances )
        # so doing a new object for now
        print "creating new! for %s/%s" % (ipm.ip, ipm.hostname)
        ipm_new = IPMeta(ip=ipm.ip)
        ipm_new.save(
        )  ## this will do another DNS lookup (which should be in cache anyways)
    else:  #save the last time this info was checked
        ipm.last_updated = datetime.now(pytz.utc)
        ipm.save()
示例#3
0
def create_ipmeta( ip_addr ): ## not used?
    dnshost = do_dns_host_lookup( ip_addr )
    if dnshost:
        dnsloc = do_dns_loc_lookup( dnshost )
    ipm=IPMeta( ##HERE
        ip=ip_addr,
        hostname=dnshost,
        dnsloc=dnsloc
    )
    ipm.save()