#!/bin/bash
# count number of files in your PATH
my_path=`echo $PATH | sed 's/:/ /g'`
count=0
for directory in ${my_path}
do
check=`ls ${directory}`
echo ${check}
for item in ${check}
do
count=$[ $count + 1 ]
done
echo "$directory - $count"
count=0
done
2. 獲取占用磁盤最大的幾個(gè)文件
#!/bin/bash
CHECK_DIRECTORIES="/var/log /home"
DATE=$(date '+%m%d%y')
#Make report file Std Output
exec > disk_space_${DATE}.rpt
#Report header for while report
echo "十大磁盤空間使用情況"
echo "for $CHECK_DIRECTORIES Directories"
for DIR_CHECK in ${CHECK_DIRECTORIES}
do
echo ""
echo "The $DIR_CHECK Directory:"
du -S ${DIR_CHECK} 2>/dev/null |
sort -rn |
sed '{11,$D; =}' |
sed 'N; s/\n/ /' |
gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
done
3. 獲取磁盤占用率并進(jìn)行預(yù)警
#! /bin/bash
requestId=$(cat /proc/sys/kernel/random/uuid)
echo ${requestId}
email_logfile="/data/ksyun/monitor/disk/diskdetail.txt";
if [[ ! -d "/data/ksyun/monitor/disk/" ]]; then
mkdir -p '/data/ksyun/monitor/disk/'
fi
if [[ ! -e "$email_logfile" ]]; then
touch "$email_logfile"
fi
cat /dev/null > ${email_logfile};
local_ip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:");
receiver="email地址"
SendMail(){
ip=$1;
disk_percent=$2;
content='ip='${ip}',disk_percent='${disk_percent}'%';
#echo $content
contentStr=${content//\"/\\\"}
echo ${contentStr}
now_time=$(date "+%Y%m%d-%H:%M:%S")
subject='測(cè)試環(huán)境('${local_ip}')磁盤監(jiān)控告警'${now_time};
curl --location --request POST 'http://公司發(fā)郵件地址/sendmail' \
--header 'Accept: application/json' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'mails='${receiver}'' \
--data-urlencode 'subject='${subject}'' \
--data-urlencode 'content='${contentStr}''
}
for d in `df -P | grep /dev | awk '{print $5}' | sed 's/%//g'`
do
if [[ $f1fpzjd -gt 80 ]]; then
df -h >>${email_logfile};
SendMail ${local_ip} $f1dzjv1;
exit 0;
fi
done