[Feature][ZK-65][mtklog] ftp log to soc

           Affected branch: unknown
           Affected module: unknown
           Is it affected on both ZXIC and MTK: MTK
           Self-test: Yes
           Doc Update: No

Change-Id: Iced09a9f58b51cd4635bdca40ca4836bcefbb1d5
diff --git a/src/lynq/packages/apps/lynq-log-transfer/lynq_inner_log_transfer.sh b/src/lynq/packages/apps/lynq-log-transfer/lynq_inner_log_transfer.sh
index 759c4f5..1a8a462 100755
--- a/src/lynq/packages/apps/lynq-log-transfer/lynq_inner_log_transfer.sh
+++ b/src/lynq/packages/apps/lynq-log-transfer/lynq_inner_log_transfer.sh
@@ -1,3 +1,4 @@
+#!/bin/bash
 #set -x
 #set -e
 
@@ -23,8 +24,8 @@
 ERROR_IS_IN_WAIT_TRRANSFER_REMAIN_DBG_LOG=$((ERROR_IS_IN_TRANSFER_LOG+12))
 
 
-DEBUG_FILE_PATH=/data/log_transfer_data/
-DEBUG_FILE_NAME=log_transfer_debug.log
+DEBUG_FILE_PATH=/var/log/
+DEBUG_FILE_NAME=syslog.log
 DEBUG_FILE_BACKUP_NAME=log_transfer_debug_0.log
 DEBUG_FILE="${DEBUG_FILE_PATH}${DEBUG_FILE_NAME}"
 DEBUG_FILE_BACKUP="${DEBUG_FILE_PATH}${DEBUG_FILE_BACKUP_NAME}"
@@ -37,7 +38,7 @@
 MTK_LOG_PATH="/var/log/mtklog/"
 TMP_LOG_PATH="/tmp/log_transfer_tmp/"
 MTK_LOG_BUFF_PATH="${TMP_LOG_PATH}mtklog_buffer/"
-MTK_LOG_MOUNT_PATH="${TMP_LOG_PATH}mtklog_mount/"
+MTK_LOG_MOUNT_PATH="${TMP_LOG_PATH}mtklog"
 CONTROL_FILE_PATH="${TMP_LOG_PATH}control/"
 LOG_IS_IN_TRANSFER=(
      "${CONTROL_FILE_PATH}is_in_transfer_sys_log"
@@ -51,9 +52,9 @@
     )
 	
 TARGET_DIR=(      
-      "/lynq/log/lynq_log_1/"
-      "/lynq/log/lynq_log_2/"
-      "/lynq/log/lynq_log_1/"
+      "lynq/log/lynq_log_1"
+      "lynq/log/lynq_log_2"
+      "None"
     )
 	
 target_dir=""
@@ -88,6 +89,58 @@
     0
 )
 
+LOG_PREFIX=(
+    "archive_log"
+    "MDLog"
+    ""
+)
+log_prefix=""
+
+#unit 0.1M
+SINGLE_FILE_SIZE=(
+    21
+    100
+    0
+)
+single_file_size=10
+
+#unit M
+INIT_LOG_MAX_SIZE=(
+    1000
+    2700
+    0
+)
+log_num_max=0
+LOG_DEL=$(($log_num_max/2))
+
+
+Q_CUR_NUM_UCI=(
+    "lynq_uci.lynq_log.log_1_cur_trf_num"
+    "lynq_uci.lynq_log.log_2_cur_trf_num"
+    ""
+)
+q_cur_num_uci=""
+
+Q_MAX_SIZE_UCI=(
+    "lynq_uci.lynq_log.log_1_max_size"
+    "lynq_uci.lynq_log.log_2_max_size"
+    ""
+)
+q_max_size_uci=""
+
+
+
+TAR_CMD="gzip"
+TAR_ARG=" -f"
+TAR_EXT="gz"
+
+UP_LOAD_POSTFIX=(
+    $TAR_EXT
+    "muxz"
+    ""
+)
+q_max_size_uci=""
+
 
 if [ ! -d ${CONTROL_FILE_PATH} ]; then                    
     mkdir -p ${CONTROL_FILE_PATH}
