1.CPU占用率高的进程
参数说明: topk 前 k 的进程、samplingTime 采样时间(秒)、采样间隔时间(秒)
#!/bin/bash
TOPK={{topk}}
SECS={{samplingTime}}
INTERVAL={{interval}}
STEPS=$(( $SECS / $INTERVAL ))
TEMP_FILE_PREFIX="/tmp/tat_public_cpu_usage"
echo Watching CPU usage...
for((i=0;i<$STEPS;i++))
do
ps -eocomm,pcpu | tail -n +2 >> $TEMP_FILE_PREFIX.$$
sleep $INTERVAL
done
echo
echo CPU eaters :
cat $TEMP_FILE_PREFIX.$$ | \
awk '
{ process[$1]+=$2;}
END{
for(i in process) {
printf("%-20s %s\n",i, process[i]) ;
}
}' | sort -nrk 2 | head -n $TOPK
rm $TEMP_FILE_PREFIX.$$
2.查看目录占用磁盘空间大小
directory 需要查看的目录路径
#!/bin/bash
du -sh {{directory}}
3.显示 linux 内核版本信息
#!/bin/bash
uname -a
4.显示 linux 主机名
#!/bin/bash
hostname
5.显示僵尸进程
#!/bin/bash
processes=$(ps ax -o user,pid,ppid,pgid,args,stat,start,time)
zombies=$(echo -e "${processes}" | grep -E "\s(Z|z|Z.*)\s")
if [ $? == 1 ]; then
echo "no zombie processes exists on machine"
else
echo -e "${processes}" | head -1
echo "$zombies"
fi
6.清理数据
#!/bin/bash
function deletefiles() {
if [ ! -d $2 ]; then
echo "The specified directory("$2") is not exist."
return
fi
expiredTimeUnit=${1: -1}
expiredTimeValue=${1:0:-1}
if [ "$expiredTimeUnit" = "d" ]; then
expiredTime=$(($expiredTimeValue * 24 * 60 * 60))
elif [ "$expiredTimeUnit" = "h" ]; then
expiredTime=$(($expiredTimeValue * 60 * 60))
elif [ "$expiredTimeUnit" = "m" ]; then
expiredTime=$(($expiredTimeValue * 60))
else
echo "The unit("$expiredTimeUnit") of file age is not supported."
return
fi
for file in $(find $2 -type f -name "$3"); do
local currentDate=$(date +%s)
local modifyDate=$(stat -c %Y $file)
local existTime=$(($currentDate - $modifyDate))
if [ $existTime -gt $expiredTime ]; then
echo "File cleaning succeeded,path:"$file"."
rm -f $file
fi
done
}
deletefiles {{delayTime}} {{filePath}} "{{matchPattern}}"
参数说明:
delayTime 文件的有效时间。如 7d(代表7天),1h(代表1小时),5m(代表5分钟),默认是7d
filePath 清理文件路径。如:/root/log/
matchPattern 清理文件匹配格式,如 *.log。 支持正则匹配