#!/usr/bin/env bash
#
# Nexus Repository Prometheus Metrics Exporter
#
# Prometheus textfile collector exporter for Sonatype Nexus Repository Manager.
# Uses the Nexus REST API to collect repository storage, component/asset
# counts, blob store usage, system status, active tasks, and disk usage.
#
# Usage:
#   NEXUS_URL="https://nexus.example.com" NEXUS_USER="admin" NEXUS_PASSWORD="secret" ./nexus-exporter.sh
#   NEXUS_URL="https://nexus.example.com" NEXUS_USER="admin" NEXUS_PASSWORD="secret" ./nexus-exporter.sh --textfile
#   NEXUS_URL="https://nexus.example.com" NEXUS_USER="admin" NEXUS_PASSWORD="secret" ./nexus-exporter.sh --install
#
# Parameters:
#   --textfile   Write to textfile collector directory
#   --install    Create cron job for automatic collection
#   --help       Show usage
#
# Environment:
#   NEXUS_URL        Nexus base URL (required)
#   NEXUS_USER       Username for API authentication (required)
#   NEXUS_PASSWORD   Password for API authentication (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:
#     - nexus_up
#     - nexus_exporter_info{version}
#     - nexus_system_status{node_id}
#
#   Repository Storage (per-repo):
#     - nexus_repo_size_bytes{repo,format,type}
#     - nexus_repo_component_count{repo,format,type}
#     - nexus_repo_asset_count{repo,format,type}
#
#   Blob Stores:
#     - nexus_blobstore_total_bytes{blobstore,type}
#     - nexus_blobstore_available_bytes{blobstore,type}
#     - nexus_blobstore_used_bytes{blobstore,type}
#     - nexus_blobstore_blob_count{blobstore,type}
#
#   Tasks:
#     - nexus_tasks_total{status}
#     - nexus_task_last_run_timestamp{task_name,task_type}
#
#   System:
#     - nexus_system_node_info{node_id,version,edition}
#     - nexus_system_filledescriptors_open
#     - nexus_system_filledescriptors_max
#
#   Exporter:
#     - nexus_exporter_duration_seconds
#     - nexus_exporter_last_run_timestamp

set -euo pipefail

# --- Configuration ---
readonly VERSION="1.0"
readonly SCRIPT_NAME="$(basename "$0")"
NEXUS_URL="${NEXUS_URL:-}"
NEXUS_USER="${NEXUS_USER:-}"
NEXUS_PASSWORD="${NEXUS_PASSWORD:-}"
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]

Nexus Repository 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:
  NEXUS_URL        Nexus base URL (required)
  NEXUS_USER       Username for API authentication (required)
  NEXUS_PASSWORD   Password for API authentication (required)
  TEXTFILE_DIR     Output directory (default: /var/lib/node_exporter/textfile_collector)
  CURL_TIMEOUT     Request timeout in seconds (default: 10)

Examples:
  NEXUS_URL="https://nexus.example.com" NEXUS_USER="admin" NEXUS_PASSWORD="secret" $SCRIPT_NAME
  NEXUS_URL="https://nexus.example.com" NEXUS_USER="admin" NEXUS_PASSWORD="secret" $SCRIPT_NAME --textfile
  NEXUS_URL="https://nexus.example.com" NEXUS_USER="admin" NEXUS_PASSWORD="secret" $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 "$NEXUS_URL" ]]; then
        echo "ERROR: NEXUS_URL environment variable is required" >&2
        exit 1
    fi
    if [[ -z "$NEXUS_USER" ]]; then
        echo "ERROR: NEXUS_USER environment variable is required" >&2
        exit 1
    fi
    if [[ -z "$NEXUS_PASSWORD" ]]; then
        echo "ERROR: NEXUS_PASSWORD environment variable is required" >&2
        exit 1
    fi
    # Strip trailing slash
    NEXUS_URL="${NEXUS_URL%/}"
}

api_get() {
    local endpoint="$1"
    curl -sf --max-time "$CURL_TIMEOUT" \
        -u "${NEXUS_USER}:${NEXUS_PASSWORD}" \
        "${NEXUS_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
}