@@ -101,7 +154,7 @@
     do
         if [ ${transfer_log_tag[$i]} -eq 1 ]; then                       
             if [ $i -eq 0 ]; then              
-                rm -rf $SYS_LOG_PATH$SYS_LOG_FILE_COMMON_PREFIX*@*                    
+                rm -rf $SYS_LOG_PATH$log_prefix*                    
             elif [ $i -eq 1 ]; then
                 echo 7 | emdlogger_ctrl        
                 rm -rf $MTK_LOG_PATH*
@@ -180,24 +233,26 @@
     add_time_and_prefix_info="${2}${add_time_info}"
 }
 
+
+DBG_INFO_PREFIX="lynq_log_transfer "
 function echo_debug_info()
 {
     echo $1
     is_in_debug 	
     if [ $? -eq 1 ]; then
-        add_time_to_info " $1"
-        echo $add_time_info >> $DEBUG_FILE        
-        rotate_debug_file        
+        add_time_and_prefix_to_info " $1" $DBG_INFO_PREFIX
+        echo $add_time_and_prefix_info >> $DEBUG_FILE        
+#       rotate_debug_file        
     fi    
 }
 
 function print_usage()
 {   
-    echo "Usage : lynq-log-transfer  1 | 2 | 3 | 101 | 102 | 103 " >&2
+    echo "Usage : lynq-log-transfer 0 | 1 | 2 | 3 | 101 | 102 | 103 " >&2
+	echo "0 : Init, param like this: 0 config_id" >&2
     echo "1 : Start Transfer syslog, param like this: 1 192.168.10.10 21 username passwword" >&2
     echo "2 : Start Transfer mtklog,  param like this: 2 192.168.10.10 21 username passwword" >&2
-    echo "3 : Start debug and Transfer debug log,  param like this: 3 192.168.10.10 21 username passwword" >&2
-    echo "note: just for 3 suppport 127.0.0.1 anyport anyuser anypassword, means keep file in local" >&2
+    echo "3 : Start debug and Transfer debug log,  param like this: 3" >&2
     echo "101 : Stop transfer sys log, param like this: 101" >&2
     echo "102 : Stop transfer mtk log, param like this: 102" >&2
     echo "103 : Stop debug and stop transfer debug log, param like this: 103" >&2    
@@ -242,6 +297,15 @@
       $ERROR_IS_IN_WAIT_TRRANSFER_REMAIN_MTK_LOG)
         echo_debug_info "is still waitting transfer remain mtk log"
         ;;           
+       $ERROR_IS_IN_TRANSFER_DBG_LOG)
+        echo_debug_info "is still in transfering dbg log"
+        ;;    
+      $ERROR_IS_IN_TRANSFER_REMAIN_DBG_LOG)  
+        echo_debug_info "is still in transfering remain dbg log"
+        ;;  
+      $ERROR_IS_IN_WAIT_TRRANSFER_REMAIN_DBG_LOG)
+        echo_debug_info "is still waitting transfer remain dbg log"
+        ;;           
       *)
         echo_debug_info "other error"
         ;;
@@ -261,7 +325,7 @@
       new_file_name=$new_file_name.$TAR_EXT
     fi        
        
-    curl ftp://$ftp_server:$ftp_port$target_dir -u "${ftp_user_name}:${ftp_password}" -T $new_file_name
+    curl ftp://$ftp_server:$ftp_port/$target_dir/ -u "${ftp_user_name}:${ftp_password}" -T $new_file_name 1>/dev/null
     ret=$?
     rm -rf $new_file_name
     return $ret
@@ -277,20 +341,23 @@
     fi
 }
 
+CUR_LOG_NUM=0
+function save_cur_log_num_to_uci()
+{
+    if [ ! -z $q_cur_num_uci ]; then    
+        uci set ${q_cur_num_uci}=${CUR_LOG_NUM}
+    fi
+}
+
 function exit_process()
 {    
-     print_exit_info $1 
-     clear_relative_tag_when_exit  $1 
-     exit $1
-
+    print_exit_info $1 
+    clear_relative_tag_when_exit  $1 
+    save_cur_log_num_to_uci
+    exit $1
 }
 
 
