Skip to content

Instantly share code, notes, and snippets.

@hsuanxyz
Created September 18, 2024 08:14
Show Gist options
  • Save hsuanxyz/154fe228d9b9c9e40c78cf28868b4779 to your computer and use it in GitHub Desktop.
Save hsuanxyz/154fe228d9b9c9e40c78cf28868b4779 to your computer and use it in GitHub Desktop.
aws ip speedtest
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# EC2 Reachability and Latency Monitor\n",
"\n",
"This notebook fetches EC2 reachability data, allows you to select a region, and then monitors the latency for that region's IP addresses."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install required libraries\n",
"!pip install requests beautifulsoup4 ping3 tabulate ipywidgets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from collections import defaultdict\n",
"import time\n",
"from ping3 import ping\n",
"from tabulate import tabulate\n",
"import ipywidgets as widgets\n",
"from IPython.display import display, clear_output\n",
"\n",
"def fetch_ec2_reachability():\n",
" url = \"http://ec2-reachability.amazonaws.com/\"\n",
" response = requests.get(url)\n",
" if response.status_code == 200:\n",
" return response.text\n",
" else:\n",
" raise Exception(f\"Failed to fetch data: HTTP {response.status_code}\")\n",
"\n",
"def parse_html_and_group_ips(html_content):\n",
" soup = BeautifulSoup(html_content, 'html.parser')\n",
" tables = soup.find_all('table')\n",
" \n",
" if not tables:\n",
" raise Exception(\"Could not find any tables in the HTML content\")\n",
"\n",
" ip_groups = defaultdict(list)\n",
" \n",
" for table in tables:\n",
" for row in table.find_all('tr')[1:]:\n",
" columns = row.find_all('td')\n",
" if len(columns) >= 3:\n",
" region = columns[0].text.strip()\n",
" ip_prefix = columns[1].text.strip()\n",
" ip = columns[2].text.strip()\n",
" ip_groups[region].append((ip_prefix, ip))\n",
" \n",
" return dict(ip_groups)\n",
"\n",
"def get_latency(ip):\n",
" try:\n",
" return round(ping(ip, timeout=2) * 1000, 2)\n",
" except Exception:\n",
" return None\n",
"\n",
"print(\"Fetching EC2 reachability data...\")\n",
"html_content = fetch_ec2_reachability()\n",
"grouped_ips = parse_html_and_group_ips(html_content)\n",
"print(\"Data fetched successfully!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a dropdown widget for region selection\n",
"region_dropdown = widgets.Dropdown(\n",
" options=list(grouped_ips.keys()),\n",
" description='Region:',\n",
" disabled=False,\n",
")\n",
"\n",
"display(region_dropdown)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def monitor_latency(region):\n",
" ip_data = grouped_ips[region]\n",
" \n",
" while True:\n",
" clear_output(wait=True)\n",
" print(f\"Latency for region: {region}\")\n",
" print(\"Press the stop button to exit\")\n",
" \n",
" table_data = []\n",
" for ip_prefix, ip in ip_data:\n",
" latency = get_latency(ip)\n",
" status = \"OK\" if latency is not None else \"Timeout\"\n",
" latency_str = f\"{latency} ms\" if latency is not None else \"N/A\"\n",
" table_data.append([ip_prefix, ip, latency_str, status])\n",
"\n",
" headers = [\"IP Prefix\", \"IP\", \"Latency\", \"Status\"]\n",
" print(tabulate(table_data, headers=headers, tablefmt=\"grid\"))\n",
" \n",
" time.sleep(5) # Update every 5 seconds\n",
"\n",
"# Create a button to start monitoring\n",
"start_button = widgets.Button(description=\"Start Monitoring\")\n",
"output = widgets.Output()\n",
"\n",
"def on_button_clicked(b):\n",
" with output:\n",
" monitor_latency(region_dropdown.value)\n",
"\n",
"start_button.on_click(on_button_clicked)\n",
"\n",
"display(start_button, output)"
]
}
],
"metadata": {
"kernelspec": {\n",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment