blob: 8876ffd6c6ef24063a968cd4066eb1c0a6f26af5 [file] [log] [blame]
developer15a43312022-04-12 11:23:23 +08001#!/bin/sh
2##########################################################################
3# If not stated otherwise in this file or this component's Licenses.txt
4# file the following copyright and licenses apply:
5#
6# Copyright 2018 RDK Management
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19##########################################################################
20
21. /etc/include.properties
22. /etc/device.properties
23
24. $RDK_PATH/utils.sh
25. $RDK_PATH/interfaceCalls.sh
26. $RDK_PATH/utils.sh
27. $RDK_PATH/logfiles.sh
28. $RDK_PATH/commonUtils.sh
29
30if [ $# -ne 6 ]; then
31 echo "USAGE: $0 <TFTP Server IP> <Flag (STB delay or not)> <SCP_SERVER> <UploadOnReboot> <UploadProtocol> <UploadHttpLink>"
32fi
33
34# assign the input arguments
35TFTP_SERVER=$1
36FLAG=$2
37DCM_FLAG=$3
38UploadOnReboot=$4
39UploadProtocol=$5
40UploadHttpLink=$6
41
42DCM_LOG_PATH=/upload
43
44if [ $FLAG -eq 0 ]; then
45 if [ -f $RAMDISK_PATH/.standby ]; then
46 echo "`/bin/timestamp` Exiting since box is in standby..!"
47 exit 0
48 fi
49fi
50
51# initialize the variables
52MAC=`getErouterMacAddress`
53HOST_IP=`getIPAddress`
54dt=`date "+%m-%d-%y-%I-%M%p"`
55LOG_FILE="$MAC_Logs_$dt.tgz"
56
57#MARKER_FILE=$MAC"_Logs_Marker_$dt.txt"
58VERSION="version.txt"
59# working folders
60PREV_LOG_PATH="$LOG_PATH/PreviousLogs"
61PREV_LOG_BACKUP_PATH="$LOG_PATH/PreviousLogs_backup/"
62DCM_UPLOAD_LIST="$LOG_PATH/dcm_upload"
63
64echo "Build Type: $BUILD_TYPE Log file: $LOG_FILE TFTP Server: $TFTP_SERVER Protocol: $UploadProtocol UploadHttpLink: $UploadHttpLink" >> $LOG_PATH/dcmscript.log
65
66prevUploadFlag=0
67
68if [ ! -d $PREV_LOG_PATH ]; then
69 echo "The Previous Logs folder is missing" >> $LOG_PATH/dcmscript.log
70 #if [ "true" != "$RDK_EMULATOR" ]; then
71 # exit 0
72 #else
73
74 if [ ! -d $LOG_PATH ]; then mkdir -p $LOG_PATH; fi
75 if [ ! -d $LOG_PATH/PreviousLogs ]; then mkdir -p $LOG_PATH/PreviousLogs; fi
76 if [ ! -d $LOG_PATH/PreviousLogs_backup ]; then mkdir -p $LOG_PATH/PreviousLogs_backup; fi
77 rm -rf $LOG_PATH/PreviousLogs_backup/*
78
79 backupSystemLogFiles mv $LOG_PATH $PREV_LOG_PATH
80 backupAppBackupLogFiles mv $LOG_PATH $PREV_LOG_PATH
81
82 #fi
83else
84 #if [ "true" != "$RDK_EMULATOR" ]; then
85 #echo ""
86 #else
87
88 rm -rf $LOG_PATH/PreviousLogs_backup/*
89 backupSystemLogFiles mv $LOG_PATH $PREV_LOG_PATH
90 backupAppBackupLogFiles mv $LOG_PATH $PREV_LOG_PATH
91 #cp $PREV_LOG_BACKUP_PATH/* $PREV_LOG_PATH/
92 #fi
93fi
94
95backupAppLogs()
96{
97 source=$1
98 destn=$2
99 if [ -f $source$RILog ] ; then cp $source$RILog $destn; fi
100 if [ -f $source$XRELog ] ; then cp $source$XRELog $destn; fi
101 if [ -f $source$WBLog ] ; then cp $source$WBLog $destn; fi
102 if [ -f $source$SysLog ] ; then cp $source$SysLog $destn; fi
103}
104renameRotatedLogs()
105{
106 logPath=$1
107 if [ -f $RDK_PATH/renameRotatedLogs.sh ]; then
108 if [ -f $logPath/ocapri_log.txt ] ; then sh $RDK_PATH/renameRotatedLogs.sh $logPath/ocapri_log.txt; fi
109 if [ -f $logPath/receiver.log ] ; then sh $RDK_PATH/renameRotatedLogs.sh $logPath/receiver.log; fi
110 if [ -f $logPath/greenpeak.log ] ; then sh $RDK_PATH/renameRotatedLogs.sh $logPath/greenpeak.log; fi
111 if [ -f $logPath/gp_init.log ] ; then sh $RDK_PATH/renameRotatedLogs.sh $logPath/gp_init.log; fi
112 if [ -f $logPath/app_status.log ] ; then sh $RDK_PATH/renameRotatedLogs.sh $logPath/app_status.log; fi
113 fi
114}
115processLogsFolder()
116{
117 srcLogPath=$1
118 destnLogPath=$2
119 backupAppLogs "$srcLogPath/" "$destnLogPath/"
120 backupSystemLogFiles "cp" $srcLogPath $destnLogPath
121 backupAppBackupLogFiles "cp" $srcLogPath $destnLogPath
122
123 if [ -f $RAMDISK_PATH/disk_log.txt ]; then cp $RAMDISK_PATH/disk_log.txt $destnLogPath ; fi
124
125 backupCount=`ls $srcLogPath/logbackup-* 2>/dev/null | wc -l`
126 if [ $backupCount -gt 0 ]; then
127 cp -r $srcLogPath/logbackup-* $destnLogPath
128 fi
129
130 if [ -f $srcLogPath/$rebootLog ]; then cp $srcLogPath/$rebootLog $destnLogPath; fi
131 if [ -f $srcLogPath/$ablReasonLog ]; then cp $srcLogPath/$ablReasonLog $destnLogPath; fi
132 if [ -f $srcLogPath/$ueiLog ]; then cp $srcLogPath/$ueiLog $destnLogPath; fi
133 if [ -f $PERSISTENT_PATH/sventest/p3541_all_csven_AV_health_data_trigger.tar.gz ] ; then
134 cp $PERSISTENT_PATH/sventest/p3541_all_csven_AV_health_data_trigger.tar.gz $destnLogPath
135 fi
136 if [ "$DEVICE_TYPE" != "mediaclient" ]; then
137 renameRotatedLogs $srcLogPath
138 fi
139}
140modifyFileWithTimestamp()
141{
142 srcLogPath=$1
143 ret=`ls $srcLogPath/*.txt | wc -l`
144 if [ ! $ret ]; then
145 ret=`ls $srcLogPath/*.log | wc -l`
146 if [ ! $ret ]; then exit 1; fi
147 fi
148
149 dt=`date "+%m-%d-%y-%I-%M%p-"`
150 FILES=*.*
151 FILES1=".*-[0-9][0-9]AM-.*"
152 FILES2=".*-[0-9][0-9]PM-.*"
153
154 for f in $FILES
155 do
156 test1=0
157 test2=0
158 test3=0
159 test4=0
160
161 test1=`expr match $f $FILES1`
162 test2=`expr match $f $FILES2`
163 test3=`expr match $f $rebootLog`
164 test4=`expr match $f $ablReasonLog`
165
166 if [ $test1 -gt 0 -o $test2 -gt 0 -o $test3 -gt 0 -o $test4 -gt 0 ]; then
167 echo "`/bin/timestamp` Processing file...$f" >> $LOG_PATH/dcmscript.log
168 else
169 mv $f $dt$f
170 fi
171 done
172 #cp /version.txt ./$dt$VERSION
173}
174
175copyAllFiles ()
176{
177
178 EXCLUDE="dcm PreviousLogs_backup PreviousLogs"
179 cd $LOG_PATH
180
181 for fileName in *
182 do
183 COPY_BOOLEAN=true
184 for excl in $EXCLUDE
185 do
186 if [ $excl == $fileName ]; then
187 COPY_BOOLEAN=false
188 fi
189 done
190
191 if $COPY_BOOLEAN; then
192 cp -R $fileName $DCM_LOG_PATH
193 fi
194 done
195}
196copyOptLogsFiles ()
197{
198 cd $LOG_PATH
199 cp * $DCM_LOG_PATH >> $LOG_PATH/dcmscript.log 2>&1
200}
201
202HttpLogUpload()
203{
204 result=1
205 FILENAME='/tmp/httpresult.txt'
206 HTTP_CODE=/tmp/curl_httpcode
207
208 CLOUD_URL=$UploadHttpLink
209 #CLOUD_URL="$(UploadHttpLink)filename=$LOG_FILE"
210
211 CURL_CMD="curl -w '%{http_code}\n' -F \"filename=@$1\" -o \"$FILENAME\" \"$CLOUD_URL\" --connect-timeout 10 -m 10"
212 echo URL_CMD: $CURL_CMD
213 ret= eval $CURL_CMD > $HTTP_CODE
214 http_code=$(awk -F\" '{print $1}' $HTTP_CODE)
215 if [ $http_code -eq 200 ];then
216 echo "`/timestamp` Done Uploading Logs through HTTP" >> $LOG_PATH/dcmscript.log
217 result=0
218 elif [ $http_code -eq 302 ];then
219 #Get the url from FILENAME
220 NewUrl=$(awk -F\" '{print $1}' $FILENAME)
221
222 CURL_CMD="curl -w '%{http_code}\n' -o \"$FILENAME\" \"$NewUrl\" --connect-timeout 10 -m 10"
223 echo URL_CMD: $CURL_CMD
224 result= eval $CURL_CMD > $HTTP_CODE
225 http_code=$(awk -F\" '{print $1}' $HTTP_CODE)
226
227 if [ $http_code -eq 200 ];then
228 echo "`/timestamp` Done Uploading Logs through - HTTP" >> $LOG_PATH/dcmscript.log
229 result=0
230 else
231 echo "`/timestamp` Failed Uploading Logs through - HTTP" >> $LOG_PATH/dcmscript.log
232 fi
233 fi
234 echo $result
235}
236
237uploadDCMLogs()
238{
239
240 cd $DCM_LOG_PATH
241 echo " Uploading Logs through DCM cron job" >> $LOG_PATH/dcmscript.log
242 modifyFileWithTimestamp $DCM_LOG_PATH >> $LOG_PATH/dcmscript.log 2>&1
243 tar -zcvf $LOG_FILE * >> $LOG_PATH/dcmscript.log 2>&1
244 sleep 60
245 echo "Uploading logs $LOG_FILE onto $TFTP_SERVER" >> $LOG_PATH/dcmscript.log
246
247 retval=1
248
249 if [ "$UploadProtocol" == "HTTP" ];then
250 retval=$(HttpLogUpload $LOG_FILE)
251 fi
252 if [ $retval -eq 1 ];then #Http upload failed
253 tftp -p -r $LOG_FILE -l $LOG_FILE $TFTP_SERVER >> $LOG_PATH/dcmscript.log 2>&1
254 echo "`/bin/timestamp` Done Uploading Logs through TFTP" >> $LOG_PATH/dcmscript.log
255 sleep 1
256 fi
257
258 rm -rf $DCM_LOG_PATH/
259}
260uploadLogOnReboot()
261{
262 uploadLog=$1
263 echo "Sleeping for seven minutes "
264 if [ "true" != "$RDK_EMULATOR" ]; then
265 sleep 12
266 fi
267 echo "Done sleeping prev logpath "$PREV_LOG_PATH
268 ret=`ls $PREV_LOG_PATH/*.txt | wc -l`
269 if [ ! $ret ]; then
270 ret=`ls $PREV_LOG_PATH/*.log | wc -l`
271 if [ ! $ret ]; then exit 1; fi
272 fi
273 if [ "$HDD_ENABLED" = "true" ]; then
274 # Special processing - Permanently backup logs on box delete the logs older than
275 # 3 days to take care of old filename
276 sleep 2
277 stat=`find /opt/logs -name "*-*-*-*-*M-" -mtime +3 -exec rm -rf {} \;`
278 # for the new filenames with suffix logbackup
279 stat=`find /opt/logs -name "*-*-*-*-*M-logbackup" -mtime +3 -exec rm -rf {} \;`
280
281 TIMESTAMP=`date "+%m-%d-%y-%I-%M%p-logbackup"`
282 PERM_LOG_PATH="$LOG_PATH/$TIMESTAMP"
283 mkdir -p $PERM_LOG_PATH
284
285 processLogsFolder $PREV_LOG_PATH $PERM_LOG_PATH
286 fi
287 echo "ckp100-------------prev log path-------------"$PREV_LOG_PATH
288 cd $PREV_LOG_PATH
289 rm $LOG_FILE
290 modifyFileWithTimestamp $PREV_LOG_PATH >> $LOG_PATH/dcmscript.log 2>&1
291
292 echo "ckp101---------------------upload log-----"$uploadLog
293 ls -al
294 sleep 30
295 if $uploadLog; then
296 echo "ckp101--------------------------"
297 tar -zcvf $LOG_FILE * >> $LOG_PATH/dcmscript.log 2>&1
298 echo "Uploading logs $LOG_FILE onto $TFTP_SERVER" >> $LOG_PATH/dcmscript.log
299 sleep 60
300 #tftp -p -r $LOG_FILE -l $LOG_FILE $TFTP_SERVER >> $LOG_PATH/dcmscript.log 2>&1
301 #sleep 1
302 #echo "`/bin/timestamp` Done Uploading Logs" >> $LOG_PATH/dcmscript.log
303 proUpdel=`cat /tmp/DCMSettings.conf | grep -i uploadRepository:uploadProtocol | tr -dc '"' |wc -c`
304 echo "number of proUPdel2:"$proUpdel
305 #proUpdel=$((proUpdel - 1))
306 uploadProtocolla=`cat /tmp/DCMSettings.conf | grep -i urn:settings:TelemetryProfile | cut -d '"' -f$proUpdel`
307 echo "Upload protocol logupload is:"$uploadProtocolla
developercc441a02022-05-09 19:04:26 +0800308 FilogicMacAddress=`ifconfig erouter0 | grep HWaddr | cut -c39-55`
309 cp $LOG_FILE $FilogicMacAddress-Logs-$LOG_FILE
developer15a43312022-04-12 11:23:23 +0800310 if [ "$uploadProtocolla" != "TFTP" ];then
311 echo "before HTTP log upload"
312 HTTPLOGUPLOADURL=`cat /tmp/DCMSettings.conf | grep -i "urn:settings:LogUploadSettings:RepositoryURL" | cut -d "=" -f2`
313 if [ "$HTTPLOGUPLOADURL" == "" ]; then
314 echo "No HTTP URL configured in xconf,going with internal one !!"
315 HTTPLOGUPLOADURL=$DCM_LA_SERVER_URL
316 fi
317 echo "HTTPLOGUPLOADURL:"$HTTPLOGUPLOADURL
318 echo "upload log file is:"$LOG_FILE
developercc441a02022-05-09 19:04:26 +0800319 CURL_CMD="curl -w '%{http_code}\n' -F filename=@$PREV_LOG_PATH/$FilogicMacAddress-Logs-$LOG_FILE '$HTTPLOGUPLOADURL' --connect-timeout 100 -m 100"
developer15a43312022-04-12 11:23:23 +0800320 #echo_t "CURL_CMD http proto log upload: $CURL_CMD" >> $DCM_LOG_FILE
321 echo "------CURL_CMD2:"$CURL_CMD
322 HTTP_CODE=`result= eval $CURL_CMD`
323 http_code=$(echo $HTTP_CODE | cut -d "." -f 2)
324 echo "http_code is :"$http_code
325 if [ $http_code -eq 200 ] ; then
326 echo "HTTP log upload succeded!!!!!!!!!!!!!!!!!"
327 else
328 loguploadRetryCount=0
329 while [ $loguploadRetryCount -lt 2 ]
330 do
331 echo "Trying to upload log file..."
developercc441a02022-05-09 19:04:26 +0800332 CURL_CMD="curl -w '%{http_code}\n' -F filename=@$PREV_LOG_PATH/$FilogicMacAddress-Logs-$LOG_FILE '$HTTPLOGUPLOADURL' --connect-timeout 100 -m 100"
developer15a43312022-04-12 11:23:23 +0800333 HTTP_CODE=`result= eval $CURL_CMD`
334 http_code_la=$(echo $HTTP_CODE | cut -d "." -f 2)
335 echo "http_code is :"$http_code_la
336 if [ "$http_code_la" != "200" ]; then
337 echo "Error in uploading log file"
338 else
339 echo "logupload succeded in retry"
340 break
341 fi
342 loguploadRetryCount=`expr $loguploadRetryCount + 1`
343 done
344 if [ $loguploadRetryCount -eq 2]; then
345 echo "HTTP log upload failed!!!!!!!!!!!!!!!!!"
346 fi
347 fi
348 else
349 echo "Uploading logs $LOG_FILE onto $TFTP_SERVER" >> $LOG_PATH/dcmscript.log
developercc441a02022-05-09 19:04:26 +0800350 tftp -p -r $FilogicMacAddress-Logs-$LOG_FILE -l $FilogicMacAddress-Logs-$LOG_FILE $TFTP_SERVER >> $LOG_PATH/dcmscript.log 2>&1
developer15a43312022-04-12 11:23:23 +0800351 ret=$?
352 echo $ret
353 if [ "$ret" -eq 1 ]; then
354 tftplauploadRetryCount=0
355 while [ "$tftplauploadRetryCount" -lt 2 ]
356 do
357 echo "Trying to upload logs file using tftp again..."
developercc441a02022-05-09 19:04:26 +0800358 tftp -p -r $FilogicMacAddress-Logs-$LOG_FILE -l $FilogicMacAddress-Logs-$LOG_FILE $TFTP_SERVER >> $LOG_PATH/dcmscript.log 2>&1
developer15a43312022-04-12 11:23:23 +0800359 ret=$?
360 if [ "$ret" -eq 1 ]; then
361 echo "error in uploading logs using tftp"
362 else
363 echo "tftp upload in retry logs succeded"
364 ret=0
365 break
366 fi
367 tftplauploadRetryCount=`expr $tftplauploadRetryCount + 1`
368 done
369 if [ "$tftlauploadRetryCount" -eq 2 ]; then
370 ret=1
371 echo "TFTP log upload failed!!!!!!!!!!!!!!!!!"
372 else
373 echo "TFTP log upload succeded !!!"
374 ret=0
375 fi
376 fi
377 fi
378 sleep 60
379 echo "Done Uploading Logs and removing rtl_json.txt file"
developercc441a02022-05-09 19:04:26 +0800380 rm -rf $PREV_LOG_PATH/$FilogicMacAddress-Logs-$LOG_FILE
developer15a43312022-04-12 11:23:23 +0800381 rm -rf $TELEMETRY_JSON_RESPONSE
382 rm -rf $PERSISTENT_PATH/*TELE*
383 fi
384 cd $PREV_LOG_PATH
385 rm -rf $PREV_LOG_PATH/$LOG_FILE
386 rm -rf $PREV_LOG_BACKUP_PATH
387 mkdir -p $PREV_LOG_BACKUP_PATH
388 if [ "$BUILD_TYPE" = "dev" ] || [ "$HDD_ENABLED" = "false" ]; then
389
390 echo "Moving to Previous Logs Backup Folder " >> $LOG_PATH/dcmscript.log
391 mv * $PREV_LOG_BACKUP_PATH
392 else
393 echo "`/bin/timestamp` Deleting from Previous Logs Folder " >> $LOG_PATH/dcmscript.log
394 rm -rf *
395 fi
396}
397#if [ "$true" != "$RDK_EMULATOR" ]; then
398#if [ -d $DCM_LOG_PATH ]; then
399 # rm -rf $DCM_LOG_PATH/
400#fi
401#Remove *.tgz files from /opt/logs
402#if [ "true" != "$RDK_EMULATOR" ]; then
403#stat=`find $LOG_PATH -name "*.tgz" -exec rm -rf {} \;`
404
405#Remove files which have timestamp in it filename
406#for item in `ls $LOG_PATH/*-*-*-*-*M-* | grep "[0-9]*-[0-9]*-[0-9]*-[0-9]*-M*" | grep -v "logbackup"`;do
407 # if [ -f "$item" ];then
408 # echo "`/bin/timestamp` Removing $item" >> $LOG_PATH/dcmscript.log
409 # rm -rf $item
410 #fi
411#done
412#fi
413#fi
414if [ $DCM_FLAG -eq 0 ] ; then
415 echo "`/bin/timestamp` Uploading Without DCM" >> $LOG_PATH/dcmscript.log
416 uploadLogOnReboot true
417else
418 if [ $FLAG -eq 1 ] ; then
419 if [ $UploadOnReboot -eq 1 ]; then
420 echo "Uploading Logs with DCM UploadOnReboot set to true" >> $LOG_PATH/dcmscript.log
421 echo "call uploadLogOnReboot"
422 uploadLogOnReboot true
423
424 else
425 echo "`/bin/timestamp` Not Uploading Logs with DCM UploadOnReboot set to false" >> $LOG_PATH/dcmscript.log
426 uploadLogOnReboot false
427 echo $PERM_LOG_PATH >> $DCM_UPLOAD_LIST
428 fi
429
430 else
431 if [ $UploadOnReboot -eq 0 ]; then
432 mkdir -p $DCM_LOG_PATH
433 if [ "$HDD_ENABLED" = "true" ]; then
434 fileUploadCount=`cat "$DCM_UPLOAD_LIST" | wc -l`
435 if [ $fileUploadCount -gt 0 ]; then
436 while read line
437 do
438 echo $line
439 cp -R $line $DCM_LOG_PATH
440 done < $DCM_UPLOAD_LIST
441 copyOptLogsFiles
442 cat /dev/null > $DCM_UPLOAD_LIST
443 uploadDCMLogs
444 else
445 copyOptLogsFiles
446 uploadDCMLogs
447 fi
448 else
449 if [ -f $PREV_LOG_BACKUP_PATH/uploaded ]; then
450 copyOptLogsFiles
451 uploadDCMLogs
452 else
453 cd $PREV_LOG_BACKUP_PATH
454 foldertime=`ls *version.txt | cut -c 1-16`
455 if [ -z $foldertime ]; then
456 foldertime=`date +%m-%d-%y-%I-%M%p`
457 fi
458 TIMESTAMP=$foldertime-logbackup
459 PERM_LOG_PATH="/mnt/memory/dcm/$TIMESTAMP"
460 mkdir -p $PERM_LOG_PATH
461 cp * $PERM_LOG_PATH
462 copyOptLogsFiles
463 uploadDCMLogs
464 cd $PREV_LOG_BACKUP_PATH
465 touch uploaded
466 fi
467 fi
468
469 else
470 if [ "$HDD_ENABLED" = "true" ]; then
471 touch $DCM_INDEX
472 copyAllFiles
473 uploadDCMLogs
474 fi
475 fi
476 fi
477fi
478