# 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" </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 }