Skip to content

Instantly share code, notes, and snippets.

@fabriziosalmi
Created July 22, 2024 13:33
Show Gist options
  • Save fabriziosalmi/bf4046c661bd1278a278d38164e84195 to your computer and use it in GitHub Desktop.
Save fabriziosalmi/bf4046c661bd1278a278d38164e84195 to your computer and use it in GitHub Desktop.
Cloudflare Tunnels metrics display
#!/bin/bash
# Fetch metrics
metrics=$(curl -s http://127.0.0.1:60123/metrics)
# Define the metrics to be displayed
metric_names=(
"cloudflared_tcp_active_sessions"
"cloudflared_tunnel_active_streams"
"cloudflared_tunnel_ha_connections"
"cloudflared_tunnel_total_requests"
"cloudflared_tunnel_request_errors"
"cloudflared_tunnel_tunnel_authenticate_success"
"cloudflared_tunnel_tunnel_register_success"
"cloudflared_udp_active_sessions"
"cloudflared_udp_total_sessions"
"coredns_panics_total"
"go_goroutines"
"go_memstats_alloc_bytes"
"process_resident_memory_bytes"
"process_virtual_memory_bytes"
"process_cpu_seconds_total"
)
# Print header
echo -e "\e[1mMetric\t\t\t\tValue\e[0m"
echo "-----------------------------------------"
# Function to color values based on conditions
colorize_value() {
local metric=$1
local value=$2
case $metric in
cloudflared_tunnel_request_errors|coredns_panics_total)
[ "$value" -gt 0 ] && echo -e "\e[31m$value\e[0m" && return
;;
go_goroutines)
[ "$value" -gt 100 ] && echo -e "\e[33m$value\e[0m" && return
;;
process_cpu_seconds_total)
[ "$(echo "$value > 1.0" | bc -l)" -eq 1 ] && echo -e "\e[33m$value\e[0m" && return
;;
esac
echo "$value"
}
# Filter and format metrics
for metric in "${metric_names[@]}"; do
value=$(echo "$metrics" | grep "^$metric " | awk '{print $2}')
if [ -z "$value" ]; then
value="N/A"
fi
colored_value=$(colorize_value "$metric" "$value")
printf "%s\t\t%s\n" "$metric" "$colored_value"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment