-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Liao PengFei <[email protected]>
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#!/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 | ||
|
||
if [ -e "/etc/logrotate.d/curve-$dir_path_name" ];then | ||
continue | ||
fi | ||
|
||
# Generate logrotate configuration | ||
logrotate_config="${dir_path}/*.log*[!gz] ${dir_path}/*.log {\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/curve-$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 '/etc/logrotate.d/curve-$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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
log_retention.sh文件使用说明: | ||
|
||
1. 执行该文件之前,需要使用'chmod +x'命令给该文件添加执行权限。 | ||
2. 该程序须自定义参数来执行自定义日志保留计划,可以在config.json定义相关参数。 | ||
3. 该程序有清理日志,与日志轮转两种模式,可以通过cnofig文件的clean参数来选择。 | ||
4. 其余日志轮转相关参数设定可使用 'man logrotate'查询。 | ||
5. 清理日志模式,是一次性操作,会删除所有日志压缩文件。 | ||
成功执行的结果为:Clean all .gz files in $folder。 | ||
6. 日志轮转模式,是logrotate自动执行的周期性操作,会根据设定的参数,将日志文件进行压缩。 | ||
成功执行的结果为:Logrotate started successfully. | ||
|
||
|
||
提示:此命令须在每个工作节点上以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. This program has two modes: log cleaning and log rotation, which can be selected through the clean parameter of the cnofig file. | ||
4. The remaining log rotation related parameter settings can be queried using 'man logrotate'. | ||
5. Clean log mode is a one-time operation and will delete all compressed log files. | ||
The result of successful execution is: Clean all .gz files in $folder. | ||
6. Log rotation mode is a periodic operation automatically performed by logrotate. It will compress log files according to the set parameters. | ||
The result of successful execution is: Logrotate started successfully. | ||
|
||
|
||
Tip: This command must be executed with root privileges on each working node to compress or delete the curve working log. |