-TAR_CMD="gzip"
-TAR_ARG=" -f"
-TAR_EXT="gz"
-
-SYS_LOG_NUM=0
 SYS_LOG_FILES=(
     "syslog.log.9"
     "syslog.log.8"
@@ -306,58 +373,70 @@
 
 SYSLOG_INDEX=(0 1 2 3 4 5 6 7 8 9)
 
-SYS_LOG_FILE_COMMON_PREFIX="archive_log"
+#SYS_LOG_FILE_COMMON_PREFIX="archive_log"
 
-
-function do_sys_log_num_plus()
+function do_cur_log_num_plus()
 {
-    ((SYS_LOG_NUM++))
-    if [ $SYS_LOG_NUM -gt 999 ]; then
-        SYS_LOG_NUM=0
-    fi    
+    ((CUR_LOG_NUM++))
+    if [ $CUR_LOG_NUM -gt $log_index_max ]; then
+        CUR_LOG_NUM=0
+    fi
+}
+
+function ftp_delete_file()
+{
+   q_cmd="DELE $target_dir/$1"
+   curl -u $ftp_user_name:$ftp_password ftp://$ftp_server:$ftp_port  -Q "$q_cmd" 1>/dev/null
+
+}
+
+function get_del_num()
+{	
+    if [ $1 -lt $((${2}/2)) ]; then
+        LOG_DEL=$((${1}+${2}/2))
+    else
+	LOG_DEL=$((${1}-${2}/2))
+    fi
 }
 
 function do_sys_log_transfer()
 {
-    cd ${SYS_LOG_PATH}
-    SYS_LOG_START=$SYS_LOG_NUM 
-    add_time_and_prefix_to_info  "" ${SYS_LOG_FILE_COMMON_PREFIX}
-    SYS_LOG_FILE_PREFIX=$add_time_and_prefix_info
+    cd ${SYS_LOG_PATH}     
     EXIST_SYS_LONG_NUM=0
     for SYS_LOG_FILE in ${SYS_LOG_FILES[@]} $1; do        
         if [ -e "${SYS_LOG_PATH}/${SYS_LOG_FILE}" ]; then            
-            mv ${SYS_LOG_FILE} ${SYS_LOG_FILE_PREFIX}_@${SYS_LOG_NUM}
-            SYSLOG_INDEX[$EXIST_SYS_LONG_NUM]=${SYS_LOG_NUM}
+            mv ${SYS_LOG_FILE} ${log_prefix}_${CUR_LOG_NUM}
+            SYSLOG_INDEX[$EXIST_SYS_LONG_NUM]=${CUR_LOG_NUM}
             ((EXIST_SYS_LONG_NUM++))        
-            do_sys_log_num_plus                            
+            do_cur_log_num_plus                            
         fi
     done    
     
     for j in `seq 0 $(($EXIST_SYS_LONG_NUM-1))`
     do          
-         ${TAR_CMD} ${TAR_ARG} ${SYS_LOG_FILE_PREFIX}_@${SYSLOG_INDEX[$j]}         
+         ${TAR_CMD} ${TAR_ARG} ${log_prefix}_${SYSLOG_INDEX[$j]}         
     done
         
     for j in `seq 0 $(($EXIST_SYS_LONG_NUM-1))`
-    do   
-         ftp_transfer_file_then_delete ${SYS_LOG_FILE_PREFIX}_@${SYSLOG_INDEX[$j]}.$TAR_EXT    0    
-         if [ $? -ne 0 ]; then
-              echo_debug_info "ftp error code is $?"
-              exit_process $ERROR_FTP_TRANSFER_FAIL        
-         fi              
+    do  
+        get_del_num ${SYSLOG_INDEX[$j]} $log_num_max
+        ftp_delete_file ${log_prefix}_${LOG_DEL}.$TAR_EXT
+        ftp_transfer_file_then_delete ${log_prefix}_${SYSLOG_INDEX[$j]}.$TAR_EXT    0    
+        if [ $? -ne 0 ]; then
+             echo_debug_info "ftp error code is $?"
+             exit_process $ERROR_FTP_TRANSFER_FAIL        
+        fi              
     done
 }    
 
 function do_backup_mtk_log()
 {
     cd $MTK_LOG_BUFF_PATH
-    for muxzfile in *."$1"; do            
+    for muxzfile in *."$1"; do       
+        get_del_num $CUR_LOG_NUM $log_num_max
+        ftp_delete_file ${log_prefix}_${LOG_DEL}.muxz	
         ftp_transfer_file_then_delete $muxzfile 0
-        if [ $? -eq 0 ]; then
-            add_time_and_prefix_to_info  "" "time_stat_"
-            touch $add_time_and_prefix_info
-            ftp_transfer_file_then_delete $add_time_and_prefix_info 0            
-        else    
+        if [ $? -ne 0 ]; then
             exit_process $ERROR_FTP_TRANSFER_FAIL        
         fi                   
     done 
@@ -370,9 +449,10 @@
     while true
     do
         file_name=$(find -name \*"$1" | xargs ls -lthr | head -1 | awk '{print $9}')        
-        if [ -n "$file_name" ]; then        
-            mv $file_name $MTK_LOG_BUFF_PATH
-            do_backup_mtk_log $1    
+        if [ -n "$file_name" ]; then        		   
+            mv $file_name $MTK_LOG_BUFF_PATH${log_prefix}_${CUR_LOG_NUM}.muxz             
+            do_backup_mtk_log "muxz"   
+            do_cur_log_num_plus
         else
             break;
         fi    
@@ -380,7 +460,7 @@
 }
 
 function do_dbg_log_transfer()
-{      
+{   
     if [ $ftp_server = "127.0.0.1" ]; then
         return 
     fi       
@@ -490,6 +570,69 @@
     fi      
 }
 
+function delete_beyond_log()
+{
+	post_fix=${UP_LOAD_POSTFIX[$1]}	 
+	
+	for j in `seq $2 $3`
+	do 
+		ftp_delete_file ${log_prefix}_${j}.${post_fix}
+	done
+}
+
+function get_init_cur_log_num()
+{
+    q_cur_num_uci=${Q_CUR_NUM_UCI[$1]}	
+        
+    if [ ! -z $q_cur_num_uci ]; then
+        q_uci_value=`uci get $q_cur_num_uci`
+        if [ -z $q_uci_value ];
+        then
+            uci set ${q_cur_num_uci}=${CUR_LOG_NUM}
+        else
+            CUR_LOG_NUM=$(($q_uci_value))
+        fi	
+        if [ $CUR_LOG_NUM -gt $log_index_max ]; then
+		    delete_beyond_log $1 $log_index_max  $(($CUR_LOG_NUM-1))
+            CUR_LOG_NUM=$log_index_max
+            uci set ${q_cur_num_uci}=${CUR_LOG_NUM}
+        fi
+    fi	
+}
+
+function get_single_file_size()
+{
+    single_file_size=${SINGLE_FILE_SIZE[$1]}  		
+	 
+    if [ $1 -eq 1 ]; then
+		q_uci_value=`uci get mdlog.property.md_log_size`
+		if [ ! -z $q_uci_value ];
+		then
+		    single_file_size=$(($(($q_uci_value))*10))			
+		fi
+    fi			
+}
+
+function get_init_max_log_num()
+{
+    init_log_max_size=${INIT_LOG_MAX_SIZE[$1]}    		
+    q_max_size_uci=${Q_MAX_SIZE_UCI[$1]}	
+	
+	get_single_file_size $1
+        
+    if [ ! -z $q_max_size_uci ]; then
+        q_uci_value=`uci get $q_max_size_uci`
+        if [ -z $q_uci_value ];
+        then
+            uci set ${q_max_size_uci}=$init_log_max_size
+            log_num_max=$(($init_log_max_size*20/single_file_size))
+        else
+            log_num_max=$(($(($q_uci_value))*20/single_file_size))
+        fi	
+    fi
+    log_index_max=$(($log_num_max-1)) 	
+}
+
 function loop_prepare()
 {
     if [ $1 -eq 1 ]; then
@@ -500,9 +643,21 @@
         fi
         if [ ! -d ${MTK_LOG_BUFF_PATH} ]; then                    
             mkdir -p ${MTK_LOG_BUFF_PATH}
-        fi    
+        fi      
     fi     
     target_dir=${TARGET_DIR[$1]}	
+    log_prefix=${LOG_PREFIX[$1]}   
+
+	
+    get_init_max_log_num $1
+    get_init_cur_log_num $1
+	
+    if [ ! -z $target_dir ]; then
+        # 3 times for guarrantee
+        curl -u $ftp_user_name:$ftp_password -XPWD -p -e ftp://$ftp_server:$ftp_port/$target_dir --ftp-create-dirs ftp://$ftp_server:$ftp_port/$target_dir/ 1>/dev/null
+        curl -u $ftp_user_name:$ftp_password -XPWD -p -e ftp://$ftp_server:$ftp_port/$target_dir --ftp-create-dirs ftp://$ftp_server:$ftp_port/$target_dir/ 1>/dev/null	
+        curl -u $ftp_user_name:$ftp_password -XPWD -p -e ftp://$ftp_server:$ftp_port/$target_dir --ftp-create-dirs ftp://$ftp_server:$ftp_port/$target_dir/ 1>/dev/null
+    fi
 }
 
 
@@ -516,19 +671,22 @@
         exit_process $((ERROR_IS_IN_TRANSFER_LOG+${index}*5+1))    
     fi
     
-    get_ftp_param $# $2 $3 $4 $5
-    
-    
-    transfer_log_tag[$index]=1
-    loop_prepare $index
+    if [ $index -ne 2 ]; then
+	    get_ftp_param $# $2 $3 $4 $5        
+        transfer_log_tag[$index]=1
+        loop_prepare $index
+    fi		
 
-    echo_debug_info "transfer ${log_print[$index]}"     
+    
     
     touch ${LOG_IS_IN_TRANSFER[${index}]}
     
+    echo_debug_info "transfer ${log_print[$index]}"     
+    
     if [ $index -eq 2 ]; then    
         echo_debug_info "log transfer param_num $# param $* start"    
         show_relative_tag
+        return 
     fi    
 
     do_loop ${index} ${loop_time[$index]}           
@@ -544,14 +702,42 @@
     if [ $index -eq 1 ]; then
         echo 7 | emdlogger_ctrl
     elif [ $index -eq 2 ]; then
-	    show_relative_tag
+        show_relative_tag
         echo_debug_info "log transfer param_num $# param $* complete" 
+        rm ${LOG_IS_IN_TRANSFER[$index]}   
+        return 
     fi
     
     stop_log_transfer  $index
 }
 
+function init_md_log()
+{
+    if [ ! -d $MTK_LOG_MOUNT_PATH ];
+    then 
+        echo_debug_info "init md log start"
+        mkdir -p $MTK_LOG_MOUNT_PATH
+        echo 7 | emdlogger_ctrl
+        rm -rf $MTK_LOG_PATH*
+        mount -o bind $MTK_LOG_MOUNT_PATH $MTK_LOG_PATH
+        rm -rf $MTK_LOG_PATH*
+        uci set mdlog.property.md_cyclic_size='36'
+        uci set mdlog.property.md_log_size=$((${SINGLE_FILE_SIZE[1]}/10))
+        uci commit
+        killall emdlogger1	
+        echo_debug_info "init md log end"
+    fi		
+}
 
+function init_log_transfer_ui()
+{
+    index=$2
+    if [ $index -eq 1 ]; then
+        #Æô¶¯debug
+        trigger_log_transfer 3  
+        init_md_log
+    fi	
+}
 
 echo_debug_info "log transfer param_num $# param $* start"
 
@@ -562,6 +748,9 @@
   101|102|103)     
     stop_log_transfer_ui "$@"
     ;;    
+  0)
+    init_log_transfer_ui "$@"
+	;;
   *)    
     exit_process $ERROR_PARAM
     ;;