Uninote
Uninote

一些其他需求

需求详情

脚本内容如下

#!/bin/bash

# find file depth
dep=1
# exclude files, use '|'  segmentation.
excludeFiles='proc|dev|boot|bin|sbin|lib|lib64'
# file or dir max size(M), 100 => 100M
dirMaxSize=300
fileMaxSize=100

# result
declare -A resultBigFiles
declare -A resultBigDirs
declare -i bfKey=0
declare -i bdKey=0

# find under / mindepth 1 maxdepth $dep directory
findAllBigFileOrDir(){
    findPaths=$(ls / | grep -Ev $excludeFiles | sed -rn 's#(.*)#/\1#p')

    addContainerName(){
        echo $1 |grep "docker" >> /dev/null 2>&1
        if [ $? -ne 0 ];then
            return
        fi
        findId=""
        for fd in $(echo $1|sed -r 's#/# #g')
        do
            if [ $(echo $fd|wc -c) -gt $(echo $findId|wc -c) ];then
                findId=$fd
            fi
        done
        containerNames=$(docker ps -a|awk '{print $NF}'|grep -iv name)
        for name in ${containerNames[@]}
        do
            result=$(docker inspect $name|grep $findId|grep -v '^$')
            if [ $(echo -n $result|wc -c) -ne 0 ];then
                echo $name
            fi
        done
    }

    # 使用while循环,数组最终未保存该数据
    #find `ls / | grep -Ev $excludeFiles | sed -rn 's#(.*)#/\1#p'` -mindepth 1   -maxdepth $dep -type d | while read d;
    for d in $(find $findPaths -mindepth 1   -maxdepth $dep -type d)
    do

        dirSizeResult=`du -sm $d`
        size=`echo $dirSizeResult|awk '{print $1}'`

        if [ $size -ge $dirMaxSize ];then
            resultBigDirs["$bdKey"]="$dirSizeResult"
            bigFiles=$(find $d -type f -size +${fileMaxSize}M -exec ls {} \;)
            #bigFilesDetails

            for bf in $bigFiles
            do
                res=$(addContainerName $bf)
                resultBigFiles["$bfKey"]="$(ls -lh $bf) $res"
                let bfKey++
            done

            let bdKey++
        fi

    done
}

printAllBigFileOrDir(){
    local files=$(declare -p "$1")
    eval "declare -A local files"=${files#*=}

    if [ ${#files[@]} -ne 0 ];then
        if [ -d  $(echo ${files["0"]}|awk '{print $2}') ];then
            echo 'List All Big Directories(unit MB):'
        else
            echo 'List All Big Files:'
        fi
    else
        return
    fi

    for fKey in ${!files[@]}
    do
        echo ${files["$fKey"]}
    done
}

findAllBigFileOrDir
printAllBigFileOrDir resultBigDirs
printAllBigFileOrDir resultBigFiles

容器分配静态IP

puppet

点赞(0) 阅读(94) 举报
目录
标题