set -ex

screenNum=$(lib::getScreenNum "$CONFIG_FILE")

echo 当前设备号: $screenNum


installNodeExporter() {
  mkdir -p /data/services/
  sed "s/{{screenNum}}/$screenNum/g" <<'EOF' > /data/services/docker-compose.yaml
services:
  nodeExporter:
    image: harbor.inner.xcastle.net/base/node-exporter:v1.9.0
    container_name: nodeExporter
    restart: unless-stopped
    stop_grace_period: 1s
    command:
      - '--path.rootfs=/host'
    network_mode: host
    pid: host
    volumes:
      - '/:/host:ro,rslave'

  nodeExporterPush:
    image: harbor.inner.xcastle.net/base/alpine:3.21-curl
    container_name: nodeExporterPush
    restart: unless-stopped
    stop_grace_period: 1s
    network_mode: host
    environment:
      screenNum: "{{screenNum}}"
      interval: 15
      enpointUrl: "https://pushgateway.inner.xcastle.net/metrics/job/screen/instance/"
      metricUrl: "http://localhost:9100/metrics"
    command: >
      sh -c "while true; do
        echo -n $(date '+%Y-%m-%d %H:%M:%S');
        curl -s -w ' status:%{http_code}, time:%{time_total}s' -H 'Authorization: Basic dHhyeTp0eHJ5MjAyNQ==' -X POST --data-binary @<(curl -s $${metricUrl}) $${enpointUrl}screen-$${screenNum};
        echo '';
        sleep $${interval}; 
      done"
EOF
  cd /data/services/ && docker compose up -d
}

installNodeExporter