Skip to content

Commit 56cf895

Browse files
authored
Log retention
Signed-off-by: Liao PengFei <[email protected]>
1 parent 9d83a9c commit 56cf895

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

playbook/logs/scripts/config.json

Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
# Generate logrotate configuration
79+
logrotate_config="${file} {\n"
80+
logrotate_config+=" rotate ${rotate}\n"
81+
logrotate_config+=" ${period}\n"
82+
83+
if [ "$compress" == "true" ]; then
84+
logrotate_config+=" compress\n"
85+
fi
86+
87+
if [ "$missingok" == "true" ]; then
88+
logrotate_config+=" missingok\n"
89+
fi
90+
91+
if [ "$notifempty" == "true" ]; then
92+
logrotate_config+=" notifempty\n"
93+
fi
94+
95+
if [ "$dateext" == "true" ]; then
96+
logrotate_config+=" dateext\n"
97+
fi
98+
99+
if [ "$create" != "" ]; then
100+
logrotate_config+=" create ${create}\n"
101+
fi
102+
103+
if [ "$size" != "" ]; then
104+
logrotate_config+=" size ${size}\n"
105+
fi
106+
107+
logrotate_config+="}\n"
108+
109+
# Write logrotate configuration to file
110+
echo -e "$logrotate_config" > "/etc/logrotate.d/$dir_path_name"
111+
112+
logrotate -d /etc/logrotate.conf 2>&1 | grep -i 'error:'
113+
if [ $? -eq 0 ]; then
114+
die "Logrotate configuration file error.\n"
115+
else
116+
success "Logrotate configuration generated and written to '$dir_path_name' file.\n"
117+
fi
118+
done
119+
120+
logrotate /etc/logrotate.conf
121+
if [ $? -eq 0 ]; then
122+
success "Logrotate started successfully.\n"
123+
fi
124+
}
125+
126+
main() {
127+
pre_check
128+
load_config
129+
retention_log
130+
}
131+
132+
############################ MAIN()
133+
main
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
log_retention.sh文件使用说明:
2+
3+
1. 执行该文件之前,需要使用'chmod +x'命令给该文件添加执行权限。
4+
2. 该程序须自定义参数来执行自定义日志保留计划,可以在config.json定义相关参数。
5+
3. 相关参数设定可使用 'man logrotate'查询
6+
7+
提示:此命令须在工作节点上以root权限执行,用于压缩或删除curve工作日志。
8+
#######################################################################
9+
Instructions for using log_retention.sh file:
10+
11+
1. Before executing the file, you need to use the 'chmod +x' command to add execution permissions to the file.
12+
2. The program requires customized parameters to execute a customized log retention plan. Relevant parameters can be defined in config.json.
13+
3. Relevant parameter settings can be queried using 'man logrotate'
14+
15+
Tip: This command must be executed with root privileges on the working node to compress or delete the curve working log.

0 commit comments

Comments
 (0)