#!/usr/bin/env bash
#
# Artifactory Prometheus Metrics Exporter
#
# Prometheus textfile collector exporter for JFrog Artifactory.
# Uses the Artifactory REST API to collect storage per repo, artifact
# counts, HTTP request stats, GC metrics, DB connections, JVM heap,
# and system health.
#
# Usage:
#   ARTIFACTORY_URL="https://artifactory.example.com" ARTIFACTORY_TOKEN="cmVmd..." ./artifactory-exporter.sh
#   ARTIFACTORY_URL="https://artifactory.example.com" ARTIFACTORY_TOKEN="cmVmd..." ./artifactory-exporter.sh --textfile
#   ARTIFACTORY_URL="https://artifactory.example.com" ARTIFACTORY_TOKEN="cmVmd..." ./artifactory-exporter.sh --install
#
# Parameters:
#   --textfile   Write to textfile collector directory
#   --install    Create cron job for automatic collection
#   --help       Show usage
#
# Environment:
#   ARTIFACTORY_URL    Artifactory base URL (required)
#   ARTIFACTORY_TOKEN  API token or access token (required)
#   TEXTFILE_DIR       Textfile collector directory (default: /var/lib/node_exporter/textfile_collector)
#   CURL_TIMEOUT       API request timeout in seconds (default: 10)
#
# Author:  Phil Connor
# Contact: contact@mylinux.work
# Website: https://mylinux.work
# License: MIT
# Version: 1.0
#
# Metrics Exported:
#   Core:
#     - artifactory_up
#     - artifactory_exporter_info{version}
#     - artifactory_health_status
#
#   Storage (per-repo):
#     - artifactory_repo_used_bytes{repo,type}
#     - artifactory_repo_artifact_count{repo,type}
#     - artifactory_repo_folder_count{repo,type}
#
#   Storage (totals):
#     - artifactory_storage_total_bytes
#     - artifactory_storage_used_bytes
#     - artifactory_storage_free_bytes
#     - artifactory_storage_binaries_count
#     - artifactory_storage_binaries_total_bytes
#     - artifactory_storage_optimization_percent
#
#   JVM:
#     - artifactory_jvm_heap_used_bytes
#     - artifactory_jvm_heap_max_bytes
#     - artifactory_jvm_heap_free_bytes
#     - artifactory_jvm_nonheap_used_bytes
#
#   Database:
#     - artifactory_db_pool_active
#     - artifactory_db_pool_idle
#     - artifactory_db_pool_max
#
#   HTTP:
#     - artifactory_http_requests_total{status}
#
#   Garbage Collection:
#     - artifactory_gc_duration_seconds
#     - artifactory_gc_freed_bytes
#     - artifactory_gc_last_run_timestamp
#
#   Exporter:
#     - artifactory_exporter_duration_seconds
#     - artifactory_exporter_last_run_timestamp

set -euo pipefail

# --- Configuration ---
readonly VERSION="1.0"
readonly SCRIPT_NAME="$(basename "$0")"
ARTIFACTORY_URL="${ARTIFACTORY_URL:-}"
ARTIFACTORY_TOKEN="${ARTIFACTORY_TOKEN:-}"
TEXTFILE_DIR="${TEXTFILE_DIR:-/var/lib/node_exporter/textfile_collector}"
CURL_TIMEOUT="${CURL_TIMEOUT:-10}"
TEXTFILE_MODE=false
OUTPUT=""
START_TIME=""

# --- Functions ---

usage() {
    cat <<EOF
Usage: $SCRIPT_NAME [OPTIONS]

Artifactory Prometheus Metrics Exporter

Options:
  --textfile    Write metrics to textfile collector directory
  --install     Create cron job for automatic collection
  --help        Show this help message

Environment Variables:
  ARTIFACTORY_URL    Artifactory base URL (required)
  ARTIFACTORY_TOKEN  API token or access token (required)
  TEXTFILE_DIR       Output directory (default: /var/lib/node_exporter/textfile_collector)
  CURL_TIMEOUT       Request timeout in seconds (default: 10)

Examples:
  ARTIFACTORY_URL="https://artifactory.example.com" ARTIFACTORY_TOKEN="cmVmd..." $SCRIPT_NAME
  ARTIFACTORY_URL="https://artifactory.example.com" ARTIFACTORY_TOKEN="cmVmd..." $SCRIPT_NAME --textfile
  ARTIFACTORY_URL="https://artifactory.example.com" ARTIFACTORY_TOKEN="cmVmd..." $SCRIPT_NAME --install
EOF
    exit 0
}

