Skip to content

Commit a5f5c3d

Browse files
committed
feat(traffic): add reset-all and reset-users commands for daily consumption monitoring (#159)
- Add mtproxymax traffic reset-all to atomically reset both global and all per-user counters - Add mtproxymax traffic reset-users as an alias for secret reset-traffic all - Add mtproxymax traffic help and tab completion - Add hint to mtproxymax traffic reset-global prompting reset-all - Document daily consumption monitoring with cron in README.md - Add unit tests in tests/test_traffic_reset.sh
1 parent e0a6fa7 commit a5f5c3d

3 files changed

Lines changed: 102 additions & 29 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,13 @@ mtproxymax secret quota-reset alice 1 # Reset on the 1st
892892
mtproxymax secret quota-reset bob 15 # Reset on the 15th
893893
mtproxymax secret quota-reset alice off # Disable
894894

895-
# Reset the independent server-wide total (does not change user quotas)
896-
mtproxymax traffic reset-global
895+
# Reset traffic counters
896+
mtproxymax traffic reset-all -y # Reset BOTH server-wide total and all user counters
897+
mtproxymax traffic reset-users # Reset all per-user traffic counters (alias for secret reset-traffic all)
898+
mtproxymax traffic reset-global # Reset independent server-wide total counter only
899+
900+
# Daily resource consumption monitoring via cron:
901+
# 0 0 * * * /opt/mtproxymax/mtproxymax traffic reset-all -y >/dev/null 2>&1
897902

898903
# Global auto-rotate — rotates secrets older than N days
899904
mtproxymax auto-rotate 90 # Rotate every 90 days
@@ -1539,7 +1544,10 @@ mtproxymax tui-theme [theme] # Switch TUI color themes (dark, matrix,
15391544
<summary><b>Monitoring</b></summary>
15401545

15411546
```bash
1542-
mtproxymax traffic # Per-user traffic breakdown
1547+
mtproxymax traffic # Per-user traffic breakdown and server total
1548+
mtproxymax traffic reset-all [-y] # Reset both server-wide total and all user counters
1549+
mtproxymax traffic reset-users # Reset all per-user traffic counters
1550+
mtproxymax traffic reset-global [-y] # Reset server-wide total counter only
15431551
mtproxymax connections # Live active connections per user
15441552
mtproxymax metrics # Engine metrics dashboard
15451553
mtproxymax metrics live [seconds] # Auto-refresh metrics (default: 5s)
@@ -1617,6 +1625,7 @@ mtproxymax update # Check for script + engine updates
16171625

16181626
### v1.4.2-LTS — Telemt Engine 3.5.14, Self-Healing Resilience & Enterprise Hardening
16191627

1628+
- **Traffic Reset & Daily Monitoring Aliases ([#159](https://github.com/SamNet-dev/MTProxyMax/issues/159)):** Added `mtproxymax traffic reset-all` and `mtproxymax traffic reset-users` (plus `mtproxymax traffic help`) to allow resetting both global and per-user traffic counters in one step for daily consumption monitoring.
16201629
- **Persistent Middle-End NAT Overrides ([#158](https://github.com/SamNet-dev/MTProxyMax/pull/158)):** Added `middle_proxy_nat_ip` and `middle_proxy_nat_probe` to `mtproxymax tune` to persist public IPv4 egress address overrides for policy-routed networks (e.g. WireGuard/MikroTik) across config regenerations.
16211630
- **Telemt Engine 3.5.14 & Build Hardening ([#157](https://github.com/SamNet-dev/MTProxyMax/pull/157)):**
16221631
- Upgraded core Rust proxy engine to **3.5.14** (`9d5b896bb695c55e2905b82f276da84e45a0a2ec`), resolving Middle-End pool convergence failures and writer starvation under egress tunnels (#949), with autonomous conntrack recovery on absent iptables-nft chains (3.5.11) and replay-safe streaming downlink (3.5.13).

‎mtproxymax.sh‎

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,6 +4972,9 @@ _mtproxymax_completion() {
49724972
# Subcommands
49734973
[ "$cmd" = "resources" ] && [ "$COMP_CWORD" -eq 2 ] && COMPREPLY=( $(compgen -W "status clear set" -- "${cur}") )
49744974
case "$cmd" in
4975+
traffic)
4976+
[ "$COMP_CWORD" -eq 2 ] && COMPREPLY=( $(compgen -W "reset-all reset-global reset-users help" -- "${cur}") )
4977+
;;
49754978
secret)
49764979
if [ "$COMP_CWORD" -eq 2 ]; then
49774980
COMPREPLY=( $(compgen -W "add remove list info rotate clone rename enable disable limits setlimit setlimits link qr note stats sort top search archive unarchive archives generate-links extend bulk-extend disable-expired export import reset-traffic tag untag tags logs quota-reset purge-disabled sub export-json rename-prefix" -- "${cur}") )
@@ -6701,6 +6704,7 @@ run_traffic_reset_global() {
67016704
if [ "$force" != "--force" ] && [ "$force" != "-y" ]; then
67026705
echo -e "This will permanently reset the cumulative server-wide traffic counters."
67036706
echo -e "Per-secret traffic and quotas will NOT be affected."
6707+
echo -e "${DIM}Tip: To reset per-user traffic counters, run: mtproxymax traffic reset-users (or reset-all)${NC}"
67046708
echo -n -e "Are you sure you want to proceed? (y/N) "
67056709
read -r confirm
67066710
[[ "${confirm,,}" =~ ^y ]] || { log_error "Aborted."; return 0; }
@@ -6748,6 +6752,27 @@ run_traffic_reset_global() {
67486752
log_success "Global traffic has been successfully reset!"
67496753
}
67506754

6755+
run_traffic_reset_all() {
6756+
local force="${1:-}"
6757+
check_root
6758+
load_settings
6759+
load_secrets
6760+
6761+
echo -e "\n ── ⚠️ ${BOLD}Reset All Traffic Counters${NC} ──\n"
6762+
if [ "$force" != "--force" ] && [ "$force" != "-y" ]; then
6763+
echo -e "This will permanently reset BOTH server-wide and all per-user traffic counters."
6764+
echo -n -e "Are you sure you want to proceed? (y/N) "
6765+
read -r confirm
6766+
[[ "${confirm,,}" =~ ^y ]] || { log_error "Aborted."; return 0; }
6767+
fi
6768+
6769+
run_traffic_reset_global --force || return 1
6770+
secret_reset_traffic all || return 1
6771+
6772+
audit_log "All global and per-user traffic counters reset to 0"
6773+
log_success "All server-wide and per-user traffic counters have been successfully reset!"
6774+
}
6775+
67516776
run_guest() {
67526777
local guest_label="${1:-}"
67536778
local limit_str="${2:-24h}"
@@ -18881,33 +18906,62 @@ cli_main() {
1888118906

1888218907
traffic)
1888318908
local subcmd="${1:-}"
18884-
if [ "$subcmd" = "reset-global" ]; then
18885-
shift
18886-
run_traffic_reset_global "$@"
18887-
else
18888-
load_settings
18889-
load_secrets
18890-
echo ""
18891-
draw_header "TRAFFIC"
18892-
local t_in t_out conns
18893-
read -r t_in t_out conns <<< "$(get_cumulative_proxy_stats)"
18894-
# Batch-load all user stats
18895-
_load_all_cumulative_user_stats 2>/dev/null
18896-
echo ""
18897-
echo -e " ${BOLD}Total:${NC} ${SYM_DOWN} $(format_bytes "$t_in") ${SYM_UP} $(format_bytes "$t_out") ${BOLD}Connections:${NC} ${conns}"
18898-
echo ""
18909+
case "$subcmd" in
18910+
reset-global)
18911+
shift
18912+
run_traffic_reset_global "$@"
18913+
;;
18914+
reset-all)
18915+
shift
18916+
run_traffic_reset_all "$@"
18917+
;;
18918+
reset-users)
18919+
shift
18920+
check_root
18921+
load_settings
18922+
load_secrets
18923+
secret_reset_traffic all "$@"
18924+
;;
18925+
help|--help|-h)
18926+
echo ""
18927+
echo -e " ${BOLD}Usage:${NC} mtproxymax traffic [subcommand]"
18928+
echo ""
18929+
echo " Subcommands:"
18930+
echo " (none) Show server-wide total and per-user traffic breakdown"
18931+
echo " reset-all Reset BOTH server-wide total and all per-user traffic counters"
18932+
echo " reset-global Reset server-wide total counter only (leaves user quotas intact)"
18933+
echo " reset-users Reset all per-user traffic counters (alias for secret reset-traffic all)"
18934+
echo ""
18935+
;;
18936+
"")
18937+
load_settings
18938+
load_secrets
18939+
echo ""
18940+
draw_header "TRAFFIC"
18941+
local t_in t_out conns
18942+
read -r t_in t_out conns <<< "$(get_cumulative_proxy_stats)"
18943+
# Batch-load all user stats
18944+
_load_all_cumulative_user_stats 2>/dev/null
18945+
echo ""
18946+
echo -e " ${BOLD}Total:${NC} ${SYM_DOWN} $(format_bytes "$t_in") ${SYM_UP} $(format_bytes "$t_out") ${BOLD}Connections:${NC} ${conns}"
18947+
echo ""
1889918948

18900-
# Per-user breakdown
18901-
for i in "${!SECRETS_LABELS[@]}"; do
18902-
[ "${SECRETS_ENABLED[$i]}" = "true" ] || continue
18903-
local label="${SECRETS_LABELS[$i]}"
18904-
local u_in=${_batch_cum_in["$label"]:-0}
18905-
local u_out=${_batch_cum_out["$label"]:-0}
18906-
local u_conns=${_batch_cum_conns["$label"]:-0}
18907-
echo -e " ${GREEN}${SYM_OK}${NC} ${BOLD}${label}${NC}: ${SYM_DOWN} $(format_bytes "$u_in") ${SYM_UP} $(format_bytes "$u_out") conns: ${u_conns}"
18908-
done
18909-
echo ""
18910-
fi
18949+
# Per-user breakdown
18950+
for i in "${!SECRETS_LABELS[@]}"; do
18951+
[ "${SECRETS_ENABLED[$i]}" = "true" ] || continue
18952+
local label="${SECRETS_LABELS[$i]}"
18953+
local u_in=${_batch_cum_in["$label"]:-0}
18954+
local u_out=${_batch_cum_out["$label"]:-0}
18955+
local u_conns=${_batch_cum_conns["$label"]:-0}
18956+
echo -e " ${GREEN}${SYM_OK}${NC} ${BOLD}${label}${NC}: ${SYM_DOWN} $(format_bytes "$u_in") ${SYM_UP} $(format_bytes "$u_out") conns: ${u_conns}"
18957+
done
18958+
echo ""
18959+
;;
18960+
*)
18961+
log_error "Unknown traffic subcommand '$subcmd'. Run: mtproxymax traffic help"
18962+
return 1
18963+
;;
18964+
esac
1891118965
;;
1891218966

1891318967
connections)

‎tests/test_traffic_reset.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,15 @@ apply_pending_traffic_resets
122122
assert_eq "monthly reset clears stale daemon input" 0 "${_cum_user_in[bob]}"
123123
assert_eq "monthly reset clears stale daemon output" 0 "${_cum_user_out[bob]}"
124124

125+
# Exercise reset-all
126+
METRICS_AVAILABLE=true
127+
printf '999|888\n' > "$STATS_DIR/cumulative_traffic"
128+
printf 'alice|1000|2000\nbob|3000|4000\n' > "$STATS_DIR/user_traffic"
129+
run_traffic_reset_all --force
130+
assert_eq "reset-all succeeds" 0 "$?"
131+
assert_eq "reset-all clears global counter" "0|0" "$(cat "$STATS_DIR/cumulative_traffic")"
132+
assert_eq "reset-all clears user traffic file" "" "$(cat "$STATS_DIR/user_traffic")"
133+
assert_eq "reset-all queues users-all daemon command" "users-all" "$(tail -n 1 "$STATS_DIR/.traffic_reset_pending")"
134+
125135
printf '\n%d tests, %d failures\n' "$TESTS_RUN" "$TESTS_FAILED"
126136
[ "$TESTS_FAILED" -eq 0 ]

0 commit comments

Comments
 (0)