• Shell脚本:向磁盘中批量写入数据


    一、关于本文

    工作要做的监控系统需要监控磁盘空间的使用率并报警。在测试这个功能的时候需要模拟两个场景:一是磁盘空间不断增长超过设定的阈值时,需要触发报警机制;二是磁盘空间降落到低于报警阈值的时候,不再进行报警。为了测试这两个场景,我写了下面三个脚本:

    1)initializer.sh:创建目录TestDir,并创建一个大文件template

    2)duplicator.sh:不断复制文件template,直到磁盘空间使用率超过输入的参数为止

    3)cleaner.sh:清除前面两个脚本留下的痕迹,即删除目录TestDir

    二、initializer.sh

    最开始创建一个大文件的方式是通过Shell向文件中写入字符并复制的方式,代码如下(initializer.sh.old):

    #!/bin/sh
    
    #本脚本用于初始化工作,创建文件夹TestDir并写入一个大小为100M的目录
    
    #创建文件TestDir
    if [ -x "./TestDir" ]; then
        rm -rf TestDir
    fi
    mkdir TestDir
    cd TestDir
    mkdir Template
    cd Template
    
    touch template.txt
    
    #制作大小为100K的文件template.txt
    string=""
    repetend="012345678901234|"
    for((i=1;i<6400;i++))
    do
        string=$string$repetend
    done
    echo $string >> template.txt
    
    #复制1000个该文件
    i=0
    while [ true ]; do
        if [ "$i" -gt 1020 ]; then
            break
        fi
        cp template.txt $i
        ((i++))
    done
    
    echo "文件制造完毕,空间占用信息如下"
    pwd .
    du -sh .
    
    cd ../..
    
    exit 0

    这种方式效率比较低,并且代码比较长,后来改用dd命令实现功能(initializer.sh):

    #!/bin/sh
    
    #本脚本用于初始化工作,创建文件夹TestDir并写入一个大小为100M的目录
    
    #创建文件TestDir
    if [ -x "./TestDir" ]; then
        rm -rf TestDir
    fi
    mkdir TestDir
    cd TestDir
    
    dd if=/dev/zero of=template bs=1M count=1024
    
    pwd .
    du -sh .
    
    cd ..
    
    exit 0

    这个脚本创建了TestDir目录,并在里面写入了一个1.1GB的文件template

    三、duplicator.sh

    脚本duplicator.sh接受一个5-95的数字,作为阈值。这个脚本不断复制initializer.sh创建的template文件,直到里面指定的磁盘空间使用率超过输入的阈值时,才停止运行。

    #!/bin/sh
    
    #运行本脚本前请先运行脚本 initializer.sh
    #本脚本用于不断复制文件,直到给出的参数阈值超过当前磁盘空间利用率
    #输入参数:磁盘空间使用率阈值
    
    #函数:打印脚本使用说明
    function usage()
    {
        echo "Usage: ./duplicator [threshold]"
        echo "threshold is an integer in the range of [1,99]"
        echo "*Run initializer.sh before run this script"
        exit 0
    }
    
    #脚本有且只有一个输入
    if [ "$#" -ne 1 ]; then
        echo "脚本应有且只有一个输入"
        usage
    fi
    
    #脚本的输入必须为5-95之间的正整数
    threshold=`echo $1 | bc`
    if [ "$threshold" -lt 5 -o "$threshold" -gt 95 ]; then
        echo "脚本的输入必须为5-95之间的正整数"
        usage
    fi
    
    #目录TestDir必须存在
    if [ ! -d ./TestDir ]; then
        echo "缺少目录 TestDir"
        usage
    fi
    
    #文件TestDir/template必须存在
    if [ ! -f ./TestDir/template ]; then
        echo "缺少文件 TestDir/template"
        usage
    fi
    
    cd TestDir
    
    #复制文件,超过输入的阈值为止
    i=0
    while [ true ]; do
        cur=`df -h | grep /dev/sda3 | awk '{printf substr($5,1,length($5)-1)}'`
        echo "Current usage: $cur | Object usage: $threshold"
        if [ "$cur" -gt "$threshold" ]; then
            break;
        fi
        cp template $i
        echo " $i Duplication complete!"
        ((i++))
    done
    
    cd .. #TestDir
    
    echo "Script finished!"
    
    exit 0

    四、cleaner.sh

    这个脚本用于清除前两个脚本在系统中留下的痕迹

    #!/bin/sh
    
    #本脚本用于清空脚本initializer.sh和duplicator.sh留下的痕迹
    
    #检查文件是否存在
    if [ ! -x "./TestDir" ]; then
        echo "文件 ./TestDir 不存在,无需清除"
        exit 0
    fi
    
    #用户确认后清除文件
    echo "真的要清除全部数据吗? (y/n)"
    read input
    case "$input" in
        y* | Y* )
            rm -rf ./TestDir
            echo "数据删除完毕";;
        n* | N* )
            echo "放弃删除数据";;
        * )
            echo "输入未识别";;
    esac
    
    exit 0

    五、调用效果截图

  • 相关阅读:
    navicat 创建查询失败 can not create file
    使用Themeleaf时, HTML内嵌的JS代码需要注意< 和 >的问题
    window下查杀占用端口的进程
    Spring MVC的Rest URL 被错误解析成jsp, 导致404错误(XML方式下@Controller和@RestController需要配置<mvc:annotation-driving/>)
    一个本地DNS解析和mysql授权导致的Mysq连接失败问题(Access denied for user 'loan'@'kfcsdb1' (using password: YES))
    taglib报错The content of element type "taglib" must match "(tlib-version,...)
    cvc-complex-type.2.4.a: Invalid content was found starting with element 'display-name'
    在eclipse中运行spring web application时的异常: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    Spring3升级到Spring4时, 运行时出现找不到MappingJacksonHttpMessageConverter的情况
    如何在Spring MVC Test中避免”Circular view path” 异常
  • 原文地址:https://www.cnblogs.com/wushuaishuai/p/8709879.html
Copyright © 2020-2023  润新知