#!/bin/bash
# CloudWatch Agent Setup for Graph Databases
# Standardized monitoring for LadybugDB graph API instances
#
# Note: Environment is included in the namespace (e.g., RoboSystems/Graph/prod)
# rather than as a dimension, since CloudWatch Agent's append_dimensions
# doesn't reliably propagate custom dimensions to the OTEL-based config.

set -e

# Validate required environment variables
: ${DATABASE_TYPE:?"DATABASE_TYPE must be set (ladybug)"}
: ${CLOUDWATCH_NAMESPACE:?"CLOUDWATCH_NAMESPACE must be set"}
: ${DATA_DIR:?"DATA_DIR must be set"}

if [ "${DATABASE_TYPE}" != "ladybug" ]; then
  echo "ERROR: Unsupported DATABASE_TYPE: ${DATABASE_TYPE}"
  exit 1
fi

# Determine setup log path and disk monitoring path (writer or replica)
# NODE_TYPE is set by the userdata script (e.g., shared_replica, standard, large)
if [[ "${NODE_TYPE:-}" == *"replica"* ]]; then
  SETUP_LOG_PATH="/var/log/${DATABASE_TYPE}-replica-setup.log"
  # Replicas use S3 ATTACH (no EBS data volume), so monitor root filesystem
  DISK_MONITOR_PATH="/"
else
  SETUP_LOG_PATH="/var/log/${DATABASE_TYPE}-writer-setup.log"
  # Writers have an EBS volume mounted at DATA_DIR
  DISK_MONITOR_PATH="${DATA_DIR}"
fi

# Use ENVIRONMENT if already set, otherwise extract from namespace
# (e.g., RoboSystems/Graph/prod -> prod)
if [ -z "${ENVIRONMENT:-}" ]; then
  ENVIRONMENT="${CLOUDWATCH_NAMESPACE##*/}"
fi

# Use unified log group from CloudFormation
UNIFIED_LOG_GROUP="/robosystems/${ENVIRONMENT}/graph-api"

echo "=== Configuring CloudWatch Agent for ${DATABASE_TYPE} ==="
echo "Namespace: ${CLOUDWATCH_NAMESPACE}"
echo "Log group: ${UNIFIED_LOG_GROUP}"
echo "Data directory: ${DATA_DIR}"
echo "Setup log: ${SETUP_LOG_PATH}"

# Verify CloudWatch agent installation
if [ ! -d "/opt/aws/amazon-cloudwatch-agent" ]; then
  echo "Installing CloudWatch agent..."
  yum update -y
  yum install -y amazon-cloudwatch-agent
fi

# Create CloudWatch agent configuration
cat > /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json << EOF
{
  "agent": {
    "metrics_collection_interval": 60,
    "run_as_user": "cwagent"
  },
  "metrics": {
    "namespace": "${CLOUDWATCH_NAMESPACE}",
    "metrics_collected": {
      "cpu": {
        "measurement": [
          {
            "name": "cpu_usage_idle",
            "rename": "CPU_USAGE_IDLE",
            "unit": "Percent"
          },
          {
            "name": "cpu_usage_iowait",
            "rename": "CPU_USAGE_IOWAIT",
            "unit": "Percent"
          },
          {
            "name": "cpu_usage_active",
            "rename": "CPU_USAGE_ACTIVE",
            "unit": "Percent"
          },
          "cpu_time_guest"
        ],
        "totalcpu": true,
        "metrics_collection_interval": 60
      },
      "disk": {
        "measurement": [
          {
            "name": "used_percent",
            "rename": "DISK_USED_PERCENT",
            "unit": "Percent"
          },
          "used",
          "total"
        ],
        "metrics_collection_interval": 60,
        "resources": [
          "${DISK_MONITOR_PATH}"
        ]
      },
      "diskio": {
        "measurement": [
          "io_time",
          "read_bytes",
          "write_bytes"
        ],
        "metrics_collection_interval": 60,
        "resources": [
          "*"
        ]
      },
      "mem": {
        "measurement": [
          "mem_used_percent"
        ],
        "metrics_collection_interval": 60
      },
      "netstat": {
        "measurement": [
          "tcp_established",
          "tcp_time_wait"
        ],
        "metrics_collection_interval": 60
      }
    },
    "aggregation_dimensions": [["AutoScalingGroupName"]],
    "append_dimensions": {
      "InstanceId": "\${aws:InstanceId}",
      "InstanceType": "\${aws:InstanceType}",
      "AutoScalingGroupName": "\${aws:AutoScalingGroupName}"
    }
  },
  "logs": {
    "logs_collected": {
      "files": {
        "collect_list": [
          {
            "file_path": "${DATA_DIR}/logs/*.log",
            "log_group_name": "${UNIFIED_LOG_GROUP}",
            "log_stream_name": "{instance_id}/application",
            "retention_in_days": 30
          },
          {
            "file_path": "${SETUP_LOG_PATH}",
            "log_group_name": "${UNIFIED_LOG_GROUP}",
            "log_stream_name": "{instance_id}/setup",
            "retention_in_days": 30
          }
        ]
      }
    }
  }
}
EOF

# Set proper permissions for config file
chmod 0644 /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
chown root:root /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

# Start CloudWatch agent
echo "Starting CloudWatch Agent..."
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
  -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json || {
    echo "WARNING: CloudWatch Agent failed to start, but continuing with setup"
    echo "Error was: $?"
}

# Enable CloudWatch agent to start on boot
systemctl enable amazon-cloudwatch-agent || true

echo "✅ CloudWatch Agent configured successfully"
exit 0
