# 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, pod memory) to $metricPromPath/kube.prom. function metricKube() { local fileProm="$metricPromPath/kube.prom" local fileTmp="${fileProm}.tmp" mkdir -p -- "$metricPromPath" || return 1 local vhostAllCount vhostUpCount vhostAllCount=$(openlitespeedConfigVhostList all | grep -c . || true) vhostUpCount=$(openlitespeedConfigVhostList 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) local masterMemReq=-1 masterMemLim=-1 local slaveMemReq=-1 slaveMemLim=-1 local olsMemReq=-1 olsMemLim=-1 local redisMemReq=-1 redisMemLim=-1 local _line _req _lim _line=$(k3sRun get pod "${mariadbMasterKube}-0" \ -o jsonpath="{.spec.containers[?(@.name=='${mariadbMasterKube}')].resources.requests.memory} {.spec.containers[?(@.name=='${mariadbMasterKube}')].resources.limits.memory}" 2>/dev/null) read -r _req _lim <<< "$_line" masterMemReq=$(sizeToBytes "$_req"); masterMemLim=$(sizeToBytes "$_lim") _line=$(k3sRun get pod "${mariadbSlaveKube}-0" \ -o jsonpath="{.spec.containers[?(@.name=='${mariadbSlaveKube}')].resources.requests.memory} {.spec.containers[?(@.name=='${mariadbSlaveKube}')].resources.limits.memory}" 2>/dev/null) read -r _req _lim <<< "$_line" slaveMemReq=$(sizeToBytes "$_req"); slaveMemLim=$(sizeToBytes "$_lim") _line=$(k3sRun get pods -l "app=$olsKube" \ -o jsonpath="{.items[0].spec.containers[?(@.name=='${olsKube}')].resources.requests.memory} {.items[0].spec.containers[?(@.name=='${olsKube}')].resources.limits.memory}" 2>/dev/null) read -r _req _lim <<< "$_line" olsMemReq=$(sizeToBytes "$_req"); olsMemLim=$(sizeToBytes "$_lim") _line=$(k3sRun get pod "${redisKube}-0" \ -o jsonpath="{.spec.containers[?(@.name=='${redisKube}')].resources.requests.memory} {.spec.containers[?(@.name=='${redisKube}')].resources.limits.memory}" 2>/dev/null) read -r _req _lim <<< "$_line" redisMemReq=$(sizeToBytes "$_req"); redisMemLim=$(sizeToBytes "$_lim") 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 run output error openlitespeedPodExec "$pod" sh -c "ps aux | awk '/lsphp/ && !/nobody/ {u=\$1;cpu[u]+=\$3;mem[u]+=\$4;count[u]++} END {for(u in cpu){name=u;cmd=\"find $olsPodPrivatePath -maxdepth 1 -uid \"u\" -type d 2>/dev/null\";if((cmd|getline dir)>0&&dir!=\"\"){n=split(dir,a,\"/\");name=a[n]};close(cmd);printf \"%.1f\\t%.1f\\t%d\\t%s\\n\",cpu[u],mem[u],count[u],name}}'" || continue while IFS=$'\t' 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 openlitespeedConfigVhostList || { appError "Error retrieving vhost list: $error" return 1 } while IFS= read -r domain <&3; do local diskUsage domainJson diskUsage="$(pathSize "$olsVhostsPath/$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 }