Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adding ripe for asn name #40

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/generate_reports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: Generate IXPs reports
on: # yamllint disable-line rule:truthy
schedule:
- cron: "0 3 * * *"
- cron: "30 19 * * *"
permissions:
contents: write
jobs:
Expand Down
52 changes: 42 additions & 10 deletions pgossip/pgossip.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ async def get_asn_details(self, asn, pfxs):
"""
if asn != 64567: # AMS-IX using private ASN
details = await self.caida_asn_whois(asn)
asn_name = await self.ripe_asn_name(asn)
await asyncio.sleep(0.5) # Non-blocking sleep
else:
details = {
Expand All @@ -170,19 +171,18 @@ async def get_asn_details(self, asn, pfxs):
"country": {"iso": "NL"},
}

if details:
if not details.get("asnName"):
details["asnName"] = "NA"
else:
if not asn_name:
asn_name = "NA"

if not details:
details = {
"asnName": "NA",
"rank": "NA",
"source": "NA",
"country": {"iso": "NA"},
}

return (
f"{pfxs} | {asn} | {details['asnName']} | {details['rank']} | {details['source']} | {details['country']['iso']} "
f"{pfxs} | {asn} | {asn_name} | {details['rank']} | {details['source']} | {details['country']['iso']} "
f"| https://www.peeringdb.com/asn/{asn}"
)

Expand Down Expand Up @@ -270,7 +270,7 @@ async def alice_neighbours(self, url, route_server):
response = session.get(url)
if response.status_code == 200:
neighbour_dict = {}
data = json.loads(response.text)
data = response.json()
if "neighbors" in data:
neigh = "neighbors"
else:
Expand Down Expand Up @@ -306,7 +306,7 @@ def bv_asn_whois(self, asn):
with requests.Session() as session:
response = session.get(url)
if response.status_code == 200:
data = json.loads(response.text)
data = response.json()
try:
result = data["data"]
except KeyError:
Expand Down Expand Up @@ -339,7 +339,7 @@ async def caida_asn_whois(self, asn):
with requests.Session() as session:
response = session.get(url)
if response.status_code == 200:
data = json.loads(response.text)
data = response.json()
try:
result = data["data"]["asn"]
except KeyError:
Expand All @@ -348,7 +348,39 @@ async def caida_asn_whois(self, asn):
else:
print(
"ERROR | HTTP status != 200 - caida_asn_whois"
f" - Error {response.status_code}: {asn} => {response.reason}"
f" - Error {response.status_code}: {asn}"
)
sys.exit(1)
return result

async def ripe_asn_name(self, asn):
"""
Fetches the holder name of the specified ASN from RIPE's API.

Args:
asn (str): The ASN number as a string.

Returns:
str or None: The holder name of the ASN if available, otherwise None.

Raises:
KeyError: If the ASN data is missing in the API response.
SystemExit: If the API response status is not 200.
"""
url = f"https://stat.ripe.net/data/as-overview/data.json?resource={asn}"
result = None
with requests.Session() as session:
response = session.get(url)
if response.status_code == 200:
try:
result = response.json()["data"]["holder"]
except KeyError:
print(f"ASN {asn} has no data at whois!")
raise
else:
print(
"ERROR | HTTP status != 200 - ripe_asn_name"
f" - Error {response.status_code}: {asn}"
)
sys.exit(1)
return result
Expand Down
Loading