Skip to content

Instantly share code, notes, and snippets.

@caoxudong
Last active September 15, 2018 13:44
Show Gist options
  • Save caoxudong/3c2617abb135537635041888dcbc3ceb to your computer and use it in GitHub Desktop.
Save caoxudong/3c2617abb135537635041888dcbc3ceb to your computer and use it in GitHub Desktop.
linux basic op scripts

线上常用脚本

删除大小为0的文件

find . -name "*" -type f -size 0c | xargs -n 1 rm -f

swap使用量top10

for file in /proc/*/status ; do awk '/VmSwap|^Pid/{printf $2 " " $3}END{ print ""}' $file; done | awk -F ' ' 'BEGIN { jpsOutputFile="/tmp/caoxudong.stat.swap.txt"; jpsCmd="jps >"jpsOutputFile; system(jpsCmd); while (getline line < jpsOutputFile) {split(line, idname, " "); apps[idname[1]]=idname[2];}}    {split($0, idcountunit, " "); result[idcountunit[1]]=apps[idcountunit[1]]" "idcountunit[2]" "idcountunit[3];}    END{for(id in result){print id, result[id]}}' | sort -k 3 -n -r | head

实际内存占用top10

ps -e -o 'pid,euser,ruser,suser,fuser,rsz,vsz,comm,args' | awk '{if(FNR != 1){print $0}}' | awk -F ' ' 'BEGIN { jpsOutputFile="/tmp/caoxudong.stat.rsz.txt"; jpsCmd="jps >"jpsOutputFile; system(jpsCmd); while (getline line < jpsOutputFile) {split(line, idname, " "); apps[idname[1]]=idname[2];}}    {split($0, mess, " "); result[mess[1]]=apps[mess[1]]" "mess[6];}    END{for(id in result){print id, result[id]}}' | sort -k 3 -n -r | head

进程使用的线程数top10

ps -eLf | tail -n +2 | awk -F ' ' '{threads_count[$2]++;} END{for(pid in threads_count){print pid,threads_count[pid];}}' | awk -F ' ' 'BEGIN { jpsOutputFile="/tmp/caoxudong.stat.threadCount.txt"; jpsCmd="jps >"jpsOutputFile; system(jpsCmd); while (getline line < jpsOutputFile) {split(line, idname, " "); apps[idname[1]]=idname[2];}}    {split($0, idcount, " "); result[idcount[1]]=apps[idcount[1]]" "idcount[2];}    END{for(id in result){print id, result[id]}}' | sort -k3 -n  -r | head

access_log统计

统计IP访问量TOP10

awk -F ' ' '{split($1, ips, ",");ip_stat[ips[1]]++} END{for(ip in ip_stat){print ip, ip_stat[ip];}}' manhattan-batman-access.log.2017-11-07 | sort -k 2 -r -n | head

统计uri访问量TOP10

awk -F ' ' '{split($8, uriAndQueryString, "?");uri_stat[substr($7, 2) " " uriAndQueryString[1]]++} END{for(uri in uri_stat){print uri, uri_stat[uri];}}' manhattan-batman-access.log.2017-11-07 | sort -k 3 -r -n | head

uri访问平均耗时TOP10

awk -F ' ' '{split($8, uriAndQueryString, "?");uri = substr($7, 2) " " uriAndQueryString[1]; uri_count_stat[uri]++; uri_process_time_stat[uri] += $NF;} END{for(uri in uri_process_time_stat){print uri, uri_process_time_stat[uri] / uri_count_stat[uri];}}' manhattan-batman-access.log.2017-11-07 | sort -k 3 -r -n | head

uri访问耗时TOP10

awk -F ' ' '{print $5, $6, $7, $8, $9, $10, $11, $NF}' manhattan-batman-access.log.2017-11-07 | sort -k 8 -r -n | head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment