admin zrewidował ten Gist . Przejdź do rewizji
1 file changed, 313 insertions, 42 deletions
install-promtail.sh
| @@ -1,6 +1,7 @@ | |||
| 1 | 1 | #!/bin/bash | |
| 2 | 2 | ||
| 3 | - | # Promtail Installation and Configuration Script for Debian | |
| 3 | + | # Universal Promtail Installation and Configuration Script | |
| 4 | + | # Supports: Debian, Ubuntu, CentOS, RHEL, Fedora, Arch Linux, Alpine, Raspberry Pi OS | |
| 4 | 5 | # Usage: bash -c "$(curl -fsSL <your-gist-url>/install-promtail.sh)" | |
| 5 | 6 | ||
| 6 | 7 | set -e | |
| @@ -14,6 +15,12 @@ PROMTAIL_DIR="/opt/promtail" | |||
| 14 | 15 | CONFIG_DIR="/etc/promtail" | |
| 15 | 16 | LOG_DIR="/var/log/promtail" | |
| 16 | 17 | ||
| 18 | + | # OS Detection Variables | |
| 19 | + | OS="" | |
| 20 | + | DIST="" | |
| 21 | + | PACKAGE_MANAGER="" | |
| 22 | + | SERVICE_MANAGER="" | |
| 23 | + | ||
| 17 | 24 | # Colors for output | |
| 18 | 25 | RED='\033[0;31m' | |
| 19 | 26 | GREEN='\033[0;32m' | |
| @@ -38,6 +45,56 @@ log_error() { | |||
| 38 | 45 | echo -e "${RED}[ERROR]${NC} $1" | |
| 39 | 46 | } | |
| 40 | 47 | ||
| 48 | + | # Detect OS and Distribution | |
| 49 | + | detect_os() { | |
| 50 | + | log_info "Detecting operating system..." | |
| 51 | + | ||
| 52 | + | if [[ -f /etc/os-release ]]; then | |
| 53 | + | . /etc/os-release | |
| 54 | + | OS=$ID | |
| 55 | + | DIST=$VERSION_ID | |
| 56 | + | elif command -v lsb_release >/dev/null 2>&1; then | |
| 57 | + | OS=$(lsb_release -si | tr '[:upper:]' '[:lower:]') | |
| 58 | + | DIST=$(lsb_release -sr) | |
| 59 | + | elif [[ -f /etc/redhat-release ]]; then | |
| 60 | + | OS="rhel" | |
| 61 | + | DIST=$(cat /etc/redhat-release | sed 's/.*release \([0-9]\).*/\1/') | |
| 62 | + | else | |
| 63 | + | log_error "Cannot detect operating system" | |
| 64 | + | exit 1 | |
| 65 | + | fi | |
| 66 | + | ||
| 67 | + | # Determine package manager | |
| 68 | + | if command -v apt-get >/dev/null 2>&1; then | |
| 69 | + | PACKAGE_MANAGER="apt" | |
| 70 | + | elif command -v yum >/dev/null 2>&1; then | |
| 71 | + | PACKAGE_MANAGER="yum" | |
| 72 | + | elif command -v dnf >/dev/null 2>&1; then | |
| 73 | + | PACKAGE_MANAGER="dnf" | |
| 74 | + | elif command -v pacman >/dev/null 2>&1; then | |
| 75 | + | PACKAGE_MANAGER="pacman" | |
| 76 | + | elif command -v apk >/dev/null 2>&1; then | |
| 77 | + | PACKAGE_MANAGER="apk" | |
| 78 | + | else | |
| 79 | + | log_error "No supported package manager found" | |
| 80 | + | exit 1 | |
| 81 | + | fi | |
| 82 | + | ||
| 83 | + | # Determine service manager | |
| 84 | + | if command -v systemctl >/dev/null 2>&1 && systemctl --version >/dev/null 2>&1; then | |
| 85 | + | SERVICE_MANAGER="systemd" | |
| 86 | + | elif command -v service >/dev/null 2>&1; then | |
| 87 | + | SERVICE_MANAGER="sysv" | |
| 88 | + | elif command -v rc-service >/dev/null 2>&1; then | |
| 89 | + | SERVICE_MANAGER="openrc" | |
| 90 | + | else | |
| 91 | + | log_error "No supported service manager found" | |
| 92 | + | exit 1 | |
| 93 | + | fi | |
| 94 | + | ||
| 95 | + | log_info "Detected: OS=$OS, Package Manager=$PACKAGE_MANAGER, Service Manager=$SERVICE_MANAGER" | |
| 96 | + | } | |
| 97 | + | ||
| 41 | 98 | # Check if running as root | |
| 42 | 99 | check_root() { | |
| 43 | 100 | if [[ $EUID -ne 0 ]]; then | |
| @@ -59,21 +116,60 @@ check_promtail_installed() { | |||
| 59 | 116 | test_loki_connectivity() { | |
| 60 | 117 | log_info "Testing connectivity to Loki endpoint: $LOKI_ENDPOINT:$LOKI_PORT" | |
| 61 | 118 | ||
| 62 | - | if timeout 10 bash -c "</dev/tcp/$LOKI_ENDPOINT/$LOKI_PORT" 2>/dev/null; then | |
| 63 | - | log_success "Successfully connected to $LOKI_ENDPOINT:$LOKI_PORT" | |
| 64 | - | return 0 | |
| 65 | - | else | |
| 66 | - | log_error "Cannot reach $LOKI_ENDPOINT:$LOKI_PORT" | |
| 67 | - | log_error "Please check your network connection and Loki server status" | |
| 68 | - | return 1 | |
| 119 | + | if command -v nc >/dev/null 2>&1; then | |
| 120 | + | # Use netcat if available | |
| 121 | + | if timeout 10 nc -z "$LOKI_ENDPOINT" "$LOKI_PORT" 2>/dev/null; then | |
| 122 | + | log_success "Successfully connected to $LOKI_ENDPOINT:$LOKI_PORT" | |
| 123 | + | return 0 | |
| 124 | + | fi | |
| 125 | + | elif command -v telnet >/dev/null 2>&1; then | |
| 126 | + | # Use telnet as fallback | |
| 127 | + | if timeout 10 bash -c "echo 'quit' | telnet $LOKI_ENDPOINT $LOKI_PORT" 2>/dev/null | grep -q "Connected"; then | |
| 128 | + | log_success "Successfully connected to $LOKI_ENDPOINT:$LOKI_PORT" | |
| 129 | + | return 0 | |
| 130 | + | fi | |
| 131 | + | elif command -v curl >/dev/null 2>&1; then | |
| 132 | + | # Use curl as last resort | |
| 133 | + | if timeout 10 curl -s "http://$LOKI_ENDPOINT:$LOKI_PORT/ready" >/dev/null 2>&1; then | |
| 134 | + | log_success "Successfully connected to $LOKI_ENDPOINT:$LOKI_PORT" | |
| 135 | + | return 0 | |
| 136 | + | fi | |
| 69 | 137 | fi | |
| 138 | + | ||
| 139 | + | log_error "Cannot reach $LOKI_ENDPOINT:$LOKI_PORT" | |
| 140 | + | log_error "Please check your network connection and Loki server status" | |
| 141 | + | return 1 | |
| 70 | 142 | } | |
| 71 | 143 | ||
| 72 | 144 | # Install dependencies | |
| 73 | 145 | install_dependencies() { | |
| 74 | 146 | log_info "Installing dependencies..." | |
| 75 | - | apt-get update -qq | |
| 76 | - | apt-get install -y wget curl unzip systemd | |
| 147 | + | ||
| 148 | + | case $PACKAGE_MANAGER in | |
| 149 | + | "apt") | |
| 150 | + | export DEBIAN_FRONTEND=noninteractive | |
| 151 | + | apt-get update -qq | |
| 152 | + | apt-get install -y wget curl unzip netcat-openbsd || apt-get install -y wget curl unzip netcat | |
| 153 | + | ;; | |
| 154 | + | "yum") | |
| 155 | + | yum install -y wget curl unzip nc | |
| 156 | + | ;; | |
| 157 | + | "dnf") | |
| 158 | + | dnf install -y wget curl unzip nc | |
| 159 | + | ;; | |
| 160 | + | "pacman") | |
| 161 | + | pacman -Sy --noconfirm wget curl unzip netcat | |
| 162 | + | ;; | |
| 163 | + | "apk") | |
| 164 | + | apk update | |
| 165 | + | apk add wget curl unzip netcat-openbsd | |
| 166 | + | ;; | |
| 167 | + | *) | |
| 168 | + | log_error "Unsupported package manager: $PACKAGE_MANAGER" | |
| 169 | + | exit 1 | |
| 170 | + | ;; | |
| 171 | + | esac | |
| 172 | + | ||
| 77 | 173 | log_success "Dependencies installed" | |
| 78 | 174 | } | |
| 79 | 175 | ||
| @@ -81,7 +177,22 @@ install_dependencies() { | |||
| 81 | 177 | create_promtail_user() { | |
| 82 | 178 | if ! id "$PROMTAIL_USER" &>/dev/null; then | |
| 83 | 179 | log_info "Creating promtail user..." | |
| 84 | - | useradd --system --no-create-home --shell /bin/false $PROMTAIL_USER | |
| 180 | + | ||
| 181 | + | case $OS in | |
| 182 | + | "alpine") | |
| 183 | + | adduser -S -D -H -s /bin/false $PROMTAIL_USER | |
| 184 | + | ;; | |
| 185 | + | *) | |
| 186 | + | if command -v useradd >/dev/null 2>&1; then | |
| 187 | + | useradd --system --no-create-home --shell /bin/false $PROMTAIL_USER 2>/dev/null || \ | |
| 188 | + | useradd -r -M -s /bin/false $PROMTAIL_USER | |
| 189 | + | else | |
| 190 | + | log_error "Cannot create user - useradd not available" | |
| 191 | + | exit 1 | |
| 192 | + | fi | |
| 193 | + | ;; | |
| 194 | + | esac | |
| 195 | + | ||
| 85 | 196 | log_success "Promtail user created" | |
| 86 | 197 | else | |
| 87 | 198 | log_info "Promtail user already exists" | |
| @@ -102,7 +213,10 @@ install_promtail() { | |||
| 102 | 213 | ARCH_SUFFIX="arm64" | |
| 103 | 214 | ;; | |
| 104 | 215 | armv7l) | |
| 105 | - | ARCH_SUFFIX="armv7" | |
| 216 | + | ARCH_SUFFIX="arm" | |
| 217 | + | ;; | |
| 218 | + | arm*) | |
| 219 | + | ARCH_SUFFIX="arm" | |
| 106 | 220 | ;; | |
| 107 | 221 | *) | |
| 108 | 222 | log_error "Unsupported architecture: $ARCH" | |
| @@ -119,8 +233,16 @@ install_promtail() { | |||
| 119 | 233 | DOWNLOAD_URL="https://github.com/grafana/loki/releases/download/v$PROMTAIL_VERSION/promtail-linux-$ARCH_SUFFIX.zip" | |
| 120 | 234 | ||
| 121 | 235 | cd /tmp | |
| 122 | - | wget -q "$DOWNLOAD_URL" -O promtail.zip | |
| 123 | - | unzip -q promtail.zip | |
| 236 | + | log_info "Downloading from: $DOWNLOAD_URL" | |
| 237 | + | if ! wget --timeout=30 --tries=3 -q "$DOWNLOAD_URL" -O promtail.zip; then | |
| 238 | + | log_error "Failed to download Promtail. Check internet connection." | |
| 239 | + | exit 1 | |
| 240 | + | fi | |
| 241 | + | ||
| 242 | + | if ! unzip -q promtail.zip; then | |
| 243 | + | log_error "Failed to extract Promtail archive" | |
| 244 | + | exit 1 | |
| 245 | + | fi | |
| 124 | 246 | ||
| 125 | 247 | # Install binary | |
| 126 | 248 | chmod +x promtail-linux-$ARCH_SUFFIX | |
| @@ -208,6 +330,25 @@ EOF | |||
| 208 | 330 | log_success "Configuration created" | |
| 209 | 331 | } | |
| 210 | 332 | ||
| 333 | + | # Create service | |
| 334 | + | create_service() { | |
| 335 | + | case $SERVICE_MANAGER in | |
| 336 | + | "systemd") | |
| 337 | + | create_systemd_service | |
| 338 | + | ;; | |
| 339 | + | "sysv") | |
| 340 | + | create_sysv_service | |
| 341 | + | ;; | |
| 342 | + | "openrc") | |
| 343 | + | create_openrc_service | |
| 344 | + | ;; | |
| 345 | + | *) | |
| 346 | + | log_error "Unsupported service manager: $SERVICE_MANAGER" | |
| 347 | + | exit 1 | |
| 348 | + | ;; | |
| 349 | + | esac | |
| 350 | + | } | |
| 351 | + | ||
| 211 | 352 | # Create systemd service | |
| 212 | 353 | create_systemd_service() { | |
| 213 | 354 | log_info "Creating systemd service..." | |
| @@ -236,10 +377,111 @@ EOF | |||
| 236 | 377 | log_success "Systemd service created" | |
| 237 | 378 | } | |
| 238 | 379 | ||
| 239 | - | # Add promtail user to adm group for log access | |
| 380 | + | # Create SysV init script | |
| 381 | + | create_sysv_service() { | |
| 382 | + | log_info "Creating SysV init script..." | |
| 383 | + | ||
| 384 | + | cat > /etc/init.d/promtail << 'EOF' | |
| 385 | + | #!/bin/bash | |
| 386 | + | # promtail Promtail log collector | |
| 387 | + | # chkconfig: 35 80 20 | |
| 388 | + | # description: Promtail log collector for Grafana Loki | |
| 389 | + | ||
| 390 | + | . /etc/rc.d/init.d/functions | |
| 391 | + | ||
| 392 | + | USER="promtail" | |
| 393 | + | DAEMON="promtail" | |
| 394 | + | ROOT_DIR="/var/lib/promtail" | |
| 395 | + | ||
| 396 | + | SERVER="$ROOT_DIR/$DAEMON" | |
| 397 | + | LOCK_FILE="/var/lock/subsys/promtail" | |
| 398 | + | ||
| 399 | + | start() { | |
| 400 | + | if [ -f $LOCK_FILE ]; then | |
| 401 | + | echo "promtail is locked." | |
| 402 | + | return 1 | |
| 403 | + | fi | |
| 404 | + | ||
| 405 | + | echo -n $"Shutting down $DAEMON: " | |
| 406 | + | pid=`ps -aefw | grep "$DAEMON" | grep -v " grep " | awk '{print $2}'` | |
| 407 | + | kill -9 $pid > /dev/null 2>&1 | |
| 408 | + | [ $? -eq 0 ] && echo "OK" || echo "FAILED" | |
| 409 | + | } | |
| 410 | + | ||
| 411 | + | stop() { | |
| 412 | + | echo -n $"Shutting down $DAEMON: " | |
| 413 | + | pid=`ps -aefw | grep "$DAEMON" | grep -v " grep " | awk '{print $2}'` | |
| 414 | + | kill -9 $pid > /dev/null 2>&1 | |
| 415 | + | [ $? -eq 0 ] && echo "OK" || echo "FAILED" | |
| 416 | + | rm -f $LOCK_FILE | |
| 417 | + | } | |
| 418 | + | ||
| 419 | + | case "$1" in | |
| 420 | + | start) | |
| 421 | + | start | |
| 422 | + | ;; | |
| 423 | + | stop) | |
| 424 | + | stop | |
| 425 | + | ;; | |
| 426 | + | status) | |
| 427 | + | status $DAEMON | |
| 428 | + | ;; | |
| 429 | + | restart) | |
| 430 | + | stop | |
| 431 | + | start | |
| 432 | + | ;; | |
| 433 | + | *) | |
| 434 | + | echo "Usage: {start|stop|status|restart}" | |
| 435 | + | exit 1 | |
| 436 | + | ;; | |
| 437 | + | esac | |
| 438 | + | ||
| 439 | + | exit $? | |
| 440 | + | EOF | |
| 441 | + | ||
| 442 | + | chmod +x /etc/init.d/promtail | |
| 443 | + | chkconfig --add promtail 2>/dev/null || update-rc.d promtail defaults | |
| 444 | + | log_success "SysV service created" | |
| 445 | + | } | |
| 446 | + | ||
| 447 | + | # Create OpenRC service | |
| 448 | + | create_openrc_service() { | |
| 449 | + | log_info "Creating OpenRC service..." | |
| 450 | + | ||
| 451 | + | cat > /etc/init.d/promtail << EOF | |
| 452 | + | #!/sbin/openrc-run | |
| 453 | + | ||
| 454 | + | name="promtail" | |
| 455 | + | description="Promtail log collector" | |
| 456 | + | ||
| 457 | + | command="/usr/local/bin/promtail" | |
| 458 | + | command_args="-config.file=$CONFIG_DIR/promtail.yml" | |
| 459 | + | command_user="$PROMTAIL_USER" | |
| 460 | + | command_background="yes" | |
| 461 | + | pidfile="/run/\${RC_SVCNAME}.pid" | |
| 462 | + | ||
| 463 | + | depend() { | |
| 464 | + | need net | |
| 465 | + | after firewall | |
| 466 | + | } | |
| 467 | + | EOF | |
| 468 | + | ||
| 469 | + | chmod +x /etc/init.d/promtail | |
| 470 | + | log_success "OpenRC service created" | |
| 471 | + | } | |
| 472 | + | ||
| 473 | + | # Add promtail user to log access groups | |
| 240 | 474 | configure_log_access() { | |
| 241 | 475 | log_info "Configuring log file access..." | |
| 242 | - | usermod -a -G adm $PROMTAIL_USER | |
| 476 | + | ||
| 477 | + | # Try to add to common log access groups | |
| 478 | + | for group in adm systemd-journal wheel; do | |
| 479 | + | if getent group $group >/dev/null 2>&1; then | |
| 480 | + | usermod -a -G $group $PROMTAIL_USER 2>/dev/null || true | |
| 481 | + | log_info "Added to group: $group" | |
| 482 | + | fi | |
| 483 | + | done | |
| 484 | + | ||
| 243 | 485 | log_success "Log access configured" | |
| 244 | 486 | } | |
| 245 | 487 | ||
| @@ -247,21 +489,41 @@ configure_log_access() { | |||
| 247 | 489 | start_service() { | |
| 248 | 490 | log_info "Starting Promtail service..." | |
| 249 | 491 | ||
| 250 | - | systemctl enable promtail | |
| 251 | - | systemctl start promtail | |
| 252 | - | ||
| 253 | - | # Wait a moment and check status | |
| 254 | - | sleep 2 | |
| 255 | - | ||
| 256 | - | if systemctl is-active --quiet promtail; then | |
| 257 | - | log_success "Promtail service is running" | |
| 258 | - | log_info "Service status:" | |
| 259 | - | systemctl status promtail --no-pager -l | |
| 260 | - | else | |
| 261 | - | log_error "Failed to start Promtail service" | |
| 262 | - | log_error "Check logs with: journalctl -u promtail -f" | |
| 263 | - | exit 1 | |
| 264 | - | fi | |
| 492 | + | case $SERVICE_MANAGER in | |
| 493 | + | "systemd") | |
| 494 | + | systemctl enable promtail | |
| 495 | + | systemctl start promtail | |
| 496 | + | sleep 2 | |
| 497 | + | if systemctl is-active --quiet promtail; then | |
| 498 | + | log_success "Promtail service is running" | |
| 499 | + | systemctl status promtail --no-pager -l | |
| 500 | + | else | |
| 501 | + | log_error "Failed to start Promtail service" | |
| 502 | + | log_error "Check logs with: journalctl -u promtail -f" | |
| 503 | + | exit 1 | |
| 504 | + | fi | |
| 505 | + | ;; | |
| 506 | + | "sysv") | |
| 507 | + | service promtail start | |
| 508 | + | chkconfig promtail on 2>/dev/null || update-rc.d promtail enable | |
| 509 | + | if service promtail status >/dev/null 2>&1; then | |
| 510 | + | log_success "Promtail service is running" | |
| 511 | + | else | |
| 512 | + | log_error "Failed to start Promtail service" | |
| 513 | + | exit 1 | |
| 514 | + | fi | |
| 515 | + | ;; | |
| 516 | + | "openrc") | |
| 517 | + | rc-update add promtail default | |
| 518 | + | rc-service promtail start | |
| 519 | + | if rc-service promtail status >/dev/null 2>&1; then | |
| 520 | + | log_success "Promtail service is running" | |
| 521 | + | else | |
| 522 | + | log_error "Failed to start Promtail service" | |
| 523 | + | exit 1 | |
| 524 | + | fi | |
| 525 | + | ;; | |
| 526 | + | esac | |
| 265 | 527 | } | |
| 266 | 528 | ||
| 267 | 529 | # Main installation process | |
| @@ -272,24 +534,33 @@ main() { | |||
| 272 | 534 | echo | |
| 273 | 535 | ||
| 274 | 536 | check_root | |
| 537 | + | detect_os | |
| 275 | 538 | ||
| 276 | 539 | if check_promtail_installed; then | |
| 277 | 540 | log_warning "Promtail appears to be already installed" | |
| 278 | - | echo "Existing installation found. Do you want to continue and reconfigure? (y/N)" | |
| 279 | - | read -r response | |
| 280 | - | if [[ ! "$response" =~ ^[Yy]$ ]]; then | |
| 281 | - | log_info "Installation cancelled" | |
| 282 | - | exit 0 | |
| 541 | + | if [[ -t 0 ]]; then | |
| 542 | + | echo "Existing installation found. Do you want to continue and reconfigure? (y/N)" | |
| 543 | + | read -r response | |
| 544 | + | if [[ ! "$response" =~ ^[Yy]$ ]]; then | |
| 545 | + | log_info "Installation cancelled" | |
| 546 | + | exit 0 | |
| 547 | + | fi | |
| 548 | + | else | |
| 549 | + | log_info "Non-interactive mode: Reconfiguring existing installation" | |
| 283 | 550 | fi | |
| 284 | 551 | fi | |
| 285 | 552 | ||
| 286 | 553 | # Test Loki connectivity first | |
| 287 | 554 | if ! test_loki_connectivity; then | |
| 288 | - | echo "Do you want to continue anyway? (y/N)" | |
| 289 | - | read -r response | |
| 290 | - | if [[ ! "$response" =~ ^[Yy]$ ]]; then | |
| 291 | - | log_info "Installation cancelled" | |
| 292 | - | exit 1 | |
| 555 | + | if [[ -t 0 ]]; then | |
| 556 | + | echo "Do you want to continue anyway? (y/N)" | |
| 557 | + | read -r response | |
| 558 | + | if [[ ! "$response" =~ ^[Yy]$ ]]; then | |
| 559 | + | log_info "Installation cancelled" | |
| 560 | + | exit 1 | |
| 561 | + | fi | |
| 562 | + | else | |
| 563 | + | log_warning "Non-interactive mode: Continuing despite connectivity issues" | |
| 293 | 564 | fi | |
| 294 | 565 | fi | |
| 295 | 566 | ||
| @@ -303,7 +574,7 @@ main() { | |||
| 303 | 574 | fi | |
| 304 | 575 | ||
| 305 | 576 | create_config | |
| 306 | - | create_systemd_service | |
| 577 | + | create_service | |
| 307 | 578 | configure_log_access | |
| 308 | 579 | start_service | |
| 309 | 580 | ||
admin zrewidował ten Gist . Przejdź do rewizji
1 file changed, 25 insertions, 66 deletions
install-promtail.sh
| @@ -140,6 +140,9 @@ install_promtail() { | |||
| 140 | 140 | create_config() { | |
| 141 | 141 | log_info "Creating Promtail configuration..." | |
| 142 | 142 | ||
| 143 | + | # Get the actual hostname | |
| 144 | + | HOSTNAME=$(hostname) | |
| 145 | + | ||
| 143 | 146 | cat > $CONFIG_DIR/promtail.yml << EOF | |
| 144 | 147 | server: | |
| 145 | 148 | http_listen_port: 9080 | |
| @@ -152,90 +155,46 @@ clients: | |||
| 152 | 155 | - url: http://$LOKI_ENDPOINT:$LOKI_PORT/loki/api/v1/push | |
| 153 | 156 | ||
| 154 | 157 | scrape_configs: | |
| 155 | - | # System logs | |
| 158 | + | # Direct /var/log/ files (system logs) | |
| 156 | 159 | - job_name: system-logs | |
| 157 | 160 | static_configs: | |
| 158 | 161 | - targets: | |
| 159 | 162 | - localhost | |
| 160 | 163 | labels: | |
| 161 | 164 | job: system-logs | |
| 162 | - | host: \$(hostname) | |
| 165 | + | service: system | |
| 166 | + | host: $HOSTNAME | |
| 163 | 167 | __path__: /var/log/*.log | |
| 164 | 168 | ||
| 165 | - | # Syslog | |
| 166 | - | - job_name: syslog | |
| 167 | - | static_configs: | |
| 168 | - | - targets: | |
| 169 | - | - localhost | |
| 170 | - | labels: | |
| 171 | - | job: syslog | |
| 172 | - | host: \$(hostname) | |
| 173 | - | __path__: /var/log/syslog | |
| 174 | - | ||
| 175 | - | # Auth logs | |
| 176 | - | - job_name: auth-logs | |
| 177 | - | static_configs: | |
| 178 | - | - targets: | |
| 179 | - | - localhost | |
| 180 | - | labels: | |
| 181 | - | job: auth-logs | |
| 182 | - | host: \$(hostname) | |
| 183 | - | __path__: /var/log/auth.log | |
| 184 | - | ||
| 185 | - | # Kernel logs | |
| 186 | - | - job_name: kernel-logs | |
| 169 | + | # Service-specific logs in subdirectories | |
| 170 | + | - job_name: service-logs | |
| 187 | 171 | static_configs: | |
| 188 | 172 | - targets: | |
| 189 | 173 | - localhost | |
| 190 | 174 | labels: | |
| 191 | - | job: kernel-logs | |
| 192 | - | host: \$(hostname) | |
| 193 | - | __path__: /var/log/kern.log | |
| 194 | - | ||
| 195 | - | # Apache logs (if exists) | |
| 196 | - | - job_name: apache-access | |
| 197 | - | static_configs: | |
| 198 | - | - targets: | |
| 199 | - | - localhost | |
| 200 | - | labels: | |
| 201 | - | job: apache-access | |
| 202 | - | host: \$(hostname) | |
| 203 | - | __path__: /var/log/apache2/access.log | |
| 175 | + | job: service-logs | |
| 176 | + | host: $HOSTNAME | |
| 177 | + | __path__: /var/log/*/*.log | |
| 204 | 178 | pipeline_stages: | |
| 205 | - | - match: | |
| 206 | - | selector: '{job="apache-access"}' | |
| 207 | - | stages: | |
| 208 | - | - regex: | |
| 209 | - | expression: '^(?P<remote_addr>\S+) \S+ \S+ \[(?P<time_local>[^\]]+)\] "(?P<method>\S+) (?P<request>\S+) \S+" (?P<status>\d+) (?P<body_bytes_sent>\d+)' | |
| 210 | - | ||
| 211 | - | # Apache error logs (if exists) | |
| 212 | - | - job_name: apache-error | |
| 213 | - | static_configs: | |
| 214 | - | - targets: | |
| 215 | - | - localhost | |
| 216 | - | labels: | |
| 217 | - | job: apache-error | |
| 218 | - | host: \$(hostname) | |
| 219 | - | __path__: /var/log/apache2/error.log | |
| 179 | + | - regex: | |
| 180 | + | expression: '/var/log/(?P<service>[^/]+)/.*' | |
| 181 | + | - labels: | |
| 182 | + | service: '{{ .service }}' | |
| 220 | 183 | ||
| 221 | - | # Nginx logs (if exists) | |
| 222 | - | - job_name: nginx-access | |
| 184 | + | # Recursively capture all nested logs (deeper than one level) | |
| 185 | + | - job_name: nested-service-logs | |
| 223 | 186 | static_configs: | |
| 224 | 187 | - targets: | |
| 225 | 188 | - localhost | |
| 226 | 189 | labels: | |
| 227 | - | job: nginx-access | |
| 228 | - | host: \$(hostname) | |
| 229 | - | __path__: /var/log/nginx/access.log | |
| 230 | - | ||
| 231 | - | - job_name: nginx-error | |
| 232 | - | static_configs: | |
| 233 | - | - targets: | |
| 234 | - | - localhost | |
| 235 | - | labels: | |
| 236 | - | job: nginx-error | |
| 237 | - | host: \$(hostname) | |
| 238 | - | __path__: /var/log/nginx/error.log | |
| 190 | + | job: nested-service-logs | |
| 191 | + | host: $HOSTNAME | |
| 192 | + | __path__: /var/log/**/*.log | |
| 193 | + | pipeline_stages: | |
| 194 | + | - regex: | |
| 195 | + | expression: '/var/log/(?P<service>[^/]+)/.*' | |
| 196 | + | - labels: | |
| 197 | + | service: '{{ .service }}' | |
| 239 | 198 | EOF | |
| 240 | 199 | ||
| 241 | 200 | # Create positions directory | |
admin zrewidował ten Gist . Przejdź do rewizji
1 file changed, 366 insertions
install-promtail.sh(stworzono plik)
| @@ -0,0 +1,366 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | ||
| 3 | + | # Promtail Installation and Configuration Script for Debian | |
| 4 | + | # Usage: bash -c "$(curl -fsSL <your-gist-url>/install-promtail.sh)" | |
| 5 | + | ||
| 6 | + | set -e | |
| 7 | + | ||
| 8 | + | # Configuration | |
| 9 | + | LOKI_ENDPOINT="loki.pfotenballen.de" | |
| 10 | + | LOKI_PORT="3100" | |
| 11 | + | PROMTAIL_VERSION="2.9.2" | |
| 12 | + | PROMTAIL_USER="promtail" | |
| 13 | + | PROMTAIL_DIR="/opt/promtail" | |
| 14 | + | CONFIG_DIR="/etc/promtail" | |
| 15 | + | LOG_DIR="/var/log/promtail" | |
| 16 | + | ||
| 17 | + | # Colors for output | |
| 18 | + | RED='\033[0;31m' | |
| 19 | + | GREEN='\033[0;32m' | |
| 20 | + | YELLOW='\033[1;33m' | |
| 21 | + | BLUE='\033[0;34m' | |
| 22 | + | NC='\033[0m' # No Color | |
| 23 | + | ||
| 24 | + | # Logging functions | |
| 25 | + | log_info() { | |
| 26 | + | echo -e "${BLUE}[INFO]${NC} $1" | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | log_success() { | |
| 30 | + | echo -e "${GREEN}[SUCCESS]${NC} $1" | |
| 31 | + | } | |
| 32 | + | ||
| 33 | + | log_warning() { | |
| 34 | + | echo -e "${YELLOW}[WARNING]${NC} $1" | |
| 35 | + | } | |
| 36 | + | ||
| 37 | + | log_error() { | |
| 38 | + | echo -e "${RED}[ERROR]${NC} $1" | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | # Check if running as root | |
| 42 | + | check_root() { | |
| 43 | + | if [[ $EUID -ne 0 ]]; then | |
| 44 | + | log_error "This script must be run as root" | |
| 45 | + | exit 1 | |
| 46 | + | fi | |
| 47 | + | } | |
| 48 | + | ||
| 49 | + | # Check if promtail is already installed | |
| 50 | + | check_promtail_installed() { | |
| 51 | + | if command -v promtail &> /dev/null || [[ -f "/usr/local/bin/promtail" ]] || [[ -f "$PROMTAIL_DIR/promtail" ]]; then | |
| 52 | + | return 0 | |
| 53 | + | else | |
| 54 | + | return 1 | |
| 55 | + | fi | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | # Test Loki endpoint connectivity | |
| 59 | + | test_loki_connectivity() { | |
| 60 | + | log_info "Testing connectivity to Loki endpoint: $LOKI_ENDPOINT:$LOKI_PORT" | |
| 61 | + | ||
| 62 | + | if timeout 10 bash -c "</dev/tcp/$LOKI_ENDPOINT/$LOKI_PORT" 2>/dev/null; then | |
| 63 | + | log_success "Successfully connected to $LOKI_ENDPOINT:$LOKI_PORT" | |
| 64 | + | return 0 | |
| 65 | + | else | |
| 66 | + | log_error "Cannot reach $LOKI_ENDPOINT:$LOKI_PORT" | |
| 67 | + | log_error "Please check your network connection and Loki server status" | |
| 68 | + | return 1 | |
| 69 | + | fi | |
| 70 | + | } | |
| 71 | + | ||
| 72 | + | # Install dependencies | |
| 73 | + | install_dependencies() { | |
| 74 | + | log_info "Installing dependencies..." | |
| 75 | + | apt-get update -qq | |
| 76 | + | apt-get install -y wget curl unzip systemd | |
| 77 | + | log_success "Dependencies installed" | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | # Create promtail user | |
| 81 | + | create_promtail_user() { | |
| 82 | + | if ! id "$PROMTAIL_USER" &>/dev/null; then | |
| 83 | + | log_info "Creating promtail user..." | |
| 84 | + | useradd --system --no-create-home --shell /bin/false $PROMTAIL_USER | |
| 85 | + | log_success "Promtail user created" | |
| 86 | + | else | |
| 87 | + | log_info "Promtail user already exists" | |
| 88 | + | fi | |
| 89 | + | } | |
| 90 | + | ||
| 91 | + | # Download and install promtail | |
| 92 | + | install_promtail() { | |
| 93 | + | log_info "Downloading Promtail v$PROMTAIL_VERSION..." | |
| 94 | + | ||
| 95 | + | # Determine architecture | |
| 96 | + | ARCH=$(uname -m) | |
| 97 | + | case $ARCH in | |
| 98 | + | x86_64) | |
| 99 | + | ARCH_SUFFIX="amd64" | |
| 100 | + | ;; | |
| 101 | + | aarch64) | |
| 102 | + | ARCH_SUFFIX="arm64" | |
| 103 | + | ;; | |
| 104 | + | armv7l) | |
| 105 | + | ARCH_SUFFIX="armv7" | |
| 106 | + | ;; | |
| 107 | + | *) | |
| 108 | + | log_error "Unsupported architecture: $ARCH" | |
| 109 | + | exit 1 | |
| 110 | + | ;; | |
| 111 | + | esac | |
| 112 | + | ||
| 113 | + | # Create directories | |
| 114 | + | mkdir -p $PROMTAIL_DIR | |
| 115 | + | mkdir -p $CONFIG_DIR | |
| 116 | + | mkdir -p $LOG_DIR | |
| 117 | + | ||
| 118 | + | # Download promtail binary | |
| 119 | + | DOWNLOAD_URL="https://github.com/grafana/loki/releases/download/v$PROMTAIL_VERSION/promtail-linux-$ARCH_SUFFIX.zip" | |
| 120 | + | ||
| 121 | + | cd /tmp | |
| 122 | + | wget -q "$DOWNLOAD_URL" -O promtail.zip | |
| 123 | + | unzip -q promtail.zip | |
| 124 | + | ||
| 125 | + | # Install binary | |
| 126 | + | chmod +x promtail-linux-$ARCH_SUFFIX | |
| 127 | + | mv promtail-linux-$ARCH_SUFFIX /usr/local/bin/promtail | |
| 128 | + | ||
| 129 | + | # Set ownership | |
| 130 | + | chown root:root /usr/local/bin/promtail | |
| 131 | + | chown -R $PROMTAIL_USER:$PROMTAIL_USER $CONFIG_DIR $LOG_DIR | |
| 132 | + | ||
| 133 | + | # Cleanup | |
| 134 | + | rm -f promtail.zip | |
| 135 | + | ||
| 136 | + | log_success "Promtail installed successfully" | |
| 137 | + | } | |
| 138 | + | ||
| 139 | + | # Create promtail configuration | |
| 140 | + | create_config() { | |
| 141 | + | log_info "Creating Promtail configuration..." | |
| 142 | + | ||
| 143 | + | cat > $CONFIG_DIR/promtail.yml << EOF | |
| 144 | + | server: | |
| 145 | + | http_listen_port: 9080 | |
| 146 | + | grpc_listen_port: 0 | |
| 147 | + | ||
| 148 | + | positions: | |
| 149 | + | filename: /var/lib/promtail/positions.yaml | |
| 150 | + | ||
| 151 | + | clients: | |
| 152 | + | - url: http://$LOKI_ENDPOINT:$LOKI_PORT/loki/api/v1/push | |
| 153 | + | ||
| 154 | + | scrape_configs: | |
| 155 | + | # System logs | |
| 156 | + | - job_name: system-logs | |
| 157 | + | static_configs: | |
| 158 | + | - targets: | |
| 159 | + | - localhost | |
| 160 | + | labels: | |
| 161 | + | job: system-logs | |
| 162 | + | host: \$(hostname) | |
| 163 | + | __path__: /var/log/*.log | |
| 164 | + | ||
| 165 | + | # Syslog | |
| 166 | + | - job_name: syslog | |
| 167 | + | static_configs: | |
| 168 | + | - targets: | |
| 169 | + | - localhost | |
| 170 | + | labels: | |
| 171 | + | job: syslog | |
| 172 | + | host: \$(hostname) | |
| 173 | + | __path__: /var/log/syslog | |
| 174 | + | ||
| 175 | + | # Auth logs | |
| 176 | + | - job_name: auth-logs | |
| 177 | + | static_configs: | |
| 178 | + | - targets: | |
| 179 | + | - localhost | |
| 180 | + | labels: | |
| 181 | + | job: auth-logs | |
| 182 | + | host: \$(hostname) | |
| 183 | + | __path__: /var/log/auth.log | |
| 184 | + | ||
| 185 | + | # Kernel logs | |
| 186 | + | - job_name: kernel-logs | |
| 187 | + | static_configs: | |
| 188 | + | - targets: | |
| 189 | + | - localhost | |
| 190 | + | labels: | |
| 191 | + | job: kernel-logs | |
| 192 | + | host: \$(hostname) | |
| 193 | + | __path__: /var/log/kern.log | |
| 194 | + | ||
| 195 | + | # Apache logs (if exists) | |
| 196 | + | - job_name: apache-access | |
| 197 | + | static_configs: | |
| 198 | + | - targets: | |
| 199 | + | - localhost | |
| 200 | + | labels: | |
| 201 | + | job: apache-access | |
| 202 | + | host: \$(hostname) | |
| 203 | + | __path__: /var/log/apache2/access.log | |
| 204 | + | pipeline_stages: | |
| 205 | + | - match: | |
| 206 | + | selector: '{job="apache-access"}' | |
| 207 | + | stages: | |
| 208 | + | - regex: | |
| 209 | + | expression: '^(?P<remote_addr>\S+) \S+ \S+ \[(?P<time_local>[^\]]+)\] "(?P<method>\S+) (?P<request>\S+) \S+" (?P<status>\d+) (?P<body_bytes_sent>\d+)' | |
| 210 | + | ||
| 211 | + | # Apache error logs (if exists) | |
| 212 | + | - job_name: apache-error | |
| 213 | + | static_configs: | |
| 214 | + | - targets: | |
| 215 | + | - localhost | |
| 216 | + | labels: | |
| 217 | + | job: apache-error | |
| 218 | + | host: \$(hostname) | |
| 219 | + | __path__: /var/log/apache2/error.log | |
| 220 | + | ||
| 221 | + | # Nginx logs (if exists) | |
| 222 | + | - job_name: nginx-access | |
| 223 | + | static_configs: | |
| 224 | + | - targets: | |
| 225 | + | - localhost | |
| 226 | + | labels: | |
| 227 | + | job: nginx-access | |
| 228 | + | host: \$(hostname) | |
| 229 | + | __path__: /var/log/nginx/access.log | |
| 230 | + | ||
| 231 | + | - job_name: nginx-error | |
| 232 | + | static_configs: | |
| 233 | + | - targets: | |
| 234 | + | - localhost | |
| 235 | + | labels: | |
| 236 | + | job: nginx-error | |
| 237 | + | host: \$(hostname) | |
| 238 | + | __path__: /var/log/nginx/error.log | |
| 239 | + | EOF | |
| 240 | + | ||
| 241 | + | # Create positions directory | |
| 242 | + | mkdir -p /var/lib/promtail | |
| 243 | + | chown $PROMTAIL_USER:$PROMTAIL_USER /var/lib/promtail | |
| 244 | + | ||
| 245 | + | # Set proper permissions | |
| 246 | + | chown $PROMTAIL_USER:$PROMTAIL_USER $CONFIG_DIR/promtail.yml | |
| 247 | + | chmod 640 $CONFIG_DIR/promtail.yml | |
| 248 | + | ||
| 249 | + | log_success "Configuration created" | |
| 250 | + | } | |
| 251 | + | ||
| 252 | + | # Create systemd service | |
| 253 | + | create_systemd_service() { | |
| 254 | + | log_info "Creating systemd service..." | |
| 255 | + | ||
| 256 | + | cat > /etc/systemd/system/promtail.service << EOF | |
| 257 | + | [Unit] | |
| 258 | + | Description=Promtail service | |
| 259 | + | Documentation=https://grafana.com/docs/loki/latest/clients/promtail/ | |
| 260 | + | After=network.target | |
| 261 | + | ||
| 262 | + | [Service] | |
| 263 | + | Type=simple | |
| 264 | + | User=$PROMTAIL_USER | |
| 265 | + | ExecStart=/usr/local/bin/promtail -config.file=$CONFIG_DIR/promtail.yml | |
| 266 | + | Restart=always | |
| 267 | + | RestartSec=10 | |
| 268 | + | StandardOutput=journal | |
| 269 | + | StandardError=journal | |
| 270 | + | SyslogIdentifier=promtail | |
| 271 | + | ||
| 272 | + | [Install] | |
| 273 | + | WantedBy=multi-user.target | |
| 274 | + | EOF | |
| 275 | + | ||
| 276 | + | systemctl daemon-reload | |
| 277 | + | log_success "Systemd service created" | |
| 278 | + | } | |
| 279 | + | ||
| 280 | + | # Add promtail user to adm group for log access | |
| 281 | + | configure_log_access() { | |
| 282 | + | log_info "Configuring log file access..." | |
| 283 | + | usermod -a -G adm $PROMTAIL_USER | |
| 284 | + | log_success "Log access configured" | |
| 285 | + | } | |
| 286 | + | ||
| 287 | + | # Start and enable service | |
| 288 | + | start_service() { | |
| 289 | + | log_info "Starting Promtail service..." | |
| 290 | + | ||
| 291 | + | systemctl enable promtail | |
| 292 | + | systemctl start promtail | |
| 293 | + | ||
| 294 | + | # Wait a moment and check status | |
| 295 | + | sleep 2 | |
| 296 | + | ||
| 297 | + | if systemctl is-active --quiet promtail; then | |
| 298 | + | log_success "Promtail service is running" | |
| 299 | + | log_info "Service status:" | |
| 300 | + | systemctl status promtail --no-pager -l | |
| 301 | + | else | |
| 302 | + | log_error "Failed to start Promtail service" | |
| 303 | + | log_error "Check logs with: journalctl -u promtail -f" | |
| 304 | + | exit 1 | |
| 305 | + | fi | |
| 306 | + | } | |
| 307 | + | ||
| 308 | + | # Main installation process | |
| 309 | + | main() { | |
| 310 | + | echo "==================================" | |
| 311 | + | echo " Promtail Installation Script " | |
| 312 | + | echo "==================================" | |
| 313 | + | echo | |
| 314 | + | ||
| 315 | + | check_root | |
| 316 | + | ||
| 317 | + | if check_promtail_installed; then | |
| 318 | + | log_warning "Promtail appears to be already installed" | |
| 319 | + | echo "Existing installation found. Do you want to continue and reconfigure? (y/N)" | |
| 320 | + | read -r response | |
| 321 | + | if [[ ! "$response" =~ ^[Yy]$ ]]; then | |
| 322 | + | log_info "Installation cancelled" | |
| 323 | + | exit 0 | |
| 324 | + | fi | |
| 325 | + | fi | |
| 326 | + | ||
| 327 | + | # Test Loki connectivity first | |
| 328 | + | if ! test_loki_connectivity; then | |
| 329 | + | echo "Do you want to continue anyway? (y/N)" | |
| 330 | + | read -r response | |
| 331 | + | if [[ ! "$response" =~ ^[Yy]$ ]]; then | |
| 332 | + | log_info "Installation cancelled" | |
| 333 | + | exit 1 | |
| 334 | + | fi | |
| 335 | + | fi | |
| 336 | + | ||
| 337 | + | install_dependencies | |
| 338 | + | create_promtail_user | |
| 339 | + | ||
| 340 | + | if ! check_promtail_installed; then | |
| 341 | + | install_promtail | |
| 342 | + | else | |
| 343 | + | log_info "Promtail binary already exists, skipping download" | |
| 344 | + | fi | |
| 345 | + | ||
| 346 | + | create_config | |
| 347 | + | create_systemd_service | |
| 348 | + | configure_log_access | |
| 349 | + | start_service | |
| 350 | + | ||
| 351 | + | echo | |
| 352 | + | echo "==================================" | |
| 353 | + | log_success "Promtail installation completed!" | |
| 354 | + | echo "==================================" | |
| 355 | + | echo | |
| 356 | + | echo "Configuration file: $CONFIG_DIR/promtail.yml" | |
| 357 | + | echo "Service status: systemctl status promtail" | |
| 358 | + | echo "Service logs: journalctl -u promtail -f" | |
| 359 | + | echo "Loki endpoint: http://$LOKI_ENDPOINT:$LOKI_PORT" | |
| 360 | + | echo | |
| 361 | + | echo "To check if logs are being sent to Loki:" | |
| 362 | + | echo "curl -G -s \"http://$LOKI_ENDPOINT:$LOKI_PORT/loki/api/v1/query\" --data-urlencode 'query={job=\"system-logs\"}'" | |
| 363 | + | } | |
| 364 | + | ||
| 365 | + | # Execute main function | |
| 366 | + | main "$@" | |