Skip to content
Open
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
95 changes: 51 additions & 44 deletions mtproxymax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1622,8 +1622,8 @@ get_proxy_stats() {
local m
if m=$(_fetch_metrics); then
local bi bo conns
bi=$(echo "$m" | awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}')
bo=$(echo "$m" | awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}')
bi=$(echo "$m" | awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}')
bo=$(echo "$m" | awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}')
conns=$(echo "$m" | awk '/^telemt_user_connections_current\{/{s+=$NF}END{printf "%.0f",s}')
echo "${bi:-0} ${bo:-0} ${conns:-0}"
return
Expand Down Expand Up @@ -1685,8 +1685,8 @@ get_user_stats() {
local m
if m=$(_fetch_metrics); then
local i o c
i=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_from_client\\{.*user=\"" u "\"" {print $NF}')
o=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_to_client\\{.*user=\"" u "\"" {print $NF}')
i=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_from_client_total\\{.*user=\"" u "\"" {print $NF}')
o=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_octets_to_client_total\\{.*user=\"" u "\"" {print $NF}')
c=$(echo "$m" | awk -v u="$user" '$0 ~ "^telemt_user_connections_current\\{.*user=\"" u "\"" {print $NF}')
echo "${i:-0} ${o:-0} ${c:-0}"
return
Expand Down Expand Up @@ -1747,10 +1747,10 @@ _load_all_cumulative_user_stats() {
q = index(s, "\"")
return q ? substr(s, 1, q - 1) : ""
}
/^telemt_user_octets_from_client\{/ {
/^telemt_user_octets_from_client_total\{/ {
u = get_user($0); if (u) users_in[u] += $NF
}
/^telemt_user_octets_to_client\{/ {
/^telemt_user_octets_to_client_total\{/ {
u = get_user($0); if (u) users_out[u] += $NF
}
/^telemt_user_connections_current\{/ {
Expand Down Expand Up @@ -1834,8 +1834,8 @@ flush_traffic_to_disk() {
if $_have_metrics; then
# Global traffic delta
local cur_gin cur_gout
cur_gin=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}')
cur_gout=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}')
cur_gin=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}')
cur_gout=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}')
cur_gin=${cur_gin:-0}; cur_gout=${cur_gout:-0}
local gd_in=$((cur_gin - snap_gin)) gd_out=$((cur_gout - snap_gout))
[ "$gd_in" -lt 0 ] 2>/dev/null && gd_in=$cur_gin
Expand Down Expand Up @@ -1868,8 +1868,8 @@ flush_traffic_to_disk() {
[[ "$label" =~ ^# ]] && continue; [ -z "$secret" ] && continue
[ "$enabled" != "true" ] && continue
local ui uo
ui=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client\\{.*user=\"" u "\"" {print $NF}')
uo=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client\\{.*user=\"" u "\"" {print $NF}')
ui=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client_total\\{.*user=\"" u "\"" {print $NF}')
uo=$(echo "$_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client_total\\{.*user=\"" u "\"" {print $NF}')
ui=${ui:-0}; uo=${uo:-0}
local si=${_fu_snap_in["$label"]:-0} so=${_fu_snap_out["$label"]:-0}
local di=$((ui - si)) doo=$((uo - so))
Expand Down Expand Up @@ -2630,8 +2630,8 @@ secret_reset_traffic() {
q = index(s, "\"")
return q ? substr(s, 1, q - 1) : ""
}
/^telemt_user_octets_from_client\{/ { u = get_user($0); if (u) in_oct[u] += $NF }
/^telemt_user_octets_to_client\{/ { u = get_user($0); if (u) out_oct[u] += $NF }
/^telemt_user_octets_from_client_total\{/ { u = get_user($0); if (u) in_oct[u] += $NF }
/^telemt_user_octets_to_client_total\{/ { u = get_user($0); if (u) out_oct[u] += $NF }
END {
for (u in in_oct) {
printf "%s|%.0f|%.0f\n", u, in_oct[u], out_oct[u]
Expand Down Expand Up @@ -2663,8 +2663,8 @@ secret_reset_traffic() {

# Update user_traffic_snapshot to exact current live Prometheus values so delta becomes 0 right now
local live_in=0 live_out=0
live_in=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}')
live_out=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}')
live_in=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_from_client_total\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}')
live_out=$(echo "$_reset_metrics" | awk -v u="$label" '$0 ~ "^telemt_user_octets_to_client_total\\{.*user=\"" u "\"" {s+=$NF} END {printf "%.0f",s}')
[[ "${live_in:-0}" =~ ^[0-9]+$ ]] || live_in=0
[[ "${live_out:-0}" =~ ^[0-9]+$ ]] || live_out=0
if [ -f "$_snap" ]; then
Expand Down Expand Up @@ -2949,11 +2949,14 @@ show_connections() {
/^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF }
/^telemt_user_connections_total\{/ { u=lbl($0,"user"); if(u) ut[u]+=$NF }
/^telemt_user_unique_ips_current\{/ { u=lbl($0,"user"); if(u) ui[u]+=$NF }
/^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_connections_current / { total=$NF }
/^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
END {
for (u in uc) users[u]=1
# Global active connections are the sum of the per-user gauge. The aggregate
# telemt_connections_current was deleted in telemt c07b600, so reading it here
# left "Total active" at 0 for every 3.5.x release.
# See tests/test_metric_names.sh for the upstream history.
for (u in uc) { users[u]=1; total+=uc[u] }
for (u in ut) users[u]=1
for (u in ui) users[u]=1
for (u in rx) users[u]=1
Expand Down Expand Up @@ -3079,8 +3082,8 @@ secret_stats() {
q = index(s, "\""); return q ? substr(s, 1, q-1) : ""
}
/^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF }
/^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_user_unique_ips_current\{/ { u=lbl($0,"user"); if(u) ip[u]+=$NF }
END { for (u in uc) printf "%s|%.0f|%.0f|%.0f|%.0f\n", u, uc[u]+0, rx[u]+0, tx[u]+0, ip[u]+0 }
')
Expand Down Expand Up @@ -3426,8 +3429,8 @@ secret_info() {
local live; live=$(echo "$m" | awk -v u="$label" '
function lbl(s, k, p, q) { p=index(s,k"=\""); if(!p) return ""; s=substr(s,p+length(k)+2); q=index(s,"\""); return q ? substr(s,1,q-1) : "" }
/^telemt_user_connections_current\{/ { if(lbl($0,"user")==u) c+=$NF }
/^telemt_user_octets_from_client\{/ { if(lbl($0,"user")==u) rx+=$NF }
/^telemt_user_octets_to_client\{/ { if(lbl($0,"user")==u) tx+=$NF }
/^telemt_user_octets_from_client_total\{/ { if(lbl($0,"user")==u) rx+=$NF }
/^telemt_user_octets_to_client_total\{/ { if(lbl($0,"user")==u) tx+=$NF }
/^telemt_user_unique_ips_current\{/ { if(lbl($0,"user")==u) ip+=$NF }
END { printf "%.0f|%.0f|%.0f|%.0f", c+0, rx+0, tx+0, ip+0 }
')
Expand Down Expand Up @@ -3623,8 +3626,8 @@ secret_top() {
parsed=$(echo "$m" | awk '
function lbl(s, k, p, q) { p=index(s,k"=\""); if(!p) return ""; s=substr(s,p+length(k)+2); q=index(s,"\""); return q ? substr(s,1,q-1) : "" }
/^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF }
/^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
END { for(u in uc) printf "%s|%.0f|%.0f|%.0f\n", u, uc[u]+0, rx[u]+0, tx[u]+0 }
')

Expand Down Expand Up @@ -5138,8 +5141,8 @@ run_upload_test() {
_metrics=$(fetch_metrics 2>/dev/null || true)
if [ -n "$_metrics" ]; then
local up_bytes down_bytes
up_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}')
down_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}')
up_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}')
down_bytes=$(echo "$_metrics" | awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}')

local up_fmt; up_fmt=$(format_bytes "${up_bytes:-0}")
local down_fmt; down_fmt=$(format_bytes "${down_bytes:-0}")
Expand Down Expand Up @@ -6621,8 +6624,8 @@ run_traffic_reset_global() {
return 1
fi
local cur_in=0 cur_out=0
cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}')
cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}')
cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}')
cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}')
cur_in=${cur_in:-0}; cur_out=${cur_out:-0}

log_info "Resetting cumulative counters..."
Expand Down Expand Up @@ -11415,8 +11418,8 @@ get_stats() {
local m=$(curl -s --max-time 2 "http://127.0.0.1:${PROXY_METRICS_PORT:-9090}/metrics" 2>/dev/null)
[ -z "$m" ] && echo "0 0 0" && return
echo "$m" | awk '
/^telemt_user_octets_from_client\{/ {i+=$NF}
/^telemt_user_octets_to_client\{/ {o+=$NF}
/^telemt_user_octets_from_client_total\{/ {i+=$NF}
/^telemt_user_octets_to_client_total\{/ {o+=$NF}
/^telemt_user_connections_current\{/ {c+=$NF}
END {printf "%.0f %.0f %.0f\n",i+0,o+0,c+0}
'
Expand Down Expand Up @@ -11583,8 +11586,8 @@ update_traffic() {
_metrics=$(curl -s --max-time 2 "http://127.0.0.1:${PROXY_METRICS_PORT:-9090}/metrics" 2>/dev/null) || true
[ -z "$_metrics" ] && return 0
local cur_in cur_out
cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client\{/{s+=$NF}END{printf "%.0f",s}')
cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client\{/{s+=$NF}END{printf "%.0f",s}')
cur_in=$(echo "$_metrics"|awk '/^telemt_user_octets_from_client_total\{/{s+=$NF}END{printf "%.0f",s}')
cur_out=$(echo "$_metrics"|awk '/^telemt_user_octets_to_client_total\{/{s+=$NF}END{printf "%.0f",s}')
cur_in=${cur_in:-0}; cur_out=${cur_out:-0}

# Compute deltas (torware pattern: detect container restart by negative delta)
Expand All @@ -11604,8 +11607,8 @@ update_traffic() {
[ -n "$_pu" ] && { _parsed_ui["$_pu"]=${_pi:-0}; _parsed_uo["$_pu"]=${_po:-0}; }
done < <(echo "$_metrics" | awk '
function lbl(s, k, p, q) { p=index(s,k"=\""); if(!p) return ""; s=substr(s,p+length(k)+2); q=index(s,"\""); return q ? substr(s,1,q-1) : "" }
/^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
END { for(u in rx) printf "%s|%.0f|%.0f\n",u,rx[u]+0,tx[u]+0 }
')
fi
Expand Down Expand Up @@ -14360,9 +14363,9 @@ show_metrics() {
/^telemt_uptime_seconds / { uptime = $NF }
/^telemt_connections_total / { c_tot = $NF }
/^telemt_connections_bad_total / { c_bad = $NF }
/^telemt_connections_current / { c_cur = $NF }
/^telemt_connections_me_current / { c_me = $NF }
/^telemt_connections_direct_current / { c_dir = $NF }
# telemt_connections_current, ..._me_current and ..._direct_current were deleted
# in telemt c07b600; the aggregate is now derived from the per-user gauge below.
# See tests/test_metric_names.sh for the upstream history.
/^telemt_upstream_connect_attempt_total / { up_att = $NF }
/^telemt_upstream_connect_success_total / { up_ok = $NF }
/^telemt_upstream_connect_fail_total / { up_fail= $NF }
Expand All @@ -14379,12 +14382,13 @@ show_metrics() {
/^telemt_upstream_connect_duration_fail_total\{/ { b=lbl($0,"bucket"); if(b) df[b]+=$NF }
/^telemt_user_connections_current\{/ { u=lbl($0,"user"); if(u) uc[u]+=$NF }
/^telemt_user_connections_total\{/ { u=lbl($0,"user"); if(u) ut[u]+=$NF }
/^telemt_user_octets_from_client\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_user_octets_from_client_total\{/ { u=lbl($0,"user"); if(u) rx[u]+=$NF }
/^telemt_user_octets_to_client_total\{/ { u=lbl($0,"user"); if(u) tx[u]+=$NF }
/^telemt_user_unique_ips_current\{/ { u=lbl($0,"user"); if(u) ui[u]+=$NF }
END {
printf "S|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f\n",
uptime+0,c_tot+0,c_bad+0,c_cur+0,c_me+0,c_dir+0,
for (u in uc) c_cur += uc[u]
printf "S|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f|%.0f\n",
uptime+0,c_tot+0,c_bad+0,c_cur+0,
up_att+0,up_ok+0,up_fail+0,me_att+0,me_ok+0,
me_wa+0,me_ww+0,me_quar+0,me_crc+0,pool+0,desync+0,padinv+0
bkeys[1]="le_100ms"; bnames[1]="<=100ms"
Expand All @@ -14405,8 +14409,8 @@ show_metrics() {
')

# Parse scalar line
local uptime c_tot c_bad c_cur c_me c_dir up_att up_ok up_fail me_att me_ok me_wa me_ww me_quar me_crc pool desync padinv
IFS='|' read -r _ uptime c_tot c_bad c_cur c_me c_dir up_att up_ok up_fail \
local uptime c_tot c_bad c_cur up_att up_ok up_fail me_att me_ok me_wa me_ww me_quar me_crc pool desync padinv
IFS='|' read -r _ uptime c_tot c_bad c_cur up_att up_ok up_fail \
me_att me_ok me_wa me_ww me_quar me_crc pool desync padinv \
<<< "$(echo "$parsed" | grep '^S|')"

Expand Down Expand Up @@ -14435,7 +14439,10 @@ show_metrics() {

echo -e " ${BOLD}Connections${NC}"
echo -e " ${DIM}total:${NC} ${c_tot:-0} ${DIM}authorized:${NC} ${BRIGHT_GREEN}${c_good}${NC} ${DIM}rejected:${NC} ${BRIGHT_RED}${c_bad:-0}${NC}"
echo -e " ${DIM}active:${NC} ${c_cur:-0} (ME: ${c_me:-0} direct: ${c_dir:-0})"
# The ME / direct breakdown is gone: telemt removed those gauges in c07b600 and
# exposes no per-transport current-connection metric any more, so printing
# "(ME: 0 direct: 0)" forever would just assert a number that was never measured.
echo -e " ${DIM}active:${NC} ${c_cur:-0}"
echo ""

echo -e " ${BOLD}Upstream${NC}"
Expand Down
Loading