Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
mzfqy committed Dec 5, 2024
2 parents 6033daa + b9608cb commit 99c58fc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
4 changes: 2 additions & 2 deletions ak-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ "$EUID" -ne 0 ]; then
fi

function install_monitor() {
wget -O setup-monitor.sh "https://raw.githubusercontent.com/KunBuFenZi/ak-monitor/refs/heads/main/setup-monitor.sh"
wget -O setup-monitor.sh "https://raw.githubusercontent.com/akile-network/akile_monitor/refs/heads/main/setup-monitor.sh"
chmod +x setup-monitor.sh
./setup-monitor.sh
}
Expand All @@ -32,7 +32,7 @@ function install_client() {
read -p "Enter auth_secret: " auth_secret
read -p "Enter URL: " url
read -p "Enter name: " name
wget -O setup-client.sh "https://raw.githubusercontent.com/KunBuFenZi/ak-monitor/refs/heads/main/setup-client.sh"
wget -O setup-client.sh "https://raw.githubusercontent.com/akile-network/akile_monitor/refs/heads/main/setup-client.sh"
chmod +x setup-client.sh
./setup-client.sh "$auth_secret" "$url" "$name"
}
Expand Down
64 changes: 31 additions & 33 deletions setup-client.sh
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
#!/bin/bash
# Check if script is run as root

# Check if run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi

# Stop existing service if running
# Redirect all output to /dev/null and save original stdout
exec 3>&1
exec 1>/dev/null 2>&1

# Install bc if not present
if ! command -v bc > /dev/null; then
if command -v apt-get > /dev/null; then
apt-get update && apt-get install -y bc
elif command -v yum > /dev/null; then
yum update -y && yum install -y bc
fi
fi

# Restore original stdout
exec 1>&3

# Stop existing service
systemctl stop ak_client

# Function to detect main network interface
get_main_interface() {
# 检查是否安装了bc命令
if ! command -v bc > /dev/null; then
echo "Installing bc..."
if command -v apt-get > /dev/null; then
apt-get update && apt-get install -y bc
elif command -v yum > /dev/null; then
yum update -y && yum install -y bc
else
echo "Could not install bc. Please install it manually."
exit 1
fi
fi

local interfaces=$(ip -o link show | \
awk -F': ' '$2 !~ /^(lo|docker|veth|br-|virbr|tun|vnet|wg|vmbr|dummy|gre|sit|vlan|lxc|lxd|tap)/{print $2}' | \
awk -F': ' '$2 !~ /^(lo|warp|docker|veth|br-|virbr|tun|vnet|wg|vmbr|dummy|gre|sit|vlan|lxc|lxd|tap)/{print $2}' | \
grep -v '@')

local interface_count=$(echo "$interfaces" | wc -l)

# 格式化流量大小的函数
format_bytes() {
local bytes=$1
if [ $bytes -lt 1024 ]; then
echo "${bytes} B"
elif [ $bytes -lt 1048576 ]; then # 1024*1024
elif [ $bytes -lt 1048576 ]; then
echo "$(echo "scale=2; $bytes/1024" | bc) KB"
elif [ $bytes -lt 1073741824 ]; then # 1024*1024*1024
elif [ $bytes -lt 1073741824 ]; then
echo "$(echo "scale=2; $bytes/1024/1024" | bc) MB"
elif [ $bytes -lt 1099511627776 ]; then # 1024*1024*1024*1024
elif [ $bytes -lt 1099511627776 ]; then
echo "$(echo "scale=2; $bytes/1024/1024/1024" | bc) GB"
else
echo "$(echo "scale=2; $bytes/1024/1024/1024/1024" | bc) TB"
fi
}

# 显示网卡流量的函数
show_interface_traffic() {
local interface=$1
local rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes)
Expand All @@ -54,7 +56,6 @@ get_main_interface() {
echo " ↑ Sent: $(format_bytes $tx_bytes)"
}

# 如果没有找到合适的接口
if [ -z "$interfaces" ]; then
echo "No suitable physical network interfaces found." >&2
echo "All available interfaces:" >&2
Expand All @@ -71,7 +72,6 @@ get_main_interface() {
return
fi

# 如果只有一个合适的接口,直接使用它
if [ "$interface_count" -eq 1 ]; then
echo "Using single available interface:" >&2
echo "$interfaces" >&2
Expand All @@ -80,7 +80,6 @@ get_main_interface() {
return
fi

# 如果有多个合适的接口,让用户选择
echo "Multiple suitable interfaces found:" >&2
echo "------------------------" >&2
local i=1
Expand All @@ -95,18 +94,17 @@ get_main_interface() {
echo "$selected_interface"
}

# Check if all arguments are provided
# Check arguments
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <auth_secret> <url> <name>"
echo "Example: $0 your_secret wss://api.123.321 HK-Akile"
exit 1
fi

# Get system architecture
# Get architecture
ARCH=$(uname -m)
CLIENT_FILE="akile_client-linux-amd64"

# Set appropriate client file based on architecture
if [ "$ARCH" = "x86_64" ]; then
CLIENT_FILE="akile_client-linux-amd64"
elif [ "$ARCH" = "aarch64" ]; then
Expand All @@ -118,7 +116,7 @@ else
exit 1
fi

# Assign command line arguments to variables
# Set variables
auth_secret="$1"
url="$2"
monitor_name="$3"
Expand All @@ -135,7 +133,7 @@ cd /etc/ak_monitor/
wget -O client https://github.com/akile-network/akile_monitor/releases/latest/download/$CLIENT_FILE
chmod 777 client

# Create systemd service file
# Create service file
cat > /etc/systemd/system/ak_client.service << 'EOF'
[Unit]
Description=AkileCloud Monitor Service
Expand All @@ -153,13 +151,13 @@ LimitNOFILE=999999999
WorkingDirectory=/etc/ak_monitor/
ExecStart=/etc/ak_monitor/client
Restart=always
RestartSec=10
RestartSec=1
[Install]
WantedBy=multi-user.target
EOF

# Create client configuration
# Create config
cat > /etc/ak_monitor/client.json << EOF
{
"auth_secret": "${auth_secret}",
Expand All @@ -169,11 +167,11 @@ cat > /etc/ak_monitor/client.json << EOF
}
EOF

# Set proper permissions
# Set permissions
chmod 644 /etc/ak_monitor/client.json
chmod 644 /etc/systemd/system/ak_client.service

# Reload systemd and enable service
# Start service
systemctl daemon-reload
systemctl enable ak_client.service
systemctl start ak_client.service
Expand Down

0 comments on commit 99c58fc

Please sign in to comment.