Debian Codegraph @

share/_ct-common

raw
1 setterm -cursor off
2
3 stty -echoctl # hide ^C
4
5 # function called by trap
6 ct_on_exit() {
7 setterm -cursor on
8 if [ $# -gt 0 ] && [ $1 -gt 0 ]; then
9 ct_notify "$(basename $0) Finish with errors"
10 fi
11 exit $1
12 }
13
14 trap 'ct_on_exit' EXIT HUP INT TERM
15
16 # Init list of OSDs Down
17 ceph osd tree down |grep "osd\."| awk '{print $1}'|sort -n -o /tmp/OSDs_Down
18
19 CT_SCRIPT_NAME=$0
20 CT_MAX_REMAPPED=64
21 CT_OSD_DOWN=0
22 CT_MAX_TEMP=70
23 CT_HELP_MSG="""
24 -h, --help show this help message and exit
25 -M MAX_REMAPPED, --max_remapped MAX_REMAPPED
26 Increasing PG_NUM waits until there is less 'MAX_REMAPPED' in progress to resume,
27 default: $CT_MAX_REMAPPED
28 -d, --osd_down Suspend operation when an OSD is down
29 -k, --exit_on_critical Exit script if critical event
30 -w WAIT, --wait_for_nobackfill WAIT
31 Wait to start until the cluster has no more backfill
32 -t MAX_TEMP, --max_temp MAX_TEMP
33 Suspend operation if serveur Temp >= MAX_TEMP
34 -S SCHEDULE, --schedule SCHEDULE
35 Schedule, eg : 21-07 for 21h to 07h or 08-16 for 8h to 16h
36 """
37
38
39 CT_HELP_EXAMPLE="-M 100 -d -k -w -t 45 -S 21-07"
40
41 CT_HELP_DESCR="[-m|--CT_MAX_REMAPPED CT_MAX_REMAPPED] [-w|--wait_for_nobackfill WAIT] [-d|osd-down]"
42
43 ct_help_min () {
44 if [ $1 -lt $2 ]
45 then
46 help_msg
47 ct_on_exit 1
48 fi
49 }
50
51 ct_help () {
52 _shift=0
53 while [ $# -ge 1 ]; do
54 ARGS=$#
55 _key="$1"
56 case $_key in
57 -h|--help)
58 help_msg
59 ct_on_exit 0
60 shift 1
61 ;;
62 -M|--max_remapped)
63 CT_MAX_REMAPPED=$2
64 ((_shift = _shift + 2))
65 shift 2
66 ;;
67 -d|--osd_down)
68 CT_OSD_DOWN=1
69 ((_shift = _shift + 1))
70 shift 1
71 ;;
72 -k|--exit_on_critical)
73 CT_EXIT_ON_ERROR=1
74 ((_shift = _shift + 1))
75 shift 1
76 ;;
77 -t|--max_temp)
78 CT_MAX_TEMP=$2
79 ((_shift = _shift + 2))
80 shift 2
81 ;;
82 -w|--wait_for_nobackfill)
83 CT_WAIT=1
84 ((_shift = _shift + 1))
85 shift 1
86 ;;
87 -S|--schedule)
88 CT_SCHEDULE=$2
89 ((_shift = _shift + 2))
90 shift 2
91 ;;
92 esac
93 if [ "$#" == "$ARGS" ]; then
94 return $_shift
95 fi
96 done
97 return $_shift
98 }
99
100 _CT_AVG_FILE=$(mktemp -t ct_avg_file.XXXXX)
101
102 _check_variation_remapped() {
103 # Calculate variation of remapped PGs for the last 60 measures (~10 minutes)
104 _PAST=$(($(date "+%s") - 600))
105 _cpt=0
106 while read -r _TIME _REMAP
107 do
108 if [ "${_TIME}" -lt "${_PAST}" ]; then
109 ((_cpt+=1))
110 else
111 break
112 fi
113 done < $_CT_AVG_FILE
114
115 sed -i "1,${_cpt}d" $_CT_AVG_FILE
116 if [ "$(cat $_CT_AVG_FILE | wc -l)" -lt "30" ]; then
117 return 1
118 fi
119 _OLD_REMAP=$(cat $_CT_AVG_FILE| head 1| awk '{print $1}')
120 while read -r _TIME _REMAP
121 do
122 if ! [ "${_REMAP}" -eq "${_OLD_REMAP}" ]; then
123 return 0
124 fi
125 done < $_CT_AVG_FILE
126 return 1
127 }
128
129 ct_get_current_remapped() {
130 # Get Current Remapped
131 _DATE=$(date "+%s")
132 _REMAP=$(ceph -s -f json | jq '.["osdmap"]["osdmap"]["num_remapped_pgs"]')
133 echo ${_DATE} ${REMAP} >> $_CT_AVG_FILE
134 echo ${_REMAP}
135 }
136
137 _ct_test_osd_down() {
138 # Test if OSD has gone down since script beginning
139 if [ $CT_OSD_DOWN -eq 1 ]; then
140 ceph osd tree down |grep "osd\."| awk '{print $1}'|sort -n -o /tmp/OSDs_Down_now
141 if ! diff -q /tmp/OSDs_Down /tmp/OSDs_Down_now > /dev/null; then
142 echo "OSDs down, operation paused"
143 echo "For resume,"
144 echo "You can reinit OSDs Down list by execute"
145 echo "'ceph osd tree down |grep \"osd\\.\"| awk '{print \$1}'|sort -n -o /tmp/OSDs_Down'"
146 ct_notify "Detected OSD Down"
147 while ! diff -q /tmp/OSDs_Down /tmp/OSDs_Down_now > /dev/null; do
148 sleep 60
149 ceph osd tree down |grep "osd\."| awk '{print $1}'|sort -n -o /tmp/OSDs_Down_now
150 done
151 ct_logger "OSDs OK: Resume"
152 fi
153 fi
154 return
155 }
156
157 _ct_test_temp () {
158 # Test current temperature
159 for HWMON in $(ls /sys/class/hwmon); do
160 if [ -f "/sys/class/hwmon/${HWMON}/temp1_input" ]; then
161 _CT_TEMP=$(($(cat "/sys/class/hwmon/${HWMON}/temp1_input") / 1000))
162 if [[ $_CT_TEMP -ge $CT_MAX_TEMP ]]; then
163 ct_logger "High temperature (${_CT_TEMP} > ${CT_MAX_TEMP}): Pause"
164 while [[ $_CT_TEMP -ge $CT_MAX_TEMP ]]; do
165 sleep 60
166 _CT_TEMP=$(($(cat "/sys/class/hwmon/${HWMON}/temp1_input") / 1000))
167 done
168 ct_logger "Temperature OK: Resume"
169 return
170 fi
171 fi
172 done
173 }
174
175 _ct_test_schedule () {
176 # Test if in the good time range
177 if [ ! -z "$CT_SCHEDULE" ]; then
178 # define Test
179 day(){ [ $_CT_F -ge $_CT_D ]; }
180 sup_begin(){ [ $_CT_T -ge $_CT_D ]; }
181 inf_end(){ [ $_CT_T -lt $_CT_F ]; }
182 test_in_hours() { { day && { sup_begin && inf_end; }; } || { ! day && { sup_begin || inf_end; }; }; }
183 _CT_T=$(date "+%H")
184 _CT_D=$(echo $CT_SCHEDULE | cut -d '-' -f 1)
185 _CT_F=$(echo $CT_SCHEDULE | cut -d '-' -f 2)
186 if ! test_in_hours; then
187 ct_logger "Outside of hours range: Pause"
188 while ! test_in_hours; do
189 sleep 3600
190 _CT_T=$(date "+%H")
191 done
192 ct_logger "Inside of hours range: Resume"
193 fi
194 fi
195 return
196 }
197
198 _ct_test_remapped(){
199 # Wait for Remapped <= MAX_REMAPPED
200 # Param 1: Max remapped
201 _ct_mr=$CT_MAX_REMAPPED
202 _ct_very_high=$((CT_MAX_REMAPPED * 3))
203 if [ -z "$1" ]; then
204 _ct_max=$((CT_MAX_REMAPPED * 2))
205 elif [[ $1 -eq 0 ]]; then
206 _ct_mr=0
207 else
208 _ct_max=$1
209 fi
210 _ct_cbf=$(ct_get_current_remapped)
211 if [[ $_ct_cbf -gt $_ct_very_high ]]; then
212 ct_notify "Currently remapped PGs is very high"
213 while [[ $_ct_cbf -gt $_ct_very_high ]]; do
214 sleep 60
215 _ct_cbf=$(ct_get_current_remapped)
216 done
217 return 1
218 elif _check_variation_remapped ; then
219 ct_notify "Remapped PG don't change for ~ 10 minutes"
220 while _check_variation_remapped; do
221 sleep 60
222 _ct_cbf=$(ct_get_current_remapped)
223 done
224 return 1
225 elif [[ $_ct_cbf -gt $_ct_mr ]]; then
226 _ct_cu=$((CT_MAX_REMAPPED - (_ct_cbf - _ct_mr)))
227 if [ $_ct_cu -lt 0 ]
228 then
229 _ct_cu=0
230 fi
231 echo -ne "\r[$(_ct_progress ${_ct_cu} ${_ct_max})] ==> Current remapped > Max remapped (${_ct_cbf}>${_ct_mr}) "
232 return 1
233 fi
234 return 0
235 }
236
237 _ct_progress() {
238 # Display Progress Bar
239 # Param 1: current value
240 # Param 2: max value
241 _ct_step=$((${2:-64} / 64))
242 _ct_max=$2
243 _ct_current=$1
244 printf '%.0s#' $(seq 0 $_ct_step $_ct_current)
245 printf '%.0s_' $(seq $_ct_current $_ct_step $_ct_max)
246 }
247
248 ct_notify() {
249 # Notify by external tool
250 # Param 1: message
251 if [ ! -z "$CT_EXIT_ON_ERROR" ]; then
252 # echo $(hostname -s)-$(basename $0): ${1}: Script stopped >&2
253 ct_logger "Critical -> $1: Script stopped"
254 setterm -cursor on
255 echo
256 exit 1
257 else
258 # echo $(hostname -s)-$(basename $0): ${1}: Script paused >&2
259 ct_logger "Critical -> $1: Script paused"
260 fi
261 }
262
263 ct_logger() {
264 # Log in syslog
265 # Param 1: message
266 echo $1
267 logger -i "$(basename $0): $1"
268 }
269
270 ct_healthy_wait() {
271 # Wait For cluster Heathly
272 _ct_test_osd_down
273 _ct_test_temp
274 _ct_test_schedule
275 while ! _ct_test_remapped $1; do
276 sleep 10
277 done
278 echo
279 }
280
281
282 if [ ! -z "$CT_WAIT" ]
283 then
284 ct_healthy_wait 0
285 fi