collect_health() {
    # Nexus status endpoint
    local status_result
    status_result=$(api_get "/service/rest/v1/status")

    if [[ -z "$status_result" ]]; then
        add_metric "nexus_up" "gauge" "Nexus reachability (1=up, 0=down)" "0"
        return 1
    fi

    add_metric "nexus_up" "gauge" "Nexus reachability (1=up, 0=down)" "1"

    # Writable check (read-write vs read-only)
    local writable_result
    writable_result=$(api_get "/service/rest/v1/status/writable")

    if [[ -n "$writable_result" ]]; then
        add_metric "nexus_system_status" "gauge" "System writable status (1=writable, 0=read-only)" "1"
    else
        add_metric "nexus_system_status" "gauge" "System writable status (1=writable, 0=read-only)" "0"
    fi

    return 0
}

collect_system_info() {
    local status_check_json
    status_check_json=$(api_get "/service/rest/v1/status/check")

    if [[ -n "$status_check_json" ]]; then
        # Available system checks include connection pools, threads, etc.
        local available_cpus
        available_cpus=$(echo "$status_check_json" | jq -r '.["availableCPUs"] // empty' 2>/dev/null)
        [[ -n "$available_cpus" ]] && add_metric "nexus_system_available_cpus" "gauge" "Available CPU cores" "$available_cpus"
    fi

    # Node info from read endpoint
    local node_json
    node_json=$(api_get "/service/rest/v1/read-only")

    # System info — try to get version and edition from the status page
    local system_json
    system_json=$(api_get "/service/rest/v1/system/node")

    if [[ -n "$system_json" ]]; then
        local node_id version edition
        node_id=$(echo "$system_json" | jq -r '.nodeId // "unknown"' 2>/dev/null)
        version=$(echo "$system_json" | jq -r '.version // "unknown"' 2>/dev/null)
        edition=$(echo "$system_json" | jq -r '.edition // "unknown"' 2>/dev/null)

        add_metric "nexus_system_node_info" "gauge" "Nexus node information" "1" "node_id=\"${node_id}\",version=\"${version}\",edition=\"${edition}\""
    fi

    # File descriptors from metrics endpoint if available
    local metrics_json
    metrics_json=$(api_get "/service/metrics/data")

    if [[ -n "$metrics_json" ]]; then
        local fd_open fd_max
        fd_open=$(echo "$metrics_json" | jq -r '.gauges["jvm.fileDescriptorRatio"].value // empty' 2>/dev/null)

        # Try the Codahale/Dropwizard metrics format
        local heap_used heap_max
        heap_used=$(echo "$metrics_json" | jq -r '.gauges["jvm.memory.heap.used"].value // empty' 2>/dev/null)
        heap_max=$(echo "$metrics_json" | jq -r '.gauges["jvm.memory.heap.max"].value // empty' 2>/dev/null)

        if [[ -n "$heap_used" ]]; then
            add_metric "nexus_jvm_heap_used_bytes" "gauge" "JVM heap memory used" "${heap_used%.*}"
        fi
        if [[ -n "$heap_max" ]]; then
            add_metric "nexus_jvm_heap_max_bytes" "gauge" "JVM heap memory maximum" "${heap_max%.*}"
        fi

        local threads_count
        threads_count=$(echo "$metrics_json" | jq -r '.gauges["jvm.thread-count"].value // empty' 2>/dev/null)
        [[ -n "$threads_count" ]] && add_metric "nexus_jvm_thread_count" "gauge" "JVM thread count" "${threads_count%.*}"
    fi
}

