This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathonboardLogUpload.sh
executable file
·329 lines (292 loc) · 11.4 KB
/
onboardLogUpload.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/sh
##########################################################################
# If not stated otherwise in this file or this component's Licenses.txt
# file the following copyright and licenses apply:
#
# Copyright 2016 RDK Management
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################
# Usage : ./onboardLogUpload.sh "upload" <file_to_upload>
# Arguments:
# upload - This will trigger an upload of <file_to_upload>
#
#
#
source /etc/log_timestamp.sh
if [ -f /etc/ONBOARD_LOGGING_ENABLE ]; then
ONBOARDLOGS_NVRAM_BACKUP_PATH="/nvram2/onboardlogs/"
ONBOARDLOGS_TMP_BACKUP_PATH="/tmp/onboardlogs/"
fi
source /lib/rdk/t2Shared_api.sh
source /etc/waninfo.sh
ARGS=$1
UploadFile=$2
blog_dir="/nvram2/onboardlogs/"
CURL_BIN="curl"
UseCodeBig=0
conn_str="Direct"
CodebigAvailable=0
encryptionEnable=`dmcli eRT getv Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.EncryptCloudUpload.Enable | grep value | cut -d ":" -f 3 | tr -d ' '`
URLENCODE_STRING=""
CODEBIG_BLOCK_TIME=1800
CODEBIG_BLOCK_FILENAME="/tmp/.lastcodebigfail_olu"
WAN_INTERFACE=$(getWanInterfaceName)
UploadHttpLink=$3
DIRECT_MAX_ATTEMPTS=3
CODEBIG_MAX_ATTEMPTS=3
#to support ocsp
EnableOCSPStapling="/tmp/.EnableOCSPStapling"
EnableOCSP="/tmp/.EnableOCSPCA"
if [ -f $EnableOCSPStapling ] || [ -f $EnableOCSP ]; then
CERT_STATUS="--cert-status"
fi
if [ "$UploadHttpLink" == "" ]
then
UploadHttpLink=$URL
fi
# Get the configuration of codebig settings
get_Codebigconfig()
{
# If GetServiceUrl not available, then only direct connection available and no fallback mechanism
if [ -f /usr/bin/GetServiceUrl ]; then
CodebigAvailable=1
fi
if [ "$CodebigAvailable" -eq "1" ]; then
CodeBigEnable=`dmcli eRT getv Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.CodeBigFirst.Enable | grep true 2>/dev/null`
fi
if [ "$CodebigAvailable" -eq "1" ] && [ "x$CodeBigEnable" != "x" ] ; then
UseCodeBig=1
conn_str="Codebig"
fi
if [ "$CodebigAvailable" -eq "1" ]; then
echo_t "Using $conn_str connection as the Primary"
else
echo_t "Only $conn_str connection is available"
fi
}
IsCodebigBlocked()
{
ret=0
if [ -f $CODEBIG_BLOCK_FILENAME ]; then
modtime=$(($(date +%s) - $(date +%s -r $CODEBIG_BLOCK_FILENAME)))
if [ "$modtime" -le "$CODEBIG_BLOCK_TIME" ]; then
echo "Last Codebig failed blocking is still valid, preventing Codebig"
ret=1
else
echo "Last Codebig failed blocking has expired, removing $CODEBIG_BLOCK_FILENAME, allowing Codebig"
rm -f $CODEBIG_BLOCK_FILENAME
ret=0
fi
fi
return $ret
}
# Direct connection Download function
useDirectRequest()
{
# Direct connection will not be tried if .lastdirectfail exists
retries=0
while [ "$retries" -lt "$DIRECT_MAX_ATTEMPTS" ]
do
WAN_INTERFACE=$(getWanInterfaceName)
echo_t "Trying Direct Communication"
CURL_CMD="$CURL_BIN --tlsv1.2 -w '%{http_code}\n' -d \"filename=$UploadFile\" $URLENCODE_STRING -o \"$OutputFile\" \"$S3_URL\" --interface $WAN_INTERFACE $addr_type $CERT_STATUS --connect-timeout 30 -m 30"
echo_t "File to be uploaded: $UploadFile"
UPTIME=`uptime`
echo_t "System Uptime is $UPTIME"
echo_t "S3 URL is : $S3_URL"
echo_t "Trial $retries for DIRECT ..."
#Sensitive info like Authorization signature should not print
echo_t "Curl Command built: `echo "$CURL_CMD" | sed -ne 's#AWSAccessKeyId=.*Signature=.*&#<hidden key>#p'`"
HTTP_CODE=`ret= eval $CURL_CMD`
if [ "x$HTTP_CODE" != "x" ];
then
http_code=$(echo "$HTTP_CODE" | awk '{print $0}' )
echo_t "Direct communication HttpCode received is : $http_code"
if [ "$http_code" != "" ];then
echo_t "Direct Communication - ret:$ret, http_code:$http_code"
if [ "$http_code" = "200" ] || [ "$http_code" = "302" ] ;then
echo $http_code > $UPLOADRESULT
return 0
fi
echo "failed" > $UPLOADRESULT
fi
else
http_code=0
echo_t "Direct Communication Failure Attempt:$retries - ret:$ret, http_code:$http_code"
fi
retries=`expr $retries + 1`
sleep 30
done
echo "Retries for Direct connection exceeded "
return 1
}
# Codebig connection Download function
useCodebigRequest()
{
# Do not try Codebig if CodebigAvailable != 1 (GetServiceUrl not there)
if [ "$CodebigAvailable" -eq "0" ] ; then
echo "OpsLog Upload : Only direct connection Available"
return 1
fi
IsCodebigBlocked
if [ "$?" = "1" ]; then
return 1
fi
if [ "$S3_MD5SUM" != "" ]; then
uploadfile_md5="&md5=$S3_MD5SUM"
fi
retries=0
while [ "$retries" -lt "$CODEBIG_MAX_ATTEMPTS" ]
do
echo_t "Trying Codebig Communication"
SIGN_CMD="GetServiceUrl 1 \"/cgi-bin/rdkb.cgi?filename=$UploadFile$uploadfile_md5\""
eval $SIGN_CMD > $SIGN_FILE
if [ -s $SIGN_FILE ]
then
echo "Log upload - GetServiceUrl success"
else
echo "Log upload - GetServiceUrl failed"
exit
fi
CB_SIGNED=`cat $SIGN_FILE`
rm -f $SIGN_FILE
S3_URL_SIGN=`echo $CB_SIGNED | sed -e "s|?.*||g"`
echo "serverUrl : $S3_URL_SIGN"
authorizationHeader=`echo $CB_SIGNED | sed -e "s|&|\", |g" -e "s|=|=\"|g" -e "s|.*filename|filename|g"`
authorizationHeader="Authorization: OAuth realm=\"\", $authorizationHeader\""
CURL_CMD="$CURL_BIN --tlsv1.2 -w '%{http_code}\n' -d \"filename=$UploadFile\" $URLENCODE_STRING -o \"$OutputFile\" \"$S3_URL_SIGN\" --interface $WAN_INTERFACE $addr_type -H '$authorizationHeader' $CERT_STATUS --connect-timeout 30 -m 30"
echo_t "File to be uploaded: $UploadFile"
UPTIME=`uptime`
echo_t "System Uptime is $UPTIME"
echo_t "S3 URL is : $S3_URL_SIGN"
echo_t "Trial $retries for CODEBIG ..."
#Sensitive info like Authorization signature should not print
echo_t "Curl Command built: `echo "$CURL_CMD" | sed -ne 's#'"$authorizationHeader"'#<Hidden authorization-header>#p'` "
HTTP_CODE=`ret= eval $CURL_CMD `
if [ "x$HTTP_CODE" != "x" ];
then
http_code=$(echo "$HTTP_CODE" | awk '{print $0}' )
echo_t "Codebig connection HttpCode received is : $http_code"
if [ "$http_code" != "" ];then
echo_t "Codebig Communication - ret:$ret, http_code:$http_code"
if [ "$http_code" = "200" ] || [ "$http_code" = "302" ] ;then
echo $http_code > $UPLOADRESULT
return 0
fi
echo "failed" > $UPLOADRESULT
fi
else
http_code=0
echo_t "Codebig Communication Failure Attempts:$retries - ret:$ret, http_code:$http_code"
fi
if [ "$retries" -lt "$CODEBIG_MAX_ATTEMPTS" ]; then
if [ "$retries" -eq "0" ]; then
sleep 10
else
sleep 30
fi
fi
retries=`expr $retries + 1`
done
echo "Retries for Codebig connection exceeded "
[ -f $CODEBIG_BLOCK_FILENAME ] || touch $CODEBIG_BLOCK_FILENAME
return 1
}
uploadOnboardLogs()
{
curDir=`pwd`
cd $blog_dir
# If interface doesnt have ipv6 address then we will force the curl to go with ipv4.
# Otherwise we will not specify the ip address family in curl options
addr_type=""
if [ "x$BOX_TYPE" = "xHUB4" ] || [ "x$BOX_TYPE" = "xSR300" ] || [ "x$BOX_TYPE" == "xSR213" ] || [ "x$BOX_TYPE" == "xSE501" ] || [ "x$BOX_TYPE" == "xWNXL11BWL" ]; then
CURRENT_WAN_IPV6_STATUS=`sysevent get ipv6_connection_state`
if [ "xup" = "x$CURRENT_WAN_IPV6_STATUS" ] ; then
[ "x`ifconfig $HUB4_IPV6_INTERFACE | grep Global | awk '/inet6/{print $3}' | cut -d '/' -f1 | head -n1`" != "x" ] || addr_type="-4"
else
[ "x`ifconfig $WAN_INTERFACE | grep inet6 | grep -i 'Global'`" != "x" ] || addr_type="-4"
fi
else
[ "x`ifconfig $WAN_INTERFACE | grep inet6 | grep -i 'Global'`" != "x" ] || addr_type="-4"
fi
S3_URL=$UploadHttpLink
S3_MD5SUM=""
echo "RFC_EncryptCloudUpload_Enable:$encryptionEnable"
if [ "$encryptionEnable" == "true" ]; then
S3_MD5SUM="$(openssl md5 -binary < $UploadFile | openssl enc -base64)"
URLENCODE_STRING="--data-urlencode \"md5=$S3_MD5SUM\""
fi
if [ "$UseCodeBig" -eq "1" ]; then
useCodebigRequest
ret=$?
else
useDirectRequest
ret=$?
fi
if [ "$ret" -ne "0" ]; then
echo "LOG UPLOAD UNSUCCESSFUL, ret = $ret"
t2CountNotify "SYS_ERROR_LOGUPLOAD_FAILED"
fi
# If 200, executing second curl command with the public key.
if [ "$http_code" = "200" ];then
#This means we have received the key to which we need to curl again in order to upload the file.
#So get the key from FILENAME
Key=$(awk '{print $0}' $OutputFile)
RemSignature=`echo $Key | sed "s/AWSAccessKeyId=.*Signature=.*&//g;s/\"//g;s/.*https/https/g"`
if [ "$encryptionEnable" != "true" ]; then
Key=\"$Key\"
fi
echo_t "Generated KeyIs : "
echo $RemSignature
CURL_CMD="curl --tlsv1.2 -w '%{http_code}\n' -T $UploadFile -o \"$OutputFile\" --interface $WAN_INTERFACE $Key $CERT_STATUS --connect-timeout 30 -m 30"
CURL_CMD_FOR_ECHO="curl --tlsv1.2 -w '%{http_code}\n' -T $UploadFile -o \"$OutputFile\" --interface $WAN_INTERFACE \"$RemSignature\" $CERT_STATUS --connect-timeout 30 -m 30"
echo_t "Curl Command built: $CURL_CMD_FOR_ECHO"
ret= eval $CURL_CMD > $HTTP_CODE
if [ -f $HTTP_CODE ];
then
http_code=$(awk '{print $0}' $HTTP_CODE)
if [ "$http_code" != "" ];then
echo_t "HttpCode received is : $http_code"
if [ "$http_code" = "200" ];then
echo $http_code > $UPLOADRESULT
break
else
echo "failed" > $UPLOADRESULT
fi
else
http_code=0
fi
fi
# Response after executing curl with the public key is 200, then file uploaded successfully.
if [ "$http_code" = "200" ];then
echo_t "LOGS UPLOADED SUCCESSFULLY, RETURN CODE: $http_code"
t2CountNotify "SYS_INFO_LOGS_UPLOADED"
fi
else
echo_t "LOG UPLOAD UNSUCCESSFUL, http_code = : $http_code"
t2CountNotify "SYS_ERROR_LOGUPLOAD_FAILED"
fi
cd $curDir
}
if [ "$ARGS" = "upload" ]
then
# Call function to upload onboard log files
uploadOnboardLogs
fi
if [ "$ARGS" = "delete" ]
then
echo_t "Deleting all onboard logs from $ONBOARDLOGS_NVRAM_BACKUP_PATH and $ONBOARDLOGS_TMP_BACKUP_PATH"
rm -rf $ONBOARDLOGS_TMP_BACKUP_PATH
rm -rf $ONBOARDLOGS_NVRAM_BACKUP_PATH
fi