Files
SODEW/sodew-bash-7.0.530/libs/modules/metric.sh
T
2026-08-12 11:59:14 +02:00

142 lines
5.5 KiB
Bash

# Restarts the vmagent deployment; errors are intentionally ignored.
function metricRestart() {
k3sRun rollout restart deployment/"$metricKube"-vmagent || true
}
# Writes Prometheus metrics (build info, vhost counts, domain counts) to $metricPromPath/kube.prom.
function metricKube() {
local fileProm="$metricPromPath/kube.prom"
local fileTmp="${fileProm}.tmp"
mkdir -p -- "$metricPromPath" || return 1
local vhostAllCount vhostUpCount
vhostAllCount=$(openlitespeedVhostList all | grep -c . || true)
vhostUpCount=$(openlitespeedVhostList up | grep -c . || true)
local appTimestamp
appTimestamp=$(date -d "$appDate ${appTime//-/:}" +%s)
local domainAllCount=-1 domainFreeCount=-1
[[ -f "$domainsList" ]] && domainAllCount=$(grep -cP '^(?!\s*$)' "$domainsList" || true)
[[ -f "$domainsList" ]] && domainFreeCount=$(grep -c '^#' "$domainsList" || true)
cat > "$fileTmp" <<EOF
# HELP kube_build_info Build and version info
# TYPE kube_build_info gauge
kube_build_info{version="${appVersion}"} 1
# HELP kube_start_time Time snapshot
# TYPE kube_start_time gauge
kube_start_time $appTimestamp
# HELP kube_vhost_all_count Number of virtual hosts
# TYPE kube_vhost_all_count gauge
kube_vhost_all_count $vhostAllCount
# HELP kube_vhost_up_count Number of active virtual hosts
# TYPE kube_vhost_up_count gauge
kube_vhost_up_count $vhostUpCount
# HELP kube_domain_all_count Number of domains
# TYPE kube_domain_all_count gauge
kube_domain_all_count $domainAllCount
# HELP kube_domain_use_count Number of use domains
# TYPE kube_domain_use_count gauge
kube_domain_use_count $domainFreeCount
EOF
mv "$fileTmp" "$fileProm"
}
# Collects lsphp CPU/memory/worker metrics per domain across OLS pods and writes to $metricPromPath/lsphp.prom.
function metricLsphp() {
local fileProm="$metricPromPath/lsphp.prom"
local fileTmp="${fileProm}.tmp"
mkdir -p -- "$metricPromPath" || return 1
local podList error
run podList error k3sPodListStatus "$openlitespeedKube" || return 1
local pod phase output cpu mem count domain
{
printf '# HELP lsphp_cpu_percent Total CPU percent used by lsphp workers per domain\n'
printf '# TYPE lsphp_cpu_percent gauge\n'
printf '# HELP lsphp_memory_percent Total memory percent used by lsphp workers per domain\n'
printf '# TYPE lsphp_memory_percent gauge\n'
printf '# HELP lsphp_worker_count Number of lsphp worker processes per domain\n'
printf '# TYPE lsphp_worker_count gauge\n'
while IFS='|' read -r pod phase; do
[[ "$phase" == "Running" ]] || continue
run output error openlitespeedPodExec "$pod" sh -c "ps aux | grep lsphp | grep -v grep | awk '{u=\$1;cpu[u]+=\$3;mem[u]+=\$4;count[u]++} END {for(u in cpu){name=u;cmd=\"find /var/www/data -maxdepth 1 -uid \"u\" -type d 2>/dev/null\";if((cmd|getline dir)>0&&dir!=\"\"){n=split(dir,a,\"/\");name=a[n]};close(cmd);print cpu[u],mem[u],count[u],name}}'" || continue
while IFS=' ' read -r cpu mem count domain; do
[[ -n "$domain" ]] || continue
printf 'lsphp_cpu_percent{domain="%s",pod="%s"} %s\n' "$domain" "$pod" "$cpu"
printf 'lsphp_memory_percent{domain="%s",pod="%s"} %s\n' "$domain" "$pod" "$mem"
printf 'lsphp_worker_count{domain="%s",pod="%s"} %s\n' "$domain" "$pod" "$count"
done <<< "$output"
done < <(awk 'NF' <<< "$podList")
} > "$fileTmp"
mv "$fileTmp" "$fileProm"
}
# Collects disk usage per site and sends metrics to the API.
function metricSites() {
local metricSitesApiUrl="https://api.sodew.ai/api/metrics/sites"
local batchId batchTime metricsJson dataJson payload httpCode
batchId="$(uuidgen | tr '[:upper:]' '[:lower:]')"
batchTime="$(date +%s)"
metricsJson='{
"site_disk_usage_mb": {"type":"number"}
}'
dataJson='{}'
local error vhostList
run vhostList error openlitespeedVhostList all || {
appError "Error retrieving vhost list: $error"
return 1
}
while IFS= read -r domain <&3; do
local diskUsage domainJson
diskUsage="$(pathSize "$vhostsPath/$domain" "mb" || true)"
[[ -n "$diskUsage" ]] || continue
domainJson="$(
jq -n --arg diskUsage "$diskUsage" \
'{
site_disk_usage_mb: { value: (if ($diskUsage | length) > 0 then ($diskUsage | tonumber) else null end) },
}'
)"
dataJson="$(jq --arg domain "$domain" --argjson row "$domainJson" '. + {($domain): $row}' <<< "$dataJson")"
done 3< <(awk 'NF' <<< "$vhostList")
payload="$(
jq -n \
--arg source_type "worker" \
--arg source_id "$hostUuid" \
--arg batch_id "$batchId" \
--argjson batch_time "$batchTime" \
--argjson metrics "$metricsJson" \
--argjson data "$dataJson" \
'{
source_type: $source_type,
source_id: $source_id,
batch_id: $batch_id,
batch_time: $batch_time,
metrics: $metrics,
data: $data
}'
)"
# Sending data...
httpCode="$(curl -sS -o /tmp/metrics_sites_response.txt -w '%{http_code}' -X POST "$metricSitesApiUrl" -H 'Content-Type: application/json' --data-binary "$payload")"
if [[ "$httpCode" != "204" ]]; then
appError "Ingest failed, HTTP $httpCode"
cat /tmp/metrics_sites_response.txt >&2 || true
return 1
fi
}