-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log retention #370
Open
hdbdn77
wants to merge
1
commit into
opencurve:develop
Choose a base branch
from
hdbdn77:log-retention
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Log retention #370
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是否每个curve节点都需要执行这个脚本才能完成日志滚动操作?
具体执行命令是
bash log_rentention.sh
吗?执行后的效果是什么?能否补充一个示例结果?是否能定期执行这个脚本?如何定期执行能否举个例子?
log_rentention.sh是否改为logrotate.sh更合适?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
前三个问题已更改,请查看。
因为有清理和轮转两种功能,所以使用了log_rentention。