Skip to content

Commit 327899a

Browse files
committed
Log retention
Signed-off-by: Liao PengFei <[email protected]>
1 parent 9d83a9c commit 327899a

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

playbook/logs/scripts/config.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"log_folder_path": "/tmp/curvefs/logs",
3+
"clean": false,
4+
"rotate": 7,
5+
"period": "daily",
6+
"compress": true,
7+
"missingok": true,
8+
"notifempty": true,
9+
"dateext":true,
10+
"create": "0644 root root",
11+
"size": ""
12+
}
+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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 '/etc/logrotate.d/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
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
log_retention.sh文件使用说明:
2+
3+
1. 执行该文件之前,需要使用'chmod +x'命令给该文件添加执行权限。
4+
2. 该程序须自定义参数来执行自定义日志保留计划,可以在config.json定义相关参数。
5+
3. 该程序有清理日志,与日志轮转两种模式,可以通过cnofig文件的clean参数来选择。
6+
4. 其余日志轮转相关参数设定可使用 'man logrotate'查询。
7+
5. 清理日志模式,是一次性操作,会删除所有日志压缩文件。
8+
成功执行的结果为:Clean all .gz files in $folder。
9+
6. 日志轮转模式,是logrotate自动执行的周期性操作,会根据设定的参数,将日志文件进行压缩。
10+
成功执行的结果为:Logrotate started successfully.
11+
12+
13+
提示:此命令须在每个工作节点上以root权限执行,用于压缩或删除curve工作日志。
14+
#######################################################################
15+
Instructions for using log_retention.sh file:
16+
17+
1. Before executing the file, you need to use the 'chmod +x' command to add execution permissions to the file.
18+
2. The program requires customized parameters to execute a customized log retention plan. Relevant parameters can be defined in config.json.
19+
3. This program has two modes: log cleaning and log rotation, which can be selected through the clean parameter of the cnofig file.
20+
4. The remaining log rotation related parameter settings can be queried using 'man logrotate'.
21+
5. Clean log mode is a one-time operation and will delete all compressed log files.
22+
The result of successful execution is: Clean all .gz files in $folder.
23+
6. Log rotation mode is a periodic operation automatically performed by logrotate. It will compress log files according to the set parameters.
24+
The result of successful execution is: Logrotate started successfully.
25+
26+
27+
Tip: This command must be executed with root privileges on each working node to compress or delete the curve working log.

0 commit comments

Comments
 (0)