collect_repositories() {
    local repos_json
    repos_json=$(api_get "/service/rest/v1/repositories")

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

    local repo_count
    repo_count=$(echo "$repos_json" | jq -r 'length // 0' 2>/dev/null)

    if [[ "$repo_count" -eq 0 ]]; then
        return
    fi

    # Extract repo data: name, format, type
    local repo_lines
    repo_lines=$(echo "$repos_json" | jq -r '
        .[]
        | [.name, (.format // "unknown"), (.type // "unknown")]
        | @tsv
    ' 2>/dev/null)

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

    # Collect per-repo component and asset counts
    OUTPUT+="# HELP nexus_repo_component_count Number of components in repository
# TYPE nexus_repo_component_count gauge
"
    while IFS=$'\t' read -r repo_name repo_format repo_type; do
        local comp_json
        comp_json=$(api_get "/service/rest/v1/search?repository=${repo_name}&format=${repo_format}")

        if [[ -n "$comp_json" ]]; then
            # The search API returns items array; use continuationToken-aware count
            local item_count
            item_count=$(echo "$comp_json" | jq -r '.items | length // 0' 2>/dev/null)
            add_metric_value "nexus_repo_component_count" "$item_count" "repo=\"${repo_name}\",format=\"${repo_format}\",type=\"${repo_type}\""
        else
            add_metric_value "nexus_repo_component_count" "0" "repo=\"${repo_name}\",format=\"${repo_format}\",type=\"${repo_type}\""
        fi
    done <<< "$repo_lines"

    OUTPUT+="# HELP nexus_repo_asset_count Number of assets in repository
# TYPE nexus_repo_asset_count gauge
"
    while IFS=$'\t' read -r repo_name repo_format repo_type; do
        local asset_json
        asset_json=$(api_get "/service/rest/v1/assets?repository=${repo_name}")

        if [[ -n "$asset_json" ]]; then
            local asset_count
            asset_count=$(echo "$asset_json" | jq -r '.items | length // 0' 2>/dev/null)
            add_metric_value "nexus_repo_asset_count" "$asset_count" "repo=\"${repo_name}\",format=\"${repo_format}\",type=\"${repo_type}\""
        else
            add_metric_value "nexus_repo_asset_count" "0" "repo=\"${repo_name}\",format=\"${repo_format}\",type=\"${repo_type}\""
        fi
    done <<< "$repo_lines"
}

collect_blobstores() {
    local blobs_json
    blobs_json=$(api_get "/service/rest/v1/blobstores")

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

    local blob_count
    blob_count=$(echo "$blobs_json" | jq -r 'length // 0' 2>/dev/null)

    if [[ "$blob_count" -eq 0 ]]; then
        return
    fi

    local blob_lines
    blob_lines=$(echo "$blobs_json" | jq -r '
        .[]
        | [.name, (.type // "unknown"), (.totalSizeInBytes // 0), (.availableSpaceInBytes // 0), (.blobCount // 0)]
        | @tsv
    ' 2>/dev/null)

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

    OUTPUT+="# HELP nexus_blobstore_total_bytes Total blob store size in bytes
# TYPE nexus_blobstore_total_bytes gauge
"
    while IFS=$'\t' read -r bs_name bs_type bs_total bs_avail bs_blobs; do
        add_metric_value "nexus_blobstore_total_bytes" "$bs_total" "blobstore=\"${bs_name}\",type=\"${bs_type}\""
    done <<< "$blob_lines"

    OUTPUT+="# HELP nexus_blobstore_available_bytes Available blob store space in bytes
# TYPE nexus_blobstore_available_bytes gauge
"
    while IFS=$'\t' read -r bs_name bs_type bs_total bs_avail bs_blobs; do
        add_metric_value "nexus_blobstore_available_bytes" "$bs_avail" "blobstore=\"${bs_name}\",type=\"${bs_type}\""
    done <<< "$blob_lines"

    OUTPUT+="# HELP nexus_blobstore_used_bytes Used blob store space in bytes
# TYPE nexus_blobstore_used_bytes gauge
"
    while IFS=$'\t' read -r bs_name bs_type bs_total bs_avail bs_blobs; do
        local used_bytes
        used_bytes=$(( bs_total - bs_avail ))
        [[ $used_bytes -lt 0 ]] && used_bytes=0
        add_metric_value "nexus_blobstore_used_bytes" "$used_bytes" "blobstore=\"${bs_name}\",type=\"${bs_type}\""
    done <<< "$blob_lines"

    OUTPUT+="# HELP nexus_blobstore_blob_count Number of blobs in blob store
# TYPE nexus_blobstore_blob_count gauge
"
    while IFS=$'\t' read -r bs_name bs_type bs_total bs_avail bs_blobs; do
        add_metric_value "nexus_blobstore_blob_count" "$bs_blobs" "blobstore=\"${bs_name}\",type=\"${bs_type}\""
    done <<< "$blob_lines"
}

collect_tasks() {
    local tasks_json
    tasks_json=$(api_get "/service/rest/v1/tasks")

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

    local total running waiting

    # Count tasks by current state
    total=$(echo "$tasks_json" | jq -r '.items | length // 0' 2>/dev/null)
    running=$(echo "$tasks_json" | jq -r '[.items[] | select(.currentState == "RUNNING")] | length' 2>/dev/null)
    waiting=$(echo "$tasks_json" | jq -r '[.items[] | select(.currentState == "WAITING")] | length' 2>/dev/null)

    OUTPUT+="# HELP nexus_tasks_total Total number of scheduled tasks by status
# TYPE nexus_tasks_total gauge
"
    add_metric_value "nexus_tasks_total" "$total" 'status="total"'
    add_metric_value "nexus_tasks_total" "$running" 'status="running"'
    add_metric_value "nexus_tasks_total" "$waiting" 'status="waiting"'

    # Per-task last run info
    local task_lines
    task_lines=$(echo "$tasks_json" | jq -r '
        .items[]
        | select(.lastRunResult != null)
        | [.name, (.typeId // "unknown"), (.lastRunResult // "UNKNOWN"), (.lastRun // "")]
        | @tsv
    ' 2>/dev/null)

    if [[ -n "$task_lines" ]]; then
        OUTPUT+="# HELP nexus_task_last_run_timestamp Unix timestamp of task last run
# TYPE nexus_task_last_run_timestamp gauge
"
        while IFS=$'\t' read -r task_name task_type task_result task_last_run; do
            if [[ -n "$task_last_run" ]]; then
                local task_epoch
                task_epoch=$(date -d "$task_last_run" +%s 2>/dev/null || echo "")
                if [[ -n "$task_epoch" ]]; then
                    add_metric_value "nexus_task_last_run_timestamp" "$task_epoch" "task_name=\"${task_name}\",task_type=\"${task_type}\""
                fi
            fi
        done <<< "$task_lines"

        OUTPUT+="# HELP nexus_task_last_run_result Result of task last run (1=OK, 0=failed)
# TYPE nexus_task_last_run_result gauge
"
        while IFS=$'\t' read -r task_name task_type task_result task_last_run; do
            local result_val=0
            [[ "$task_result" == "OK" ]] && result_val=1
            add_metric_value "nexus_task_last_run_result" "$result_val" "task_name=\"${task_name}\",task_type=\"${task_type}\""
        done <<< "$task_lines"
    fi
}

write_output() {
    if [[ "$TEXTFILE_MODE" == true ]]; then
        local output_file="${TEXTFILE_DIR}/nexus.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/nexus-exporter <<EOF
# Nexus Repository Prometheus Exporter — runs every 5 minutes
NEXUS_URL=${NEXUS_URL}
NEXUS_USER=${NEXUS_USER}
NEXUS_PASSWORD=${NEXUS_PASSWORD}
TEXTFILE_DIR=${TEXTFILE_DIR}
*/5 * * * * root ${script_path} --textfile 2>/dev/null
EOF

    chmod 644 /etc/cron.d/nexus-exporter
    echo "Installed cron job: /etc/cron.d/nexus-exporter"
    echo "Metrics will be written to: ${TEXTFILE_DIR}/nexus.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 "nexus_exporter_info" "gauge" "Exporter version information" "1" "version=\"${VERSION}\""

    # Collect metrics
    if collect_health; then
        collect_system_info
        collect_repositories
        collect_blobstores
        collect_tasks
    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 "nexus_exporter_duration_seconds" "gauge" "Time to generate all metrics" "$duration"
    add_metric "nexus_exporter_last_run_timestamp" "gauge" "Unix timestamp of last successful run" "$(date +%s)"

    write_output
}

main "$@"
