def purge_unknown_traceroutes (conn, unknown_traceroutes_db): lock=MutexFile(LOCK_FILE) if not lock.acquire (): return False if not conn: conn = ixmaps.DBConnect.getConnection() unknown_traceroutes = unknown_traceroutes_db.dictresult() if len(unknown_traceroutes) == 0: return ids_to_purge = "" was_previous_traceroute = False for traceroute in unknown_traceroutes: if was_previous_traceroute: ids_to_purge += ", " else: was_previous_traceroute = True ids_to_purge += str(traceroute['id']) qstr = "delete from traceroute_traits where id in (%s)" % (ids_to_purge) # print qstr conn.query (qstr) qstr = "delete from traceroute_countries where traceroute_id in (%s)" \ % (ids_to_purge) # print qstr conn.query (qstr) return True
owners = create_from_file({}, CHOwner.factory, "owners.txt") print owners if show_chotels: chotels = create_from_file({}, CHotel.factory, "chotels.txt") print chotels for tr_elem in tr: tr_elem.lookup_geoloc(ml) #print al.query_dquad(tr_elem.dquad) tr_elem.lookup_asnum(al) if show_chotels: tr_elem.lookup_chotel(chotels) print tr elif do_update_ip_addr_info: lock=MutexFile(LOCK_FILE) if lock.acquire(): conn = DBConnect.getConnection() addrs = get_unknown_ip_addrs(conn) #print addrs tr = [TRNode(dq_to_num(t), 0.0) for t in addrs] for tr_elem in tr: tr_elem.lookup_geoloc(ml) tr_elem.lookup_asnum(al) tr_elem.set_fqdn() tr_elem.update_db(conn) #print tr lock.release() else: print "cannot acquire lock file" sys.exit(0)
def add_traceroutes (conn, new_traceroutes_db, be_verbose=False): lock=MutexFile(LOCK_FILE) if not lock.acquire (): return False if not conn: conn = ixmaps.DBConnect.getConnection() new_traceroutes = new_traceroutes_db.dictresult() insert_into_traits = "" insert_into_countries = "" for traceroute in new_traceroutes: traceroute_id = traceroute['id'] if be_verbose: print traceroute_id qres = conn.query ("select * from tr_item where traceroute_id=%i" % traceroute_id) tr_body = qres.dictresult() if not tr_body: continue (nhops, nattempts) = get_tr_items_dim (tr_body) ipaddrs = [None] * nhops # ip_info = [None] * nhops # print "tr_body:" , tr_body for probe in tr_body: # print " probe['hop']:", probe['hop'] hop = probe['hop']-1 # --- There are multiple attempts for each hop, but we # only need one, so, after you aquire an IP, ignore # further attempts --- if (ipaddrs[hop]): continue ipaddrs[hop] = probe['ip_addr'] # for hop in range (nhops): # ip_info[hop] = ixmaps.get_ip_addr_info (ipaddrs[hop], conn) ip_info = get_ip_addr_info_list (ipaddrs, conn) number_of_ips = len(ip_info) is_nsa = False # print "ip_info:", ip_info # print "len(ip_info):", len (ip_info) # print "len(ipaddrs):", len(ipaddrs) # print "ipaddrs:", ipaddrs # print "number_of_ips:", number_of_ips # print "for hop in range (number_of_ips):" for hop in range (number_of_ips): # print ' ipaddrs[' + str(hop) + ']:', ipaddrs[hop] pr_addr = ipaddrs[hop] # print (ip_info[hop]) is_nsa = ixmaps.is_nsa(ip_info[hop]) # print " is_nsa:", is_nsa if is_nsa == True: break is_chotel = False # print "for hop in range (number_of_ips):" for hop in range (number_of_ips): pr_addr = ipaddrs[hop] # print " ip_info[" + str(hop) + "]: ", ip_info[hop] is_chotel = ixmaps.is_chotel(ip_info[hop]) if is_chotel == True: break canada_flag = False us_flag = False for hop in range (number_of_ips): pr_addr = ipaddrs[hop] country = ixmaps.get_country (ip_info[hop]) if country == "CA": canada_flag = True if country == "US": us_flag = True if (canada_flag == True) and (us_flag == True): break # --- Remove, so as not to get an SQL error, just in case some # traits-data for this traceroute already exists --- conn.query ("delete from traceroute_traits where id = %i" % traceroute_id) conn.query ("""insert into traceroute_traits (id, nsa, hotel) values (%i, %s, %s)""" % (traceroute_id, is_nsa, is_chotel)) # --- Remove, so as not to get an SQL error, just in case some # country-data for this traceroute already exists --- if canada_flag or us_flag: conn.query("delete from traceroute_countries where traceroute_id = %s" \ % (traceroute_id) ) if canada_flag: conn.query ("""insert into traceroute_countries (country_code, traceroute_id) values ('CA', %i)""" % traceroute_id) if us_flag: conn.query ("""insert into traceroute_countries (country_code, traceroute_id) values ('US', %i)""" % traceroute_id) return True
def build_tables(conn): lock=MutexFile(LOCK_FILE) if not lock.acquire (): return False if not conn: conn = ixmaps.DBConnect.getConnection() # --- Create traceroute_traits table --- conn.query (""" -- -- Name: traceroute_traits; Type: TABLE; Schema: public; Owner: ixmaps; Tablespace: -- CREATE TABLE traceroute_traits ( id integer PRIMARY KEY, nsa boolean, hotel boolean ); ALTER TABLE public.traceroute_traits OWNER TO ixmaps; -- -- Name: traceroute_traits; Type: ACL; Schema: public; Owner: ixmaps -- REVOKE ALL ON TABLE traceroute_traits FROM PUBLIC; REVOKE ALL ON TABLE traceroute_traits FROM ixmaps; GRANT ALL ON TABLE traceroute_traits TO ixmaps; GRANT SELECT ON TABLE traceroute_traits TO PUBLIC; """) # --- Create traceroute_countries table --- conn.query (""" -- -- Name: traceroute_countries; Type: TABLE; Schema: public; Owner: ixmaps; Tablespace: -- CREATE TABLE traceroute_countries ( country_code character(2) NOT NULL, traceroute_id integer NOT NULL, PRIMARY KEY (country_code, traceroute_id) ); ALTER TABLE public.traceroute_countries OWNER TO ixmaps; -- -- Name: traceroute_countries; Type: ACL; Schema: public; Owner: ixmaps -- REVOKE ALL ON TABLE traceroute_countries FROM PUBLIC; REVOKE ALL ON TABLE traceroute_countries FROM ixmaps; GRANT ALL ON TABLE traceroute_countries TO ixmaps; GRANT SELECT ON TABLE traceroute_countries TO PUBLIC; """) # # --- Create countries table --- # conn.query = (""" # -- # -- Name: country; Type: TABLE; Schema: public; Owner: ixmaps; Tablespace: # -- # CREATE TABLE country ( # code character(2) NOT NULL, # name character varying(27) # ); # ALTER TABLE public.country OWNER TO ixmaps; # -- # -- Data for Name: country; Type: TABLE DATA; Schema: public; Owner: ixmaps # -- # COPY country (code, name) FROM stdin; # CA Canada # US United States # xx Unknown # \. # """) return True