1.CPU占用率高的进程
参数说明: topk 前 k 的进程、samplingTime 采样时间(秒)、采样间隔时间(秒)
#!/bin/bashTOPK={{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++))dops -eocomm,pcpu | tail -n +2 >> $TEMP_FILE_PREFIX.$$sleep $INTERVALdoneechoecho 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 $TOPKrm $TEMP_FILE_PREFIX.$$
2.查看目录占用磁盘空间大小
directory 需要查看的目录路径
#!/bin/bashdu -sh {{directory}}
3.显示 linux 内核版本信息
#!/bin/bashuname -a
4.显示 linux 主机名
#!/bin/bashhostname
5.显示僵尸进程
#!/bin/bashprocesses=$(ps ax -o user,pid,ppid,pgid,args,stat,start,time)zombies=$(echo -e "${processes}" | grep -E "\s(Z|z|Z.*)\s")if [ $? == 1 ]; thenecho "no zombie processes exists on machine"elseecho -e "${processes}" | head -1echo "$zombies"fi
6.清理数据
#!/bin/bashfunction deletefiles() {if [ ! -d $2 ]; thenecho "The specified directory("$2") is not exist."returnfiexpiredTimeUnit=${1: -1}expiredTimeValue=${1:0:-1}if [ "$expiredTimeUnit" = "d" ]; thenexpiredTime=$(($expiredTimeValue * 24 * 60 * 60))elif [ "$expiredTimeUnit" = "h" ]; thenexpiredTime=$(($expiredTimeValue * 60 * 60))elif [ "$expiredTimeUnit" = "m" ]; thenexpiredTime=$(($expiredTimeValue * 60))elseecho "The unit("$expiredTimeUnit") of file age is not supported."returnfifor file in $(find $2 -type f -name "$3"); dolocal currentDate=$(date +%s)local modifyDate=$(stat -c %Y $file)local existTime=$(($currentDate - $modifyDate))if [ $existTime -gt $expiredTime ]; thenecho "File cleaning succeeded,path:"$file"."rm -f $filefidone}deletefiles {{delayTime}} {{filePath}} "{{matchPattern}}"
参数说明:
delayTime 文件的有效时间。如 7d(代表7天),1h(代表1小时),5m(代表5分钟),默认是7d
filePath 清理文件路径。如:/root/log/
matchPattern 清理文件匹配格式,如 *.log。 支持正则匹配