check_dependencies() {
    local missing=()
    for cmd in curl jq; do
        if ! command -v "$cmd" &>/dev/null; then
            missing+=("$cmd")
        fi
    done
    if [[ ${#missing[@]} -gt 0 ]]; then
        echo "ERROR: Missing required commands: ${missing[*]}" >&2
        echo "Install with: apt install ${missing[*]}  OR  dnf install ${missing[*]}" >&2
        exit 1
    fi
}

validate_config() {
    if [[ -z "$ARTIFACTORY_URL" ]]; then
        echo "ERROR: ARTIFACTORY_URL environment variable is required" >&2
        exit 1
    fi
    if [[ -z "$ARTIFACTORY_TOKEN" ]]; then
        echo "ERROR: ARTIFACTORY_TOKEN environment variable is required" >&2
        exit 1
    fi
    # Strip trailing slash
    ARTIFACTORY_URL="${ARTIFACTORY_URL%/}"
}

api_get() {
    local endpoint="$1"
    curl -sf --max-time "$CURL_TIMEOUT" \
        -H "Authorization: Bearer ${ARTIFACTORY_TOKEN}" \
        "${ARTIFACTORY_URL}${endpoint}" 2>/dev/null || echo ""
}

add_metric() {
    local name="$1"
    local type="$2"
    local help="$3"
    local value="$4"
    local labels="${5:-}"

    if [[ -n "$labels" ]]; then
        OUTPUT+="# HELP ${name} ${help}
# TYPE ${name} ${type}
${name}{${labels}} ${value}
"
    else
        OUTPUT+="# HELP ${name} ${help}
# TYPE ${name} ${type}
${name} ${value}
"
    fi
}

add_metric_value() {
    local name="$1"
    local value="$2"
    local labels="${3:-}"

    if [[ -n "$labels" ]]; then
        OUTPUT+="${name}{${labels}} ${value}
"
    else
        OUTPUT+="${name} ${value}
"
    fi
}

# Convert Artifactory human-readable size strings to bytes.
# Artifactory returns storage sizes as "1.23 GB", "456.78 MB", etc.
parse_size_to_bytes() {
    local size_str="$1"

    if [[ -z "$size_str" || "$size_str" == "null" ]]; then
        echo "0"
        return
    fi

    local number unit
    number=$(echo "$size_str" | grep -oP '[\d.]+' | head -1)
    unit=$(echo "$size_str" | grep -oP '[A-Za-z]+' | head -1)

    if [[ -z "$number" ]]; then
        echo "0"
        return
    fi

    case "${unit^^}" in
        BYTES|B)
            echo "$number" | awk '{printf "%.0f", $1}' ;;
        KB)
            echo "$number" | awk '{printf "%.0f", $1 * 1024}' ;;
        MB)
            echo "$number" | awk '{printf "%.0f", $1 * 1048576}' ;;
        GB)
            echo "$number" | awk '{printf "%.0f", $1 * 1073741824}' ;;
        TB)
            echo "$number" | awk '{printf "%.0f", $1 * 1099511627776}' ;;
        *)
            echo "$number" | awk '{printf "%.0f", $1}' ;;
    esac
}

# Parse percentage string like "85.43%" to a float.
parse_percent() {
    local pct_str="$1"

    if [[ -z "$pct_str" || "$pct_str" == "null" ]]; then
        echo "0"
        return
    fi

    echo "$pct_str" | grep -oP '[\d.]+' | head -1 || echo "0"
}

