-
Notifications
You must be signed in to change notification settings - Fork 0
/
cce-log-export.sh
executable file
·192 lines (167 loc) · 4.25 KB
/
cce-log-export.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env bash
readonly TARGET_DIR="/tmp/cce-log-exporter"
readonly OPENSTACK_METADATA="http://169.254.169.254/openstack/2015-10-15/meta_data.json"
readonly LOG_DIR="/var/log"
PACKAGES=(
sar
curl
)
# in /var/log
COMMON_LOGS=(
dmesg
cce-agent-install.log
cloud-init.log
kern.log
)
#in /var/log/cce
CCE_LOGS=(
containerd/containerd.log
everest-csi-driver/everest-csi-controller.log
everest-csi-driver/everest-csi-driver.log
kubernetes/kube-proxy.log
kubernetes/kubelet.log
yangtse/yangtse-agent.log
yangtse/yangtse-cni.log
canal/canal-agent.log
)
CONFIG_FILES=(
/opt/cloud/cce/kubernetes/kube-proxy/kube-proxy
/opt/cloud/cce/kubernetes/kubelet/kubelet_config.yaml
/opt/cloud/cce/kubernetes/kubelet/kubelet
/opt/cloud/cce/kubernetes/cce-agent/agentConfig
/etc/containerd/config.toml
/etc/docker/daemon.json
)
help() {
echo "usage..."
exit 0
}
info() {
>&2 echo -e "\e[32m[INFO]\e[0m $*"
}
warn() {
>&2 echo -e "\e[33m[WARN]\e[0m $*"
}
err() {
>&2 echo -e "\e[31m[ERROR]\e[0m $*"
exit 1
}
check_root() {
if [[ "$(id -u)" -ne 0 ]]; then
err "Script must be running as root!"
fi
}
create_script_dir() {
info "Creating target directory."
mkdir -p ${TARGET_DIR}/config
mkdir -p ${TARGET_DIR}/logs/cce
}
get_openstack_metadata() {
info "Quering openstack metadata."
curl --connect-timeout 2 ${OPENSTACK_METADATA} > ${TARGET_DIR}/meta_data.json 2>/dev/null \
&& info "Openstack metadata saved." || warn "Cannot obtain openstack metadata"
}
get_kernel_info() {
info "Getting kernel version."
uname -a > ${TARGET_DIR}/kernel_version.txt
}
get_mounts() {
info "Getting mount info"
mount > ${TARGET_DIR}/mount.txt
}
get_load() {
info "Getting historical load values."
sar -q > ${TAGET_DIR}/load_values.txt \
&& info "Getting load values success." || warn "Cannot get historycal load values."
}
get_iptables() {
info "Getting iptables tables."
iptables-save > ${TARGET_DIR}/iptables.txt
}
check_disk() {
info "Checking available disk space on root."
local free_disk
threshold=2000000
result=$(df / | grep -v "Filesystem" | awk '{ print $4 }')
if [[ "${result}" -le "${threshold}" ]]; then
err "You need at least 2GB free space on root to run the script!"
fi
}
check_packages() {
for package in ${PACKAGES[*]}; do
if which $package | grep -q $package ; then
info "Package $package is installed."
else
warn "Package $package is not installed."
warn "To get all the export features, please install $package."
fi
done
}
get_common_logs() {
local syslog_lines="100000"
info "Collecting common logs."
if [[ -e /var/log/messages ]]; then
info "Processing messages."
tail -n ${syslog_lines} ${LOG_DIR}/messages > ${TARGET_DIR}/logs/messages
fi
if [[ -e /var/log/syslog ]]; then
info "Processing syslog."
tail -n ${syslog_lines} ${LOG_DIR}/syslog > ${TARGET_DIR}/logs/syslog
fi
for log_file in ${COMMON_LOGS[*]}; do
info "Processing ${log_file}."
if [[ -e "${LOG_DIR}/${log_file}" ]]; then
cp -f ${LOG_DIR}/${log_file}* ${TARGET_DIR}/logs/ 2>/dev/null
else
warn "Cannot find ${log_file}."
fi
done
info "Common logs collected."
}
get_cce_logs() {
info "Collecting CCE logs."
for log_file in ${CCE_LOGS[*]}; do
info "Processing ${log_file}."
if [[ -e "${LOG_DIR}/cce/${log_file}" ]]; then
cp -f ${LOG_DIR}/cce/${log_file} ${TARGET_DIR}/logs/cce/ 2>/dev/null
else
warn "Cannot find ${log_file}."
fi
done
info "CCE logs collected."
}
get_configs() {
info "Collecting config files."
for conf_file in ${CONFIG_FILES[*]}; do
info "Processing ${conf_file}."
if [[ -e ${conf_file} ]]; then
cp -f ${conf_file} ${TARGET_DIR}/config/
else
warn "Cannot find ${conf_file}"
fi
done
info "Configs collected"
}
pack() {
tar --gzip -cvf /tmp/cce-export-${HOSTNAME}.tar.gz --directory ${TARGET_DIR} . >/dev/null 2>&1 \
&& info "Packaging complete. Please see the file at: /tmp/cce-export-${HOSTNAME}.tar.gz" || err "Cannot package directory."
}
init() {
check_root
check_disk
check_packages
}
run() {
create_script_dir
get_kernel_info
get_openstack_metadata
get_load
get_mounts
get_iptables
get_common_logs
get_cce_logs
get_configs
pack
}
init
run