mirror of
https://github.com/vdsm/virtual-dsm.git
synced 2025-06-08 17:37:58 +08:00
Update utils.sh
This commit is contained in:
parent
20864993b5
commit
85d6f554a3
144
src/utils.sh
144
src/utils.sh
@ -1,5 +1,149 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
|
|
||||||
|
# Helper functions
|
||||||
|
|
||||||
|
info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "${1:-}" "\E[0m\n"; }
|
||||||
|
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: ${1:-}" "\E[0m\n" >&2; }
|
||||||
|
warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: ${1:-}" "\E[0m\n" >&2; }
|
||||||
|
|
||||||
|
formatBytes() {
|
||||||
|
local result
|
||||||
|
result=$(numfmt --to=iec --suffix=B "$1" | sed -r 's/([A-Z])/ \1/' | sed 's/ B/ bytes/g;')
|
||||||
|
local unit="${result//[0-9. ]}"
|
||||||
|
result="${result//[a-zA-Z ]/}"
|
||||||
|
if [[ "${2:-}" == "up" ]]; then
|
||||||
|
if [[ "$result" == *"."* ]]; then
|
||||||
|
result="${result%%.*}"
|
||||||
|
result=$((result+1))
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ "${2:-}" == "down" ]]; then
|
||||||
|
result="${result%%.*}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
echo "$result $unit"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
isAlive() {
|
||||||
|
local pid="$1"
|
||||||
|
|
||||||
|
if kill -0 "$pid" 2>/dev/null; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
pKill() {
|
||||||
|
local pid="$1"
|
||||||
|
|
||||||
|
{ kill -15 "$pid" || true; } 2>/dev/null
|
||||||
|
|
||||||
|
while isAlive "$pid"; do
|
||||||
|
sleep 0.2
|
||||||
|
done
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fWait() {
|
||||||
|
local name="$1"
|
||||||
|
|
||||||
|
while pgrep -f -l "$name" >/dev/null; do
|
||||||
|
sleep 0.2
|
||||||
|
done
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fKill() {
|
||||||
|
local name="$1"
|
||||||
|
|
||||||
|
{ pkill -f "$name" || true; } 2>/dev/null
|
||||||
|
fWait "$name"
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
escape () {
|
||||||
|
local s
|
||||||
|
s=${1//&/\&}
|
||||||
|
s=${s//</\<}
|
||||||
|
s=${s//>/\>}
|
||||||
|
s=${s//'"'/\"}
|
||||||
|
printf -- %s "$s"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
html() {
|
||||||
|
local title
|
||||||
|
local body
|
||||||
|
local script
|
||||||
|
local footer
|
||||||
|
|
||||||
|
title=$(escape "$APP")
|
||||||
|
title="<title>$title</title>"
|
||||||
|
footer=$(escape "$FOOTER1")
|
||||||
|
|
||||||
|
body=$(escape "$1")
|
||||||
|
if [[ "$body" == *"..." ]]; then
|
||||||
|
body="<p class=\"loading\">${body/.../}</p>"
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ -n "${2:-}" ] && script="$2" || script=""
|
||||||
|
|
||||||
|
local HTML
|
||||||
|
HTML=$(<"$TEMPLATE")
|
||||||
|
HTML="${HTML/\[1\]/$title}"
|
||||||
|
HTML="${HTML/\[2\]/$script}"
|
||||||
|
HTML="${HTML/\[3\]/$body}"
|
||||||
|
HTML="${HTML/\[4\]/$footer}"
|
||||||
|
HTML="${HTML/\[5\]/$FOOTER2}"
|
||||||
|
|
||||||
|
echo "$HTML" > "$PAGE"
|
||||||
|
echo "$body" > "$INFO"
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cpu() {
|
||||||
|
local ret
|
||||||
|
local cpu=""
|
||||||
|
|
||||||
|
ret=$(lscpu)
|
||||||
|
|
||||||
|
if grep -qi "model name" <<< "$ret"; then
|
||||||
|
cpu=$(echo "$ret" | grep -m 1 -i 'model name' | cut -f 2 -d ":" | awk '{$1=$1}1' | sed 's# @.*##g' | sed s/"(R)"//g | sed 's/[^[:alnum:] ]\+/ /g' | sed 's/ */ /g')
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${cpu// /}" ] && grep -qi "model:" <<< "$ret"; then
|
||||||
|
cpu=$(echo "$ret" | grep -m 1 -i 'model:' | cut -f 2 -d ":" | awk '{$1=$1}1' | sed 's# @.*##g' | sed s/"(R)"//g | sed 's/[^[:alnum:] ]\+/ /g' | sed 's/ */ /g')
|
||||||
|
fi
|
||||||
|
|
||||||
|
cpu="${cpu// CPU/}"
|
||||||
|
cpu="${cpu// 6 Core/}"
|
||||||
|
cpu="${cpu// 8 Core/}"
|
||||||
|
cpu="${cpu// 16 Core/}"
|
||||||
|
cpu="${cpu// 32 Core/}"
|
||||||
|
cpu="${cpu// 64 Core/}"
|
||||||
|
cpu="${cpu//10th Gen /}"
|
||||||
|
cpu="${cpu//11th Gen /}"
|
||||||
|
cpu="${cpu//12th Gen /}"
|
||||||
|
cpu="${cpu//13th Gen /}"
|
||||||
|
cpu="${cpu//14th Gen /}"
|
||||||
|
cpu="${cpu//15th Gen /}"
|
||||||
|
cpu="${cpu// Processor/}"
|
||||||
|
cpu="${cpu// Quad core/}"
|
||||||
|
cpu="${cpu// Core TM/ Core}"
|
||||||
|
cpu="${cpu// with Radeon Graphics/}"
|
||||||
|
cpu="${cpu// with Radeon Vega Graphics/}"
|
||||||
|
|
||||||
|
[ -z "${cpu// /}" ] && cpu="Unknown"
|
||||||
|
|
||||||
|
echo "$cpu"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user