#!/bin/bash
# QC registration watcher: tektonbkp, visualsync, moongdata
while true; do
  TS=$(date -u '+%Y-%m-%d %H:%M:%S')
  for ID in tektonbkp visualsync moongdata; do
    R=$(curl -s -m 12 -X POST https://global.quickconnect.to/Serv.php \
      -H 'Content-Type: application/json' \
      -d "[{\"version\":1,\"command\":\"get_server_info\",\"stop_when_error\":false,\"stop_when_success\":false,\"id\":\"dsm_portal_https\",\"serverID\":\"$ID\",\"is_gofile\":false}]")
    ERR=$(echo "$R" | grep -oE '"errno":[0-9]+' | head -1)
    echo "$TS $ID $ERR" >> /opt/drop/qc_watch.log
    if ! echo "$ERR" | grep -qE '"errno":[^0]'; then
      if [ -n "$R" ] && ! echo "$R" | grep -q '"errno":4'; then
        echo "$TS ALIVE $ID $R" >> /opt/drop/qc_alive.log
      fi
    fi
  done
  # keep log small: last 500 lines
  tail -500 /opt/drop/qc_watch.log > /opt/drop/qc_watch.tmp 2>/dev/null && mv /opt/drop/qc_watch.tmp /opt/drop/qc_watch.log
  sleep 180
done
