|
1 | | -#!/bin/sh |
| 1 | +#!/bin/bash |
2 | 2 | set -e |
3 | | -set -o xtrace |
4 | 3 |
|
5 | | -export PATH="$PATH":/opt/fluent-bit/bin |
| 4 | +export PATH="$PATH:/opt/fluent-bit/bin" |
6 | 5 |
|
7 | | -if [ "$1" = 'logrotate' ]; then |
| 6 | +LOGROTATE_SCHEDULE="${LOGROTATE_SCHEDULE:-0 0 * * *}" |
| 7 | + |
| 8 | +is_logrotate_config_invalid() { |
| 9 | + local config_file="$1" |
| 10 | + if [ -z "$config_file" ] || [ ! -f "$config_file" ]; then |
| 11 | + return 1 |
| 12 | + fi |
| 13 | + # Specifying -d runs in debug mode, so even in case of errors, it will exit with 0. |
| 14 | + # We need to check the output for "error" but skip those lines that are related to the missing logrotate.status file. |
| 15 | + # Filter out logrotate.status lines first, then check for remaining errors |
| 16 | + ( |
| 17 | + set +e |
| 18 | + logrotate -d "$config_file" 2>&1 | grep -v "logrotate.status" | grep -qi "error" |
| 19 | + ) |
| 20 | + return $? |
| 21 | +} |
| 22 | + |
| 23 | +run_logrotate() { |
| 24 | + local logrotate_status_file="/data/db/logs/logrotate.status" |
| 25 | + local logrotate_conf_file="/opt/percona/logcollector/logrotate/logrotate.conf" |
| 26 | + local logrotate_additional_conf_files=() |
| 27 | + local conf_d_dir="/opt/percona/logcollector/logrotate/conf.d" |
| 28 | + |
| 29 | + # Check if mongodb.conf exists and validate it |
| 30 | + if [ -f "$conf_d_dir/mongodb.conf" ]; then |
| 31 | + logrotate_conf_file="$conf_d_dir/mongodb.conf" |
| 32 | + if is_logrotate_config_invalid "$logrotate_conf_file"; then |
| 33 | + echo "ERROR: Logrotate configuration is invalid, fallback to default configuration" |
| 34 | + logrotate_conf_file="/opt/percona/logcollector/logrotate/logrotate.conf" |
| 35 | + fi |
| 36 | + fi |
| 37 | + |
| 38 | + # Process all .conf files in conf.d directory (excluding mongodb.conf which is already handled) |
| 39 | + if [ -d "$conf_d_dir" ]; then |
| 40 | + for conf_file in "$conf_d_dir"/*.conf; do |
| 41 | + # Check if glob matched any files (if no .conf files exist, the glob returns itself) |
| 42 | + [ -f "$conf_file" ] || continue |
| 43 | + # Skip mongodb.conf as it's already processed above |
| 44 | + [ "$(basename "$conf_file")" = "mongodb.conf" ] && continue |
| 45 | + if is_logrotate_config_invalid "$conf_file"; then |
| 46 | + echo "ERROR: Logrotate configuration file $conf_file is invalid, it will be ignored" |
| 47 | + else |
| 48 | + logrotate_additional_conf_files+=("$conf_file") |
| 49 | + fi |
| 50 | + done |
| 51 | + fi |
| 52 | + # Ensure logrotate can run with current UID |
8 | 53 | if [[ $EUID != 1001 ]]; then |
9 | 54 | # logrotate requires UID in /etc/passwd |
10 | 55 | sed -e "s^x:1001:^x:$EUID:^" /etc/passwd >/tmp/passwd |
11 | 56 | cat /tmp/passwd >/etc/passwd |
12 | 57 | rm -rf /tmp/passwd |
13 | 58 | fi |
14 | | - exec go-cron "0 0 * * *" sh -c "logrotate -s /data/db/logs/logrotate.status /opt/percona/logcollector/logrotate/logrotate.conf;" |
15 | | -else |
16 | | - if [ "$1" = 'fluent-bit' ]; then |
17 | | - fluentbit_opt+='-c /opt/percona/logcollector/fluentbit/fluentbit.conf' |
18 | | - fi |
19 | 59 |
|
20 | | - exec "$@" $fluentbit_opt |
21 | | -fi |
| 60 | + local logrotate_cmd="logrotate -s \"$logrotate_status_file\" \"$logrotate_conf_file\"" |
| 61 | + for additional_conf in "${logrotate_additional_conf_files[@]}"; do |
| 62 | + logrotate_cmd="$logrotate_cmd \"$additional_conf\"" |
| 63 | + done |
| 64 | + |
| 65 | + set -o xtrace |
| 66 | + exec go-cron "$LOGROTATE_SCHEDULE" sh -c "$logrotate_cmd" |
| 67 | +} |
| 68 | + |
| 69 | +run_fluentbit() { |
| 70 | + local fluentbit_opt=(-c /opt/percona/logcollector/fluentbit/fluentbit.conf) |
| 71 | + mkdir -p /tmp/fluentbit/custom |
| 72 | + set +e |
| 73 | + local fluentbit_conf_dir="/opt/percona/logcollector/fluentbit/custom" |
| 74 | + for conf_file in $fluentbit_conf_dir/*.conf; do |
| 75 | + [ -f "$conf_file" ] || continue |
| 76 | + if ! fluent-bit --dry-run -c "$conf_file" >/dev/null 2>&1; then |
| 77 | + echo "ERROR: Fluentbit configuration file $conf_file is invalid, it will be ignored" |
| 78 | + else |
| 79 | + cp "$conf_file" /tmp/fluentbit/custom/ |
| 80 | + fi |
| 81 | + done |
| 82 | + touch /tmp/fluentbit/custom/default.conf || true |
| 83 | + |
| 84 | + set -e |
| 85 | + set -o xtrace |
| 86 | + exec "$@" "${fluentbit_opt[@]}" |
| 87 | +} |
| 88 | + |
| 89 | +case "$1" in |
| 90 | + logrotate) |
| 91 | + run_logrotate |
| 92 | + ;; |
| 93 | + fluent-bit) |
| 94 | + run_fluentbit "$@" |
| 95 | + ;; |
| 96 | + *) |
| 97 | + echo "Invalid argument: $1" |
| 98 | + exit 1 |
| 99 | + ;; |
| 100 | +esac |
0 commit comments