作用:监控或消耗内存资源, 指定需要消耗到的百分比
实现:通过在内存中写入一定大小的文件实现
演示:
直接回车,查看当前内存的百分比
输入欲消耗的内存百分比
代码专区:
#/bin/bash # LY# ------------------# Copyright 2014.11.25 LingYi (lydygly@163.com) QQ:1519952564get_file_num(){ big_num=$1 small_num=$2 NUM=$3 file_num_perc=$( echo "${big_num}-${small_num}" | bc ) file_num_moment=$( echo "$( echo "0$(echo "scale=2; ${file_num_perc}/100" | bc)" )*${NUM}" | bc ) file_num_one=$( echo ${file_num_moment} | awk -F. '{print $1}' ) file_num_two=$( echo ${file_num_moment} | awk -F. '{print $2}' ) [[ ${file_num_two:0:1} -eq 0 ]] && file_num_two=${file_num_two:1:1} [[ ${file_num_two} -ge 50 ]] && let file_num_one++ echo ${file_num_one}}clean_up(){ umount ${memory_dir} && rm -fr ${memory_dir} rm -fr .test_memory_file_sum.file}if [[ ! -f /usr/bin/bc ]]; then echo "Try to install bc tool ... " yum install -y bc >/dev/null 2>&1 if [[ $? -ne 0 ]]; then echo -e "\033[1;31mTool: \"bc\" install failed !!\033[0m" exit 1 fifitrap 'clean_up &>/dev/null; tput cnorm; echo -e "\033[0m"; exit 0' 2echo -e "\033[1;32mInput the percentage that you want, if empty then only monitor."echo -e "\033[1;32m\"Ctrl + C\" to exit."echo -ne "\033[1;34mInput the percentage[ "echo -ne "\033[1;31meg:50 "echo -ne "\033[1;34m]: "read keep_busy_percif [[ $keep_busy_perc == "q" ]] || [[ $keep_busy_perc == "Q" ]]; then exit 0; fimemory_dir=".memory.$(date +"%Y%m%d%H%M%S")" mkdir -p ${memory_dir}if [[ $? -ne 0 ]]; then echo "Create memory test dir failed !!"; exit 1; fimemory_sum=$( free -m | awk '{print $2}'|sed -n '2p' )mount -t tmpfs -o size=${memory_sum}M tmpfs ${memory_dir}tput civiswhile :; do memory_used=$( free -m | awk '{print $3}' | sed -n '2p' ) memory_free=$( echo "${memory_sum}-${memory_used}" | bc ) memory_busy_perc=$( echo "scale=2; $( echo "scale=4; ${memory_used}/${memory_sum}*100" | bc)/1" | bc ) tput cup 00; tput ed echo -e "\033[1;32mmemory sum :\033[1;34m$(printf "%-8s" " ${memory_sum}")\033[1;32mM" echo -e "\033[1;32mmemory used:\033[1;35m$(printf "%-8s" " ${memory_used}")\033[1;32mM" echo -e "\033[1;32mmemory free:\033[1;36m$(printf "%-8s" " ${memory_free}")\033[1;32mM" echo -e "\033[1;32mmemory perc:\033[1;31m$(printf "%-8s" " ${memory_busy_perc}")\033[1;32m%" echo -ne "\033[0m" one=$( echo ${memory_busy_perc} | awk -F. '{print $1}') two=$( echo ${memory_busy_perc} | awk -F. '{print $2}') [[ ${two:0:1} -eq 0 ]] && two=${two:1:1} [[ ${two} -ge 50 ]] && let one++ if [[ -z $( ls ${memory_dir} ) ]]; then if [[ ${keep_busy_perc} -gt ${one} ]]; then echo -ne "\033[1;31mInitializating ...\033[0m" file_sum=$( get_file_num ${keep_busy_perc} ${one} ${memory_sum} ) echo ${file_sum} >.test_memory_file_sum.file for ((i=1; i<=file_sum; i++)); do dd if=/dev/zero of=${memory_dir}/${i} bs=1M count=1 & done >/dev/null 2>&1 wait fi else if [[ ${keep_busy_perc} -gt ${one} ]]; then file_num=$( get_file_num ${keep_busy_perc} ${one} ${memory_sum} ) file_sum=$(echo "`cat .test_memory_file_sum.file`+${file_num}" | bc) echo ${file_sum} >.test_memory_file_sum.file for ((i=file_num+1; i<=${file_sum}; i++)); do dd if=/dev/zero of=${memory_dir}/${i} bs=1M count=1 & done >/dev/null 2>&1 wait fi if [[ ${keep_busy_perc} -lt ${one} ]]; then file_num=$( get_file_num ${one} ${keep_busy_perc} ${memory_sum} ) if [[ ${file_num} -gt $(cat .test_memory_file_sum.file) ]]; then rm -fr ${memory_dir}/* else for ((i=0; i
附件下载源代码