it-praxisportal/courses/bash/16_master_abschlussprojekt_devops_suite/musterloesung.sh

153 lines
3.9 KiB
Bash

#!/usr/bin/env bash
# ==============================================================================
# Master 16: DevOps Multi-Server Automation Suite
# MUSTERLÖSUNG
# ==============================================================================
parse_devops_cli_args() {
local config_file="servers.conf"
local parallel_workers=4
local dry_run="false"
while [[ $# -gt 0 ]]; do
case "$1" in
-c|--config)
config_file="$2"
shift 2
;;
-p|--parallel)
parallel_workers="$2"
shift 2
;;
--dry-run)
dry_run="true"
shift 1
;;
-h|--help)
echo "Usage: devops_suite [options]"
return 0
;;
*)
shift 1
;;
esac
done
echo "CONFIG: $config_file | WORKERS: $parallel_workers | DRY_RUN: $dry_run"
return 0
}
parallel_server_health_check() {
local servers_file="${1:-}"
local max_workers="${2:-4}"
if [ -z "$servers_file" ] || [ ! -f "$servers_file" ]; then
return 1
fi
local pids=()
while IFS= read -r server || [ -n "$server" ]; do
[ -z "$server" ] && continue
(
if [[ "$server" == *"offline"* ]]; then
echo "[HEALTH] $server -> OFFLINE"
else
echo "[HEALTH] $server -> ONLINE"
fi
) &
pids+=($!)
while [ $(jobs -rp | wc -l) -ge "$max_workers" ]; do
sleep 0.01
done
done < "$servers_file"
for pid in "${pids[@]}"; do
wait "$pid" 2>/dev/null || true
done
echo "[HEALTH_SUMMARY] ALL_CHECKS_COMPLETED"
return 0
}
analyze_server_logs_parallel() {
local log_dir="${1:-}"
local pattern="${2:-ERROR}"
local max_threads="${3:-4}"
if [ -z "$log_dir" ] || [ ! -d "$log_dir" ]; then
return 1
fi
local log_files=()
while IFS= read -r f; do
[ -n "$f" ] && log_files+=("$f")
done < <(find "$log_dir" -type f -name "*.log" 2>/dev/null)
local file_count=${#log_files[@]}
if [ "$file_count" -eq 0 ]; then
echo "INCIDENTS_FOUND: 0 | LOGS_SCANNED: 0"
return 0
fi
# Paralleles Durchsuchen via xargs und grep
local total_incidents
total_incidents=$(find "$log_dir" -type f -name "*.log" | xargs -P "$max_threads" -I {} grep -E -c "$pattern" {} 2>/dev/null | awk '{s+=$1} END {print s+0}')
echo "INCIDENTS_FOUND: $total_incidents | LOGS_SCANNED: $file_count"
return 0
}
build_webhook_payload() {
local webhook_url="${1:-}"
local title="${2:-}"
local message="${3:-}"
local severity="${4:-INFO}"
if [ -z "$webhook_url" ] || [ -z "$title" ] || [ -z "$message" ]; then
return 1
fi
echo "curl -s -f -X POST \"$webhook_url\" -H \"Content-Type: application/json\" -d '{\"title\":\"$title\",\"message\":\"$message\",\"severity\":\"$severity\"}'"
}
generate_devops_html_report() {
local title="${1:-}"
local healthy="${2:-0}"
local total="${3:-0}"
local incidents="${4:-0}"
local output_file="${5:-}"
if [ -z "$title" ] || [ -z "$output_file" ]; then
return 1
fi
cat <<EOF > "$output_file"
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>$title</title>
<link rel="stylesheet" href="../../../assets/style.css">
</head>
<body>
<div class="app-layout">
<div class="app-main">
<header>
<div class="header-badge">DevOps Fleet Monitor</div>
<h1>$title</h1>
</header>
<main class="container">
<section class="card">
<h2>Server Fleet Status</h2>
<p>Healthy Servers: <span class="badge-success">$healthy / $total</span></p>
<p>Incidents Detected: <span class="badge-error">$incidents</span></p>
</section>
</main>
</div>
</div>
</body>
</html>
EOF
return 0
}