From 56cf895f9c544d53f2d57ba5f34583d4a28181c5 Mon Sep 17 00:00:00 2001 From: Liao PengFei <136953902+hdbdn77@users.noreply.github.com> Date: Tue, 28 Nov 2023 01:06:25 +0800 Subject: [PATCH] Log retention Signed-off-by: Liao PengFei <136953902+hdbdn77@users.noreply.github.com> --- playbook/logs/scripts/config.json | 12 +++ playbook/logs/scripts/log_retention.sh | 133 ++++++++++++++++++++++++ playbook/logs/scripts/log_retention.txt | 15 +++ 3 files changed, 160 insertions(+) create mode 100644 playbook/logs/scripts/config.json create mode 100644 playbook/logs/scripts/log_retention.sh create mode 100644 playbook/logs/scripts/log_retention.txt diff --git a/playbook/logs/scripts/config.json b/playbook/logs/scripts/config.json new file mode 100644 index 000000000..a919e9579 --- /dev/null +++ b/playbook/logs/scripts/config.json @@ -0,0 +1,12 @@ +{ + "log_folder_path": "/tmp/curvefs/logs", + "clean": false, + "rotate": 7, + "period": "daily", + "compress": true, + "missingok": true, + "notifempty": true, + "dateext":true, + "create": "0644 root root", + "size": "" +} \ No newline at end of file diff --git a/playbook/logs/scripts/log_retention.sh b/playbook/logs/scripts/log_retention.sh new file mode 100644 index 000000000..fd5fb7b00 --- /dev/null +++ b/playbook/logs/scripts/log_retention.sh @@ -0,0 +1,133 @@ +#!/bin/bash + +############################ GLOBAL VARIABLES +g_color_yellow=$(printf '\033[33m') +g_color_red=$(printf '\033[31m') +g_color_normal=$(printf '\033[0m') + +############################ BASIC FUNCTIONS +msg() { + printf '%b' "${1}" >&2 +} + +success() { + msg "${g_color_yellow}[✔]${g_color_normal} ${1}" +} + +die() { + msg "${g_color_red}[✘]${g_color_normal} ${1}" + exit 1 +} +############################ FUNCTIONS +pre_check() { + if [ -z "$(which logrotate)" ]; then + die "logrotate could not be found" + fi + + if [ -z "$(which jq)" ]; then + die "jq could not be found" + fi +} + +load_config() { + # Specify JSON file path + json_file="config.json" + + # Check if the file exists + if [ ! -f "$json_file" ]; then + die "json file ${json_file} does not exist.\n" + fi + + log_folder_path=$(jq -r '.log_folder_path' "$json_file") + clean=$(jq -r '.clean' "$json_file") + rotate=$(jq -r '.rotate' "$json_file") + period=$(jq -r '.period' "$json_file") + compress=$(jq -r '.compress' "$json_file") + missingok=$(jq -r '.missingok' "$json_file") + dateext=$(jq -r '.dateext' "$json_file") + notifempty=$(jq -r '.notifempty' "$json_file") + create=$(jq -r '.create' "$json_file") + size=$(jq -r '.size' "$json_file") +} + +delete_all_gz_files() { + local folder="$1" + find "$folder" -name "*.gz" -type f -exec rm {} \; + if [ $? -eq 0 ]; then + success "Clean all .gz files in $folder\n" + fi +} + +retention_log() { + match_string=".log" + find_files=$(find "$log_folder_path" -type f -name "*$match_string*" -not -name "*.gz") + + if [ -z "$find_files" ]; then + die "Log file not found, please check whether the log file path is correct.\n" + fi + + for file in $find_files; do + dir_path=$(dirname -- "$file") + dir_path_name=$(basename -- "$dir_path") + + if [ "$clean" == "true" ]; then + delete_all_gz_files "$dir_path" + continue + fi + + # Generate logrotate configuration + logrotate_config="${file} {\n" + logrotate_config+=" rotate ${rotate}\n" + logrotate_config+=" ${period}\n" + + if [ "$compress" == "true" ]; then + logrotate_config+=" compress\n" + fi + + if [ "$missingok" == "true" ]; then + logrotate_config+=" missingok\n" + fi + + if [ "$notifempty" == "true" ]; then + logrotate_config+=" notifempty\n" + fi + + if [ "$dateext" == "true" ]; then + logrotate_config+=" dateext\n" + fi + + if [ "$create" != "" ]; then + logrotate_config+=" create ${create}\n" + fi + + if [ "$size" != "" ]; then + logrotate_config+=" size ${size}\n" + fi + + logrotate_config+="}\n" + + # Write logrotate configuration to file + echo -e "$logrotate_config" > "/etc/logrotate.d/$dir_path_name" + + logrotate -d /etc/logrotate.conf 2>&1 | grep -i 'error:' + if [ $? -eq 0 ]; then + die "Logrotate configuration file error.\n" + else + success "Logrotate configuration generated and written to '$dir_path_name' file.\n" + fi + done + + logrotate /etc/logrotate.conf + if [ $? -eq 0 ]; then + success "Logrotate started successfully.\n" + fi +} + +main() { + pre_check + load_config + retention_log +} + +############################ MAIN() +main diff --git a/playbook/logs/scripts/log_retention.txt b/playbook/logs/scripts/log_retention.txt new file mode 100644 index 000000000..b83f65e04 --- /dev/null +++ b/playbook/logs/scripts/log_retention.txt @@ -0,0 +1,15 @@ +log_retention.sh文件使用说明: + +1. 执行该文件之前,需要使用'chmod +x'命令给该文件添加执行权限。 +2. 该程序须自定义参数来执行自定义日志保留计划,可以在config.json定义相关参数。 +3. 相关参数设定可使用 'man logrotate'查询 + +提示:此命令须在工作节点上以root权限执行,用于压缩或删除curve工作日志。 +####################################################################### +Instructions for using log_retention.sh file: + +1. Before executing the file, you need to use the 'chmod +x' command to add execution permissions to the file. +2. The program requires customized parameters to execute a customized log retention plan. Relevant parameters can be defined in config.json. +3. Relevant parameter settings can be queried using 'man logrotate' + +Tip: This command must be executed with root privileges on the working node to compress or delete the curve working log. \ No newline at end of file