#!/bin/bash

now_data=`date -d "today" +%Y-%m-%d`
start_log() {
    for pod_name in `kubectl get pod -n edoc2 | grep -v NAME | awk -F " " '{print $1}'`;do
        echo $pod_name
        file_path="./logs/${now_data}/$pod_name.log"
        if [ -f $file_path ];then
            echo "log file have been exist!"
            pid=`ps -aux | grep "${pod_name}" | grep -v grep | grep -v polkitd | awk -F " " '{print $2}'`
            if [ "${pid_list}" == "" ];then
                usleep  
            else
                kill -9 $pid
            fi
            #nohup kubectl logs $pod_name -n edoc2 -f --tail=10 >> logs/"${now_data}"/$pod_name.log > /dev/null 2>&1 &
            nohup kubectl logs -f --tail=10 $pod_name -n edoc2 >> logs/"${now_data}"/$pod_name.log > /dev/null 2>&1 &
            #sleep 1
        else
            echo "log file don't exist!"
            kubectl logs $pod_name -n edoc2 -f --tail=100 > logs/"${now_data}"/$pod_name.log
            #sleep 1
        fi
    done
}

stop_log() {
    pid_list=`ps -aux | grep logs | grep -v grep | grep -v polkitd | awk -F " " '{print $2}'`
    echo ${pid_list}
    if [ "${pid_list}" == "" ];then
        echo "pid not exist!"
    else
        for pod_pid in $pid_list;do
            kill -9 $pod_pid
            echo $?
            echo $pod_pid
        done
    fi
}

Action=$1
case $Action in 
    1|start|START)
        echo "begin beat logs ........."
        start_log
        ;;
    2|stop|STOP)
        echo "begin kill logs ..........."
        stop_log
        ;;
    3|restart|RESTART)
        echo "begin restart logs ........"
        stop_log
        sleep 3
        start_log
        ;;
    4|daemonize|DAEMONIZE)
        while true
        do
            now_data=`date -d "today" +%Y-%m-%d`
            dir_path="./logs/${now_data}/"
            echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            echo $now_data
            echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            echo "##### begin collect logs to localdir ..........................."
            echo "===================================================="
            if [ -d $dir_path ];then
                usleep
            else
                echo "##### $dir_path don't exist! ########################"
                mkdir -p ./logs/"${now_data}"
            fi
            for i in `seq 1 300`;do
                #stop_log
                sleep 1
                #echo $i
                #str ii="$i"
                if [ $i == 30 ] || [ $i == 60 ] || [ $i == 120 ] || [ $i == 180 ] || [ $i == 240 ] || [ $i == 300 ];then
                    sed -i "s/\*\@\*/\\n/g" ./logs/"${now_data}"/*.log
                fi
                if [ $i == 15 ];then
                    log_list=`find ./logs/"${now_data}"/ -name *.log | awk -F "logs/" '{print $2}'`
                    if [ "${log_list}" == "" ];then
                        #echo "##### log_list is nothing should start log! ##################"
                        start_log
                        sleep 1
                    else
                        stop_log
                        for log_name in $log_list;do
                            log_size=`du -sh ./logs/"${log_name}" | awk -F " " '{print $1}'`
                            unit=${log_size: -1}
                            log_size=${log_size%?}
                            #echo $log_size
                            #echo $unit
                            if [ "${unit}" == "M" ] && [ "${log_size}" -gt 200 ];then
                                echo "##### need tar file name is""${log_name}"
                                tar -Jcvf ./logs/"${log_name}".tar.xz ./logs/"${log_name}"
                                sleep 1
                                rm -rf ./logs/"${log_name}"
                            fi
                            if [ "${unit}" == "G" ] && [ "${log_size}" -gt 1 ];then
                                echo "##### need tar file name is""${log_name}"
                                tar -Jcvf ./logs/"${log_name}".tar.xz ./logs/"${log_name}"
                                sleep 1
                                rm -rf ./logs/"${log_name}"
                            fi
                            tmp_date=`date -d"-3 day" +%Y-%m-%d`
                            #echo $tmp_date
                            rm -rf ./logs/${tmp_date}
                            #echo "./logs/${tmp_date}"
                            #echo "##### porcee have been delete the ${tmp_date} of dir"
                        done
                    fi
                    start_log
                else
                    usleep
                fi
            done
            end_data=`date -d "yesterday" +%Y-%m-%d`
            echo "##### ${end_data}"
            echo "===================================================="
            #sleep 300
        done
        ;;
    *)
        echo "输入操作不存在 .........."
        #exit;
esac
