Multi-Cloud Network Performance Monitoring Script 📊
23/01/2025 security 4 mins read
Prerequisites
- Linux/Unix environment with bash shell
- SSH access to your cloud instances
- iperf3 installed locally
- Cloud instance with:
- Open ports for SSH (22), iperf3 (5201), and ICMP (ping)
- Public IP address
- SSH key-based authentication configured
Installation
First, ensure you have iperf3 installed on your local machine:
Script Implementation
Create a new file named cloud-network-monitor.sh
:
#!/bin/bash
# Cloud provider configurations# Uncomment and configure your desired cloud provider
# AWS Configuration#CLOUD_HOST="[email protected]"#KEY_PATH="/root/.ssh/aws_key"
# Oracle Cloud Configuration#CLOUD_HOST="[email protected]" # Oracle instances use the public IP#KEY_PATH="/root/.ssh/oracle_key"
# DigitalOcean Configuration#CLOUD_HOST="[email protected]" # DigitalOcean droplets typically use root user#KEY_PATH="/root/.ssh/digitalocean_key"
# Common ConfigurationINTERVAL=60LOG_FILE="network_metrics.log"
# Function to validate configurationvalidate_config() { if [ -z "$CLOUD_HOST" ] || [ -z "$KEY_PATH" ]; then echo "Error: Please configure CLOUD_HOST and KEY_PATH variables" exit 1 fi
if [ ! -f "$KEY_PATH" ]; then echo "Error: SSH key not found at $KEY_PATH" exit 1 fi}
# Function to check network metricscheck_network() { local cloud_ip=$(echo $CLOUD_HOST | cut -d@ -f2) echo "=== Network Test $(date) ===" | tee -a $LOG_FILE echo "Testing connection to: $CLOUD_HOST" | tee -a $LOG_FILE
# Test ping latency (5 packets) echo "Testing latency..." | tee -a $LOG_FILE ping -c 5 $cloud_ip | tee -a $LOG_FILE
# Test bandwidth using iperf3 echo "Testing bandwidth..." | tee -a $LOG_FILE
# Start iperf3 server on cloud VM with error handling if ! ssh -i $KEY_PATH -o StrictHostKeyChecking=accept-new $CLOUD_HOST "which iperf3 || sudo apt-get update && sudo apt-get install -y iperf3"; then echo "Failed to ensure iperf3 is installed on cloud server" | tee -a $LOG_FILE return 1 fi
if ! ssh -i $KEY_PATH $CLOUD_HOST "iperf3 -s -D"; then echo "Failed to start iperf3 server on cloud instance" | tee -a $LOG_FILE return 1 fi
# Wait for server to start sleep 2
# Run bandwidth test locally iperf3 -c $cloud_ip -t 10 | tee -a $LOG_FILE
# Stop iperf3 server on cloud VM ssh -i $KEY_PATH $CLOUD_HOST "pkill iperf3"
echo "----------------------------------------" | tee -a $LOG_FILE}
# Validate configuration before startingvalidate_config
# Main loopwhile true; do check_network || echo "Network check failed, will retry in $INTERVAL seconds" sleep $INTERVALdone
Cloud Provider Setup
AWS Configuration
-
Configure your security group to allow:
Type: All ICMP - IPv4Source: Your home IPType: Custom TCPPort: 5201Source: Your home IPType: SSHPort: 22Source: Your home IP -
Ensure your SSH key is in OpenSSH format (if using .ppk):
puttygen /path/to/key.ppk -O private-openssh -o /root/.ssh/aws_keychmod 600 /root/.ssh/aws_key
Oracle Cloud Configuration
- Add the following security rules to your VCN:
TCP port 22 (SSH)TCP port 5201 (iperf3)ICMP Type 3 (ping)
DigitalOcean Configuration
- Create firewall rules in the DigitalOcean console:
TCP port 22 (SSH)TCP port 5201 (iperf3)ICMP (ping)
Usage
-
Set proper permissions for the script:
chmod +x cloud-network-monitor.sh -
Update the configuration section with your cloud instance details
-
Run the script:
./cloud-network-monitor.sh
The script will continuously monitor:
- Network latency using ping
- Bandwidth using iperf3
- Results are logged to network_metrics.log
Troubleshooting
If you encounter SSH key issues:
# Fix key permissionschmod 600 /path/to/your/key
# Test SSH connectionssh -i /path/to/your/key -v your_cloud_host
For iperf3 connection issues:
# Test iperf3 manuallyiperf3 -c your_cloud_ip -p 5201