awk, tr 조합
728x90

특정 폴더 하위의 용량 체크 후 상위 3개를 뽑는 쉘 스크립트이다.

#!/bin/sh

BACK_SERVER_IP_LIST=("IP1" "IP2" "IP3" "IP4")
FRONT_SERVER_IP_LIST=("IP1" "IP2" "IP3" "IP4")

BACKEND='{'
TOTAL=''

# SERVER 당 du -sh 결과 (상위 3개)
for value in "${BACK_SERVER_IP_LIST[@]}";
do

        tomcat7_DU=$(echo <password> | sudo -S sshpass -p '<password>' ssh '<user>'@$value 'echo <password> | sudo -S du -sh /usr/local/tomcat7/* 2>/dev/null | sort -hr | awk "NR >=0 && NR <=3" | tr "\n" "|"')

        backend_DU=$(echo <password> | sudo -S sshpass -p '<password>' ssh '<user>'@$value 'echo <password> | sudo -S du -sh /var/log/backend/* 2>/dev/null | sort -hr | awk "NR >=0 && NR <=3" | tr "\n" "|"')
        tomcat7_array=$(echo $tomcat7_DU| tr "|" "\n")

        backend_array=$(echo $backend_DU| tr "|" "\n")

        RESULT='"'$value'":[{"tomcat7":"'${tomcat7_array[@]}'","backend":"'${backend_array[@]}'"}]'
        if [ $value == 'IP1' ];
        then
                BACKEND+=$RESULT
        else
                BACKEND+=,$RESULT
        fi

done
BACKEND+='}'

FRONTEND='{'

for value in "${FRONT_SERVER_IP_LIST[@]}";
do
        tomcat7_DU=$(echo <password> | sudo -S sshpass -p '<password>' ssh '<user>'@$value 'echo <password> | sudo -S du -sh /usr/local/tomcat7/* 2>/dev/null | sort -hr | awk "NR >=0 && NR <=3" | tr "\n" "|"')
        frontend_DU=$(echo <password> | sudo -S sshpass -p '<password>' ssh '<user>'@$value 'echo <password> | sudo -S du -sh /var/log/frontend/* 2>/dev/null | sort -hr | awk "NR >=0 && NR <=3" | tr "\n" "|"')
        tomcat7_array=$(echo $tomcat7_DU| tr "|" "\n")

        frontend_array=$(echo $frontend_DU| tr "|" "\n")
        RESULT='"'$value'":[{"tomcat7":"'${tomcat7_array[@]}'","frontend":"'${frontend_array[@]}'"}]'
        if [ $value == '<target IP>' ];
        then
                FRONTEND+=$RESULT

        else
                FRONTEND+=,$RESULT
        fi
done

FRONTEND+='}'

TOTAL+='{"frontend":['$FRONTEND'],"backend":['$backend']}'

echo $TOTAL | jq '.' > result.json
728x90

'OS > Linux' 카테고리의 다른 글

OS 입출력  (0) 2023.05.16
Linux 스케줄러  (0) 2022.11.04
Linux Signal  (0) 2022.08.10
리눅스 명령어 정리 중..  (0) 2022.07.25
Linux (CentOS 6) 서비스 등록  (0) 2022.07.25