#!/bin/bash ################################################################################ # Script Name: coolify-exporter.sh # Version: 1.0 # Description: Prometheus exporter for Coolify PaaS providing operational # metrics via the Coolify API — application status, deployment # counts, database health, server info, SSL certificate expiry, # and API health # # Author: Phil Connor # Contact: contact@mylinux.work # Website: https://mylinux.work # License: MIT # # Prerequisites: # - Coolify instance running with API enabled # - Coolify API token (generate in Settings → API Tokens) # - curl for API calls # - jq for JSON parsing # - netcat (nc) for HTTP mode # # Usage: # # Output to stdout # ./coolify-exporter.sh # # # HTTP server mode # ./coolify-exporter.sh --http -p 9196 # # # Textfile collector mode # ./coolify-exporter.sh --textfile # # # Custom API token and URL # ./coolify-exporter.sh --api-url http://coolify.local:8000 --api-token mytoken # # Metrics Exported: # - coolify_up - API reachability (1=up, 0=down) # - coolify_info{version} - Coolify version info # - coolify_applications_total - Total application count # - coolify_applications_by_status{status} - Applications by status # - coolify_deployments_total - Total deployments # - coolify_deployments_running - Currently running deployments # - coolify_deployments_failed_total - Total failed deployments # - coolify_databases_total - Total managed databases # - coolify_databases_running - Running databases # - coolify_servers_total - Total servers managed # - coolify_servers_reachable - Reachable servers # - coolify_services_total - Total services # - coolify_services_running - Running services # - coolify_exporter_duration_seconds - Script execution time # - coolify_exporter_last_run_timestamp - Last run timestamp # # Configuration: # Default HTTP port: 9196 # Default API URL: http://localhost:8000 # Textfile directory: /var/lib/node_exporter # ################################################################################ set -euo pipefail # ============================================================================ # CONFIGURATION VARIABLES # ============================================================================ TEXTFILE_DIR="/var/lib/node_exporter" OUTPUT_FILE="" HTTP_MODE=false HTTP_PORT=9196 API_URL="http://localhost:8000" API_TOKEN="" # ============================================================================ # HELPER FUNCTIONS # ============================================================================ show_usage() { cat <&2; exit 1 ;; esac done } # Check prerequisites # Returns: 0 if OK, 1 if error check_prerequisites() { if ! command -v curl >/dev/null 2>&1; then echo "ERROR: curl not found" >&2 return 1 fi if ! command -v jq >/dev/null 2>&1; then echo "ERROR: jq not found (required for JSON parsing)" >&2 return 1 fi if [ -z "$API_TOKEN" ]; then echo "ERROR: --api-token is required" >&2 return 1 fi return 0 } # Escape special characters in Prometheus label values # Args: $1 - string to escape # Returns: escaped string safe for Prometheus labels prom_escape() { local val="$1" val="${val//\\/\\\\}" val="${val//\"/\\\"}" val="${val//$'\n'/}" echo "$val" } # Make an authenticated API call # Args: $1 - API endpoint path (e.g., /api/v1/applications) # Returns: JSON response on stdout api_call() { local endpoint="$1" curl -s -X GET \ -H "Authorization: Bearer ${API_TOKEN}" \ -H "Accept: application/json" \ "${API_URL}${endpoint}" 2>/dev/null } # ============================================================================ # METRIC GENERATION # ============================================================================ # Generate all Prometheus metrics # Returns: Prometheus text format metrics on stdout generate_metrics() { local script_start script_start=$(date +%s) # Check prerequisites if ! check_prerequisites; then cat </dev/null) if [ -z "$version_response" ]; then cat </dev/null) if [ -z "$coolify_version" ] || [ "$coolify_version" = "null" ]; then cat </dev/null) running_apps=$(echo "$apps_response" | jq '[.[] | select(.status == "running")] | length' 2>/dev/null) stopped_apps=$(echo "$apps_response" | jq '[.[] | select(.status == "stopped" or .status == "exited")] | length' 2>/dev/null) exited_apps=$(echo "$apps_response" | jq '[.[] | select(.status == "restarting" or .status == "degraded")] | length' 2>/dev/null) total_apps=${total_apps:-0} running_apps=${running_apps:-0} stopped_apps=${stopped_apps:-0} exited_apps=${exited_apps:-0} fi cat </dev/null) running_deployments=$(echo "$deployments_response" | jq '[.[] | select(.status == "in_progress")] | length' 2>/dev/null) failed_deployments=$(echo "$deployments_response" | jq '[.[] | select(.status == "failed" or .status == "error")] | length' 2>/dev/null) queued_deployments=$(echo "$deployments_response" | jq '[.[] | select(.status == "queued")] | length' 2>/dev/null) total_deployments=${total_deployments:-0} running_deployments=${running_deployments:-0} failed_deployments=${failed_deployments:-0} queued_deployments=${queued_deployments:-0} fi cat </dev/null) running_databases=$(echo "$databases_response" | jq '[.[] | select(.status == "running")] | length' 2>/dev/null) total_databases=${total_databases:-0} running_databases=${running_databases:-0} fi cat </dev/null) reachable_servers=$(echo "$servers_response" | jq '[.[] | select(.settings.is_reachable == true)] | length' 2>/dev/null) total_servers=${total_servers:-0} reachable_servers=${reachable_servers:-0} fi cat </dev/null) running_services=$(echo "$services_response" | jq '[.[] | select(.status == "running")] | length' 2>/dev/null) total_services=${total_services:-0} running_services=${running_services:-0} fi cat <&2 if ! command -v nc >/dev/null 2>&1; then echo "ERROR: netcat (nc) required for HTTP mode" >&2 exit 1 fi # Infinite loop accepting HTTP requests while true; do { read -r request # Check if request is for /metrics endpoint if [[ "$request" =~ ^GET\ /metrics ]]; then echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/plain; version=0.0.4\r\n\r" generate_metrics else # Serve HTML landing page for other requests echo -e "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r" cat < Coolify Exporter v1.0

Coolify Prometheus Exporter v1.0

Metrics

Operational metrics from the Coolify API.

EOF fi } | nc -l -p "$HTTP_PORT" -q 1 2>/dev/null done } # ============================================================================ # MAIN EXECUTION # ============================================================================ # Main entry point - routes to appropriate output mode main() { parse_args "$@" if [ "$HTTP_MODE" = true ]; then # Run HTTP server (blocks until killed) run_http_server elif [ -n "$OUTPUT_FILE" ]; then # Textfile collector mode: write atomically using temp file local output_dir output_dir="$(dirname "$OUTPUT_FILE")" mkdir -p "$output_dir" # Create temp file in SAME directory for atomic rename (same filesystem) local temp_file temp_file=$(mktemp "${output_dir}/.coolify_metrics.XXXXXX") # Generate metrics to temp file if ! generate_metrics > "$temp_file" 2>/dev/null; then rm -f "$temp_file" echo "ERROR: Failed to generate metrics" >&2 exit 1 fi # Validate: file must exist, have content local file_lines file_lines=$(wc -l < "$temp_file" 2>/dev/null || echo 0) if [ "$file_lines" -lt 10 ]; then rm -f "$temp_file" echo "ERROR: Metrics file too small ($file_lines lines), keeping previous" >&2 exit 1 fi # Set permissions before move chmod 644 "$temp_file" # Atomic rename - no gap where file is missing mv -f "$temp_file" "$OUTPUT_FILE" echo "Metrics written to $OUTPUT_FILE ($file_lines lines)" >&2 else # Default: output to stdout generate_metrics fi } # Execute main function with all script arguments main "$@"