|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +############################ GLOBAL VARIABLES |
| 4 | +g_color_yellow=$(printf '\033[33m') |
| 5 | +g_color_red=$(printf '\033[31m') |
| 6 | +g_color_normal=$(printf '\033[0m') |
| 7 | + |
| 8 | +############################ BASIC FUNCTIONS |
| 9 | +msg() { |
| 10 | + printf '%b' "${1}" >&2 |
| 11 | +} |
| 12 | + |
| 13 | +success() { |
| 14 | + msg "${g_color_yellow}[✔]${g_color_normal} ${1}" |
| 15 | +} |
| 16 | + |
| 17 | +die() { |
| 18 | + msg "${g_color_red}[✘]${g_color_normal} ${1}" |
| 19 | + exit 1 |
| 20 | +} |
| 21 | +############################ FUNCTIONS |
| 22 | +pre_check() { |
| 23 | + if [ -z "$(which logrotate)" ]; then |
| 24 | + die "logrotate could not be found" |
| 25 | + fi |
| 26 | + |
| 27 | + if [ -z "$(which jq)" ]; then |
| 28 | + die "jq could not be found" |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +load_config() { |
| 33 | + # Specify JSON file path |
| 34 | + json_file="config.json" |
| 35 | + |
| 36 | + # Check if the file exists |
| 37 | + if [ ! -f "$json_file" ]; then |
| 38 | + die "json file ${json_file} does not exist.\n" |
| 39 | + fi |
| 40 | + |
| 41 | + log_folder_path=$(jq -r '.log_folder_path' "$json_file") |
| 42 | + clean=$(jq -r '.clean' "$json_file") |
| 43 | + rotate=$(jq -r '.rotate' "$json_file") |
| 44 | + period=$(jq -r '.period' "$json_file") |
| 45 | + compress=$(jq -r '.compress' "$json_file") |
| 46 | + missingok=$(jq -r '.missingok' "$json_file") |
| 47 | + dateext=$(jq -r '.dateext' "$json_file") |
| 48 | + notifempty=$(jq -r '.notifempty' "$json_file") |
| 49 | + create=$(jq -r '.create' "$json_file") |
| 50 | + size=$(jq -r '.size' "$json_file") |
| 51 | +} |
| 52 | + |
| 53 | +delete_all_gz_files() { |
| 54 | + local folder="$1" |
| 55 | + find "$folder" -name "*.gz" -type f -exec rm {} \; |
| 56 | + if [ $? -eq 0 ]; then |
| 57 | + success "Clean all .gz files in $folder\n" |
| 58 | + fi |
| 59 | +} |
| 60 | + |
| 61 | +retention_log() { |
| 62 | + match_string=".log" |
| 63 | + find_files=$(find "$log_folder_path" -type f -name "*$match_string*" -not -name "*.gz") |
| 64 | + |
| 65 | + if [ -z "$find_files" ]; then |
| 66 | + die "Log file not found, please check whether the log file path is correct.\n" |
| 67 | + fi |
| 68 | + |
| 69 | + for file in $find_files; do |
| 70 | + dir_path=$(dirname -- "$file") |
| 71 | + dir_path_name=$(basename -- "$dir_path") |
| 72 | + |
| 73 | + if [ "$clean" == "true" ]; then |
| 74 | + delete_all_gz_files "$dir_path" |
| 75 | + continue |
| 76 | + fi |
| 77 | + |
| 78 | + if [ -e "/etc/logrotate.d/curve-$dir_path_name" ];then |
| 79 | + continue |
| 80 | + fi |
| 81 | + |
| 82 | + # Generate logrotate configuration |
| 83 | + logrotate_config="${dir_path}/*.log*[!gz] ${dir_path}/*.log {\n" |
| 84 | + logrotate_config+=" rotate ${rotate}\n" |
| 85 | + logrotate_config+=" ${period}\n" |
| 86 | + |
| 87 | + if [ "$compress" == "true" ]; then |
| 88 | + logrotate_config+=" compress\n" |
| 89 | + fi |
| 90 | + |
| 91 | + if [ "$missingok" == "true" ]; then |
| 92 | + logrotate_config+=" missingok\n" |
| 93 | + fi |
| 94 | + |
| 95 | + if [ "$notifempty" == "true" ]; then |
| 96 | + logrotate_config+=" notifempty\n" |
| 97 | + fi |
| 98 | + |
| 99 | + if [ "$dateext" == "true" ]; then |
| 100 | + logrotate_config+=" dateext\n" |
| 101 | + fi |
| 102 | + |
| 103 | + if [ "$create" != "" ]; then |
| 104 | + logrotate_config+=" create ${create}\n" |
| 105 | + fi |
| 106 | + |
| 107 | + if [ "$size" != "" ]; then |
| 108 | + logrotate_config+=" size ${size}\n" |
| 109 | + fi |
| 110 | + |
| 111 | + logrotate_config+="}\n" |
| 112 | + |
| 113 | + # Write logrotate configuration to file |
| 114 | + echo -e "$logrotate_config" > "/etc/logrotate.d/curve-$dir_path_name" |
| 115 | + |
| 116 | + logrotate -d /etc/logrotate.conf 2>&1 | grep -i 'error:' |
| 117 | + if [ $? -eq 0 ]; then |
| 118 | + die "Logrotate configuration file error.\n" |
| 119 | + else |
| 120 | + success "Logrotate configuration generated and written to 'curve-$dir_path_name' file.\n" |
| 121 | + fi |
| 122 | + done |
| 123 | + |
| 124 | + logrotate /etc/logrotate.conf |
| 125 | + if [ $? -eq 0 ]; then |
| 126 | + success "Logrotate started successfully.\n" |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +main() { |
| 131 | + pre_check |
| 132 | + load_config |
| 133 | + retention_log |
| 134 | +} |
| 135 | + |
| 136 | +############################ MAIN() |
| 137 | +main |
0 commit comments