Search RDNS file and insert relevant records into the database.
(logger, rdns_file, mongo_connection, dns_manager, ip_manager, zones)
| 176 | |
| 177 | |
| 178 | def update_rdns(logger, rdns_file, mongo_connection, dns_manager, ip_manager, zones): |
| 179 | """ |
| 180 | Search RDNS file and insert relevant records into the database. |
| 181 | """ |
| 182 | g_dns = GoogleDNS.GoogleDNS() |
| 183 | rdns_collection = mongo_connection.get_sonar_reverse_dns_connection() |
| 184 | |
| 185 | with open(rdns_file, "r") as rdns_f: |
| 186 | for line in rdns_f: |
| 187 | try: |
| 188 | data = json.loads(line) |
| 189 | except ValueError: |
| 190 | continue |
| 191 | except Exception as e: |
| 192 | logger.error("Error parsing file...") |
| 193 | logger.error("Exception: " + str(e)) |
| 194 | raise |
| 195 | |
| 196 | if "type" in data and data["type"] != "ptr": |
| 197 | continue |
| 198 | |
| 199 | try: |
| 200 | ip_addr = data["name"] |
| 201 | except: |
| 202 | ip_addr = None |
| 203 | |
| 204 | try: |
| 205 | domain = data["value"] |
| 206 | except KeyError: |
| 207 | domain = None |
| 208 | |
| 209 | timestamp = data["timestamp"] |
| 210 | |
| 211 | if domain != None and ip_addr != None and ip_manager.is_tracked_ip(ip_addr): |
| 212 | logger.debug("Matched RDNS " + ip_addr) |
| 213 | zone = find_zone(domain, zones) |
| 214 | result = mongo_connection.perform_count( |
| 215 | rdns_collection, {"ip": ip_addr} |
| 216 | ) |
| 217 | |
| 218 | if result == 0: |
| 219 | insert_json = {} |
| 220 | insert_json["ip"] = ip_addr |
| 221 | insert_json["zone"] = zone |
| 222 | insert_json["fqdn"] = domain |
| 223 | insert_json["status"] = "unknown" |
| 224 | insert_json["sonar_timestamp"] = int(timestamp) |
| 225 | insert_json["created"] = datetime.now() |
| 226 | insert_json["updated"] = datetime.now() |
| 227 | mongo_connection.perform_insert(rdns_collection, insert_json) |
| 228 | else: |
| 229 | rdns_collection.update_one( |
| 230 | {"ip": ip_addr}, |
| 231 | {"$set": {"fqdn": domain}, "$currentDate": {"updated": True}}, |
| 232 | ) |
| 233 | |
| 234 | check_for_ptr_record(ip_addr, dns_manager, g_dns, zones) |
| 235 |
no test coverage detected