opt/zion-terminal/bin/zion-lib.sh (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#!/usr/bin/env bash
# =====================================================================
# ZION TERMINAL - Biblioteca compartilhada
# Cyberdeck criado por Pablo Murad em 2026
# ---------------------------------------------------------------------
# Funcoes de coleta de metricas, cores, sparklines e niveis de saude.
# Getters rapidos rodam ao vivo; getters lentos (internet, updates,
# peers) leem o cache /run/zion/status quando disponivel.
# =====================================================================
ZION_DIR="/opt/zion-terminal"
ZION_CACHE="/run/zion/status"
ZION_TEMA_FILE="$ZION_DIR/config/tema.sh"
# Carrega o tema de cores
[ -r "$ZION_TEMA_FILE" ] && . "$ZION_TEMA_FILE"
[ -r "$ZION_DIR/lib/zion-ui.sh" ] && . "$ZION_DIR/lib/zion-ui.sh"
# --- Utilidades de terminal ---------------------------------------
zion_cols() { local c; c="${COLUMNS:-}"; [ -z "$c" ] && c=$(tput cols 2>/dev/null); echo "${c:-80}"; }
zc() { # imprime com cor: zc "$ZP" "texto"
printf '%b%s%b' "$1" "$2" "$ZR"
}
# --- Metricas rapidas (ao vivo) -----------------------------------
zion_temp_c() {
local t=0 f
for f in /sys/class/thermal/thermal_zone0/temp; do
[ -r "$f" ] && t=$(cat "$f") && break
done
echo $(( t / 1000 ))
}
zion_load() { awk '{print $1}' /proc/loadavg; }
zion_cpu_pct() { # amostra rapida de ~0.25s
local a b i1 i2 t1 t2
a=($(sed -n '1{s/cpu *//;p}' /proc/stat)); i1=${a[3]}; t1=0; for v in "${a[@]}"; do t1=$((t1+v)); done
sleep 0.25
b=($(sed -n '1{s/cpu *//;p}' /proc/stat)); i2=${b[3]}; t2=0; for v in "${b[@]}"; do t2=$((t2+v)); done
local dt=$((t2-t1)) di=$((i2-i1))
[ "$dt" -le 0 ] && { echo 0; return; }
echo $(( (100*(dt-di)) / dt ))
}
zion_ram_used() { awk '/MemTotal/{t=$2}/MemAvailable/{a=$2}END{printf "%d",(t-a)/1024}' /proc/meminfo; }
zion_ram_total_mb() { awk '/MemTotal/{printf "%d",$2/1024}' /proc/meminfo; }
zion_ram_pct() { local u tm; u=$(zion_ram_used); tm=$(zion_ram_total_mb); [ "$tm" -gt 0 ] && echo $((100*u/tm)) || echo 0; }
zion_ram_str() {
local u tm; u=$(zion_ram_used); tm=$(zion_ram_total_mb)
if [ "$tm" -ge 1024 ]; then printf '%d MB / %d GB' "$u" $((tm/1024)); else printf '%d MB / %d MB' "$u" "$tm"; fi
}
zion_ram() { zion_ram_str; } # compatibilidade
zion_disco() {
df -BG --output=used,size,pcent / 2>/dev/null | awk 'NR==2{gsub(/G/,"");printf "%s GB / %s GB (%s)",$1,$2,$3}'
}
zion_disco_pct() { df --output=pcent / 2>/dev/null | awk 'NR==2{gsub(/[ %]/,"");print}'; }
zion_uptime() {
local s=$(cut -d. -f1 /proc/uptime) d h m
d=$((s/86400)); h=$(( (s%86400)/3600 )); m=$(( (s%3600)/60 ))
[ $d -gt 0 ] && printf '%dd %dh %dm' $d $h $m || { [ $h -gt 0 ] && printf '%dh %dm' $h $m || printf '%dm' $m; }
}
zion_ip_local() { ip -4 -o addr show scope global 2>/dev/null | awk '$2!="tailscale0"{print $4}' | cut -d/ -f1 | head -1; }
zion_ip_ts() { tailscale ip -4 2>/dev/null | head -1; }
zion_ssh_estado() { systemctl is-active ssh 2>/dev/null; }
zion_falhas() { systemctl --failed --no-legend 2>/dev/null | grep -c . ; }
# --- Metricas lentas (cache-first) --------------------------------
zion_cache_get() { # $1=chave -> valor do cache, ou vazio
[ -r "$ZION_CACHE" ] && sed -n "s/^$1=//p" "$ZION_CACHE" | head -1
}
zion_ts_estado() {
local v; v=$(zion_cache_get TS_ESTADO)
if [ -z "$v" ]; then
if tailscale status >/dev/null 2>&1; then v="conectado"; else v="parado"; fi
fi
echo "$v"
}
zion_internet() {
local v; v=$(zion_cache_get INTERNET)
if [ -z "$v" ]; then
if timeout 2 curl -fsS -o /dev/null --max-time 2 http://connectivitycheck.gstatic.com/generate_204 2>/dev/null; then
v="disponivel"; else v="indisponivel"; fi
fi
echo "$v"
}
zion_updates() {
local v; v=$(zion_cache_get UPDATES)
[ -z "$v" ] && v=$(apt list --upgradable 2>/dev/null | grep -vc Listing)
echo "${v:-0}"
}
# --- Niveis de saude (cor por faixa) ------------------------------
zion_nivel_temp() { # ecoa cor; define ZION_NIVEL=normal|atencao|critico
local t=$1
if [ "$t" -ge 80 ]; then ZION_NIVEL=critico; printf '%b' "$ZCRIT"
elif [ "$t" -ge 65 ]; then ZION_NIVEL=atencao; printf '%b' "$ZWARN"
else ZION_NIVEL=normal; printf '%b' "$ZOK"; fi
}
zion_nivel_pct() { # $1=percentual -> cor
local p=$1
if [ "$p" -ge 90 ]; then printf '%b' "$ZCRIT"
elif [ "$p" -ge 75 ]; then printf '%b' "$ZWARN"
else printf '%b' "$ZOK"; fi
}
# --- Sparkline Unicode --------------------------------------------
zion_sparkline() { # recebe numeros por stdin ou args -> ▁▂▃▄▅▆▇█
local nums=("$@") chars=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █) out="" min max n idx
[ ${#nums[@]} -eq 0 ] && read -r -a nums
[ ${#nums[@]} -eq 0 ] && { echo ""; return; }
min=${nums[0]}; max=${nums[0]}
for n in "${nums[@]}"; do [ "$n" -lt "$min" ] && min=$n; [ "$n" -gt "$max" ] && max=$n; done
local rng=$((max-min)); [ $rng -eq 0 ] && rng=1
for n in "${nums[@]}"; do idx=$(( (n-min)*7/rng )); out+="${chars[$idx]}"; done
printf '%s' "$out"
}
# --- Linha divisoria tematica -------------------------------------
zion_regua() { local w=${1:-$(zion_cols)}; printf '%b' "$ZMUT"; printf '─%.0s' $(seq 1 "$w"); printf '%b\n' "$ZR"; }
|