collect_health() {
    # Simple ping check
    local ping_result
    ping_result=$(api_get "/api/system/ping")

    if [[ -z "$ping_result" || "$ping_result" != "OK" ]]; then
        add_metric "artifactory_up" "gauge" "Artifactory reachability (1=up, 0=down)" "0"
        return 1
    fi

    add_metric "artifactory_up" "gauge" "Artifactory reachability (1=up, 0=down)" "1"

    # Detailed health check via router API
    local health_json
    health_json=$(api_get "/router/api/v1/system/health")

    if [[ -n "$health_json" ]]; then
        local node_state
        node_state=$(echo "$health_json" | jq -r '.node_state // .services[0].state // empty' 2>/dev/null)

        if [[ "$node_state" == "HEALTHY" ]]; then
            add_metric "artifactory_health_status" "gauge" "System health (1=healthy, 0=unhealthy)" "1"
        else
            add_metric "artifactory_health_status" "gauge" "System health (1=healthy, 0=unhealthy)" "0"
        fi
    else
        # Ping succeeded so system is at least partially healthy
        add_metric "artifactory_health_status" "gauge" "System health (1=healthy, 0=unhealthy)" "1"
    fi

    return 0
}

collect_storage() {
    local storage_json
    storage_json=$(api_get "/api/storageinfo")

    if [[ -z "$storage_json" ]]; then
        return
    fi

    # --- Total storage summary ---
    local total_space used_space free_space
    total_space=$(echo "$storage_json" | jq -r '.fileStoreSummary.totalSpace // empty' 2>/dev/null)
    used_space=$(echo "$storage_json" | jq -r '.fileStoreSummary.usedSpace // empty' 2>/dev/null)
    free_space=$(echo "$storage_json" | jq -r '.fileStoreSummary.freeSpace // empty' 2>/dev/null)

    [[ -n "$total_space" ]] && add_metric "artifactory_storage_total_bytes" "gauge" "Total file store capacity in bytes" "$(parse_size_to_bytes "$total_space")"
    [[ -n "$used_space" ]] && add_metric "artifactory_storage_used_bytes" "gauge" "Used file store space in bytes" "$(parse_size_to_bytes "$used_space")"
    [[ -n "$free_space" ]] && add_metric "artifactory_storage_free_bytes" "gauge" "Free file store space in bytes" "$(parse_size_to_bytes "$free_space")"

    # --- Binaries summary ---
    local binaries_count binaries_size optimization
    binaries_count=$(echo "$storage_json" | jq -r '.binariesSummary.binariesCount // empty' 2>/dev/null)
    binaries_size=$(echo "$storage_json" | jq -r '.binariesSummary.binariesSize // empty' 2>/dev/null)
    optimization=$(echo "$storage_json" | jq -r '.binariesSummary.optimization // empty' 2>/dev/null)

    if [[ -n "$binaries_count" ]]; then
        local clean_count
        clean_count=$(echo "$binaries_count" | tr -d ',')
        add_metric "artifactory_storage_binaries_count" "gauge" "Total number of binaries stored" "$clean_count"
    fi
    [[ -n "$binaries_size" ]] && add_metric "artifactory_storage_binaries_total_bytes" "gauge" "Total size of binaries in bytes" "$(parse_size_to_bytes "$binaries_size")"
    [[ -n "$optimization" ]] && add_metric "artifactory_storage_optimization_percent" "gauge" "Storage optimization percentage" "$(parse_percent "$optimization")"

    # --- Per-repository metrics ---
    local repo_count
    repo_count=$(echo "$storage_json" | jq -r '.repositoriesSummaryList | length // 0' 2>/dev/null)

    if [[ "$repo_count" -gt 0 ]]; then
        # Extract repo data as tab-separated lines: key, type, usedSpace, filesCount, foldersCount
        local repo_lines
        repo_lines=$(echo "$storage_json" | jq -r '
            .repositoriesSummaryList[]
            | select(.repoKey != "TOTAL")
            | [.repoKey, (.repoType // "UNKNOWN"), (.usedSpace // "0 bytes"), (.filesCount // 0), (.foldersCount // 0)]
            | @tsv
        ' 2>/dev/null)

        if [[ -n "$repo_lines" ]]; then
            OUTPUT+="# HELP artifactory_repo_used_bytes Repository used space in bytes
# TYPE artifactory_repo_used_bytes gauge
"
            while IFS=$'\t' read -r repo_key repo_type repo_used files_count folders_count; do
                local repo_bytes
                repo_bytes=$(parse_size_to_bytes "$repo_used")
                add_metric_value "artifactory_repo_used_bytes" "$repo_bytes" "repo=\"${repo_key}\",type=\"${repo_type}\""
            done <<< "$repo_lines"

            OUTPUT+="# HELP artifactory_repo_artifact_count Number of artifacts in repository
# TYPE artifactory_repo_artifact_count gauge
"
            while IFS=$'\t' read -r repo_key repo_type repo_used files_count folders_count; do
                add_metric_value "artifactory_repo_artifact_count" "$files_count" "repo=\"${repo_key}\",type=\"${repo_type}\""
            done <<< "$repo_lines"

            OUTPUT+="# HELP artifactory_repo_folder_count Number of folders in repository
# TYPE artifactory_repo_folder_count gauge
"
            while IFS=$'\t' read -r repo_key repo_type repo_used files_count folders_count; do
                add_metric_value "artifactory_repo_folder_count" "$folders_count" "repo=\"${repo_key}\",type=\"${repo_type}\""
            done <<< "$repo_lines"
        fi
    fi
}

collect_system_info() {
    # Try the open metrics endpoint first (Artifactory 7.x+)
    local metrics_text
    metrics_text=$(api_get "/api/v1/system/metrics")

    if [[ -n "$metrics_text" ]]; then
        # Parse JVM heap from open metrics format
        local heap_used heap_max heap_free nonheap_used
        heap_used=$(echo "$metrics_text" | grep -m1 'jvm_memory_used_bytes.*area="heap"' | grep -oP '[\d.]+$' || true)
        heap_max=$(echo "$metrics_text" | grep -m1 'jvm_memory_max_bytes.*area="heap"' | grep -oP '[\d.]+$' || true)
        heap_free=$(echo "$metrics_text" | grep -m1 'jvm_memory_committed_bytes.*area="heap"' | grep -oP '[\d.]+$' || true)
        nonheap_used=$(echo "$metrics_text" | grep -m1 'jvm_memory_used_bytes.*area="nonheap"' | grep -oP '[\d.]+$' || true)

        [[ -n "$heap_used" ]] && add_metric "artifactory_jvm_heap_used_bytes" "gauge" "JVM heap memory used" "${heap_used%.*}"
        [[ -n "$heap_max" ]] && add_metric "artifactory_jvm_heap_max_bytes" "gauge" "JVM heap memory maximum" "${heap_max%.*}"
        if [[ -n "$heap_free" && -n "$heap_used" ]]; then
            local free_calc
            free_calc=$(echo "$heap_free $heap_used" | awk '{printf "%.0f", $1 - $2}')
            add_metric "artifactory_jvm_heap_free_bytes" "gauge" "JVM heap memory free" "$free_calc"
        fi
        [[ -n "$nonheap_used" ]] && add_metric "artifactory_jvm_nonheap_used_bytes" "gauge" "JVM non-heap memory used" "${nonheap_used%.*}"

        # Parse DB pool from open metrics
        local db_active db_idle db_max
        db_active=$(echo "$metrics_text" | grep -m1 'db_pool_active_connections' | grep -oP '[\d.]+$' || true)
        db_idle=$(echo "$metrics_text" | grep -m1 'db_pool_idle_connections' | grep -oP '[\d.]+$' || true)
        db_max=$(echo "$metrics_text" | grep -m1 'db_pool_max_connections' | grep -oP '[\d.]+$' || true)

        [[ -n "$db_active" ]] && add_metric "artifactory_db_pool_active" "gauge" "Active database connections" "${db_active%.*}"
        [[ -n "$db_idle" ]] && add_metric "artifactory_db_pool_idle" "gauge" "Idle database connections" "${db_idle%.*}"
        [[ -n "$db_max" ]] && add_metric "artifactory_db_pool_max" "gauge" "Maximum database connections" "${db_max%.*}"

        return
    fi

    # Fallback: use system info endpoint (older Artifactory)
    local info_json
    info_json=$(api_get "/api/system/info")

    if [[ -z "$info_json" ]]; then
        return
    fi

    local heap_used_str heap_max_str heap_free_str
    heap_used_str=$(echo "$info_json" | jq -r '.["jvm.heap.used"] // empty' 2>/dev/null)
    heap_max_str=$(echo "$info_json" | jq -r '.["jvm.heap.max"] // empty' 2>/dev/null)
    heap_free_str=$(echo "$info_json" | jq -r '.["jvm.heap.free"] // empty' 2>/dev/null)

    [[ -n "$heap_used_str" ]] && add_metric "artifactory_jvm_heap_used_bytes" "gauge" "JVM heap memory used" "$(parse_size_to_bytes "$heap_used_str")"
    [[ -n "$heap_max_str" ]] && add_metric "artifactory_jvm_heap_max_bytes" "gauge" "JVM heap memory maximum" "$(parse_size_to_bytes "$heap_max_str")"
    [[ -n "$heap_free_str" ]] && add_metric "artifactory_jvm_heap_free_bytes" "gauge" "JVM heap memory free" "$(parse_size_to_bytes "$heap_free_str")"

    local db_active db_max
    db_active=$(echo "$info_json" | jq -r '.["db.pool.active"] // empty' 2>/dev/null)
    db_max=$(echo "$info_json" | jq -r '.["db.pool.max"] // empty' 2>/dev/null)

    [[ -n "$db_active" ]] && add_metric "artifactory_db_pool_active" "gauge" "Active database connections" "$db_active"
    [[ -n "$db_max" ]] && add_metric "artifactory_db_pool_max" "gauge" "Maximum database connections" "$db_max"
}

collect_http_stats() {
    # Try open metrics endpoint for HTTP stats (Artifactory 7.x+)
    local metrics_text
    metrics_text=$(api_get "/api/v1/system/metrics")

    if [[ -n "$metrics_text" ]]; then
        local http_2xx http_3xx http_4xx http_5xx
        http_2xx=$(echo "$metrics_text" | grep -m1 'http_response_total.*status="2xx"' | grep -oP '[\d.]+$' || true)
        http_3xx=$(echo "$metrics_text" | grep -m1 'http_response_total.*status="3xx"' | grep -oP '[\d.]+$' || true)
        http_4xx=$(echo "$metrics_text" | grep -m1 'http_response_total.*status="4xx"' | grep -oP '[\d.]+$' || true)
        http_5xx=$(echo "$metrics_text" | grep -m1 'http_response_total.*status="5xx"' | grep -oP '[\d.]+$' || true)

        OUTPUT+="# HELP artifactory_http_requests_total Total HTTP requests by status class
# TYPE artifactory_http_requests_total counter
"
        [[ -n "$http_2xx" ]] && add_metric_value "artifactory_http_requests_total" "${http_2xx%.*}" 'status="2xx"'
        [[ -n "$http_3xx" ]] && add_metric_value "artifactory_http_requests_total" "${http_3xx%.*}" 'status="3xx"'
        [[ -n "$http_4xx" ]] && add_metric_value "artifactory_http_requests_total" "${http_4xx%.*}" 'status="4xx"'
        [[ -n "$http_5xx" ]] && add_metric_value "artifactory_http_requests_total" "${http_5xx%.*}" 'status="5xx"'
    fi
}

collect_gc_info() {
    local gc_json
    gc_json=$(api_get "/api/system/storage/gc")

    if [[ -z "$gc_json" ]]; then
        return
    fi

    # Duration in milliseconds
    local gc_duration_ms
    gc_duration_ms=$(echo "$gc_json" | jq -r '.gcDurationMillis // empty' 2>/dev/null)

    if [[ -n "$gc_duration_ms" ]]; then
        local gc_duration_secs
        gc_duration_secs=$(echo "$gc_duration_ms" | awk '{printf "%.3f", $1 / 1000}')
        add_metric "artifactory_gc_duration_seconds" "gauge" "Duration of last garbage collection in seconds" "$gc_duration_secs"
    fi

    # Freed space
    local gc_freed_size
    gc_freed_size=$(echo "$gc_json" | jq -r '.freedSpace // empty' 2>/dev/null)

    if [[ -n "$gc_freed_size" ]]; then
        local gc_freed_bytes
        gc_freed_bytes=$(parse_size_to_bytes "$gc_freed_size")
        add_metric "artifactory_gc_freed_bytes" "gauge" "Bytes freed by last garbage collection" "$gc_freed_bytes"
    fi

    # Last run timestamp
    local gc_time
    gc_time=$(echo "$gc_json" | jq -r '.gcTime // empty' 2>/dev/null)

    if [[ -n "$gc_time" ]]; then
        # Try to convert ISO timestamp to epoch
        local gc_epoch
        gc_epoch=$(date -d "$gc_time" +%s 2>/dev/null || echo "")
        if [[ -n "$gc_epoch" ]]; then
            add_metric "artifactory_gc_last_run_timestamp" "gauge" "Unix timestamp of last garbage collection" "$gc_epoch"
        fi
    fi
}

write_output() {
    if [[ "$TEXTFILE_MODE" == true ]]; then
        local output_file="${TEXTFILE_DIR}/artifactory.prom"
        local temp_file="${output_file}.$$"

        mkdir -p "$TEXTFILE_DIR"
        echo "$OUTPUT" > "$temp_file"
        mv "$temp_file" "$output_file"
    else
        echo "$OUTPUT"
    fi
}

install_cron() {
    if [[ $EUID -ne 0 ]]; then
        echo "ERROR: --install requires root" >&2
        exit 1
    fi

    local script_path
    script_path=$(readlink -f "$0")

    cat > /etc/cron.d/artifactory-exporter <<EOF
# Artifactory Prometheus Exporter — runs every 2 minutes
ARTIFACTORY_URL=${ARTIFACTORY_URL}
ARTIFACTORY_TOKEN=${ARTIFACTORY_TOKEN}
TEXTFILE_DIR=${TEXTFILE_DIR}
*/2 * * * * root ${script_path} --textfile 2>/dev/null
EOF

    chmod 644 /etc/cron.d/artifactory-exporter
    echo "Installed cron job: /etc/cron.d/artifactory-exporter"
    echo "Metrics will be written to: ${TEXTFILE_DIR}/artifactory.prom"
}

# --- Main ---

main() {
    # Parse arguments
    for arg in "$@"; do
        case "$arg" in
            --textfile) TEXTFILE_MODE=true ;;
            --install)
                check_dependencies
                validate_config
                install_cron
                exit 0
                ;;
            --help|-h) usage ;;
            *) echo "Unknown option: $arg" >&2; usage ;;
        esac
    done

    check_dependencies
    validate_config

    START_TIME=$(date +%s%N)

    # Exporter info
    add_metric "artifactory_exporter_info" "gauge" "Exporter version information" "1" "version=\"${VERSION}\""

    # Collect metrics
    if collect_health; then
        collect_storage
        collect_system_info
        collect_http_stats
        collect_gc_info
    fi

    # Exporter performance
    local end_time duration
    end_time=$(date +%s%N)
    duration=$(echo "scale=2; ($end_time - $START_TIME) / 1000000000" | bc 2>/dev/null || echo "0")
    add_metric "artifactory_exporter_duration_seconds" "gauge" "Time to generate all metrics" "$duration"
    add_metric "artifactory_exporter_last_run_timestamp" "gauge" "Unix timestamp of last successful run" "$(date +%s)"

    write_output
}

main "$@"
