日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Shell常用服務(wù)器日志分析命令總結(jié)_linux shell

作者:CodeCloud ? 更新時(shí)間: 2022-08-03 編程語(yǔ)言

1、查看有多少個(gè)IP訪問(wèn)

awk?'{print?$1}'?log_file|sort|uniq|wc?-l

2、查看某一個(gè)頁(yè)面被訪問(wèn)的次數(shù)

grep?"/index.php"?log_file?|?wc?-l

3、查看每一個(gè)IP訪問(wèn)了多少個(gè)頁(yè)面

awk?'{++S[$1]}?END?{for?(a?in?S)?print?a,S[a]}'?log_file?>?log.txt
sort?-n?-t?'?'?-k?2?log.txt?配合sort進(jìn)一步排序

4、將每個(gè)IP訪問(wèn)的頁(yè)面數(shù)進(jìn)行從小到大排序

awk?'{++S[$1]}?END?{for?(a?in?S)?print?S[a],a}'?log_file?|?sort?-n

5、查看某一個(gè)IP訪問(wèn)了哪些頁(yè)面

grep?^111.111.111.111?log_file|?awk?'{print?$1,$7}'

6、去掉搜索引擎統(tǒng)計(jì)的頁(yè)面

awk?'{print?$12,$1}'?log_file?|?grep?^\"Mozilla?|?awk?'{print?$2}'?|sort?|?uniq?|?wc?-l

7、查看2015年8月16日14時(shí)這一個(gè)小時(shí)內(nèi)有多少I(mǎi)P訪問(wèn)

awk?'{print?$4,$1}'?log_file?|?grep?16/Aug/2015:14?|?awk?'{print?$2}'|?sort?|?uniq?|?wc?-l

8、查看訪問(wèn)前十個(gè)ip地址

awk?'{print?$1}'?|sort|uniq?-c|sort?-nr?|head?-10?access_log

uniq -c 相當(dāng)于分組統(tǒng)計(jì)并把統(tǒng)計(jì)數(shù)放在最前面

cat?access.log|awk?'{print?$1}'|sort|uniq?-c|sort?-nr|head?-10
cat?access.log|awk?'{counts[$(11)]+=1};?END?{for(url?in?counts)?print?counts[url],?url}

9、訪問(wèn)次數(shù)最多的10個(gè)文件或頁(yè)面

cat?log_file|awk?'{print?$11}'|sort|uniq?-c|sort?-nr?|?head?-10
cat?log_file|awk?'{print?$11}'|sort|uniq?-c|sort?-nr|head?-20
awk?'{print?$1}'?log_file?|sort?-n?-r?|uniq?-c?|?sort?-n?-r?|?head?-20

訪問(wèn)量最大的前20個(gè)ip

10、通過(guò)子域名訪問(wèn)次數(shù)

依據(jù)referer來(lái)計(jì)算,稍有不準(zhǔn)

cat?access.log?|?awk?'{print?$11}'?|?sed?-e?'?s/http:\/\///'?-e?'?s/\/.*//'?|?sort?|?uniq?-c?|?sort?-rn?|?head?-20

11、列出傳輸大小最大的幾個(gè)文件

cat?www.access.log?|awk?'($7~/\.php/){print?$10?"?"?$1?"?"?$4?"?"?$7}'|sort?-nr|head?-100

12、列出輸出大于200000byte(約200kb)的頁(yè)面以及對(duì)應(yīng)頁(yè)面發(fā)生次數(shù)

cat?www.access.log?|awk?'($10?>?200000?&&?$7~/\.php/){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-100

13、如果日志最后一列記錄的是頁(yè)面文件傳輸時(shí)間,則有列出到客戶(hù)端最耗時(shí)的頁(yè)面

cat?www.access.log?|awk?'($7~/\.php/){print?$NF?"?"?$1?"?"?$4?"?"?$7}'|sort?-nr|head?-100

14、列出最最耗時(shí)的頁(yè)面(超過(guò)60秒的)的以及對(duì)應(yīng)頁(yè)面發(fā)生次數(shù)

cat?www.access.log?|awk?'($NF?>?60?&&?$7~/\.php/){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-100

15、列出傳輸時(shí)間超過(guò) 30 秒的文件

cat?www.access.log?|awk?'($NF?>?30){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-20

16、列出當(dāng)前服務(wù)器每一進(jìn)程運(yùn)行的數(shù)量,倒序排列

ps?-ef?|?awk?-F?'?'?'{print?$8?"?"?$9}'?|sort?|?uniq?-c?|sort?-nr?|head?-20

17、查看apache當(dāng)前并發(fā)訪問(wèn)數(shù)

對(duì)比httpd.conf中MaxClients的數(shù)字差距多少

netstat?-an?|?grep?ESTABLISHED?|?wc?-l

18、可以使用如下參數(shù)查看數(shù)據(jù)

ps?-ef|grep?httpd|wc?-l
1388

統(tǒng)計(jì)httpd進(jìn)程數(shù),連個(gè)請(qǐng)求會(huì)啟動(dòng)一個(gè)進(jìn)程,使用于Apache服務(wù)器。

表示Apache能夠處理1388個(gè)并發(fā)請(qǐng)求,這個(gè)值A(chǔ)pache可根據(jù)負(fù)載情況自動(dòng)調(diào)整

netstat?-nat|grep?-i?"80"|wc?-l
4341

netstat -an會(huì)打印系統(tǒng)當(dāng)前網(wǎng)絡(luò)鏈接狀態(tài),而grep -i “80”是用來(lái)提取與80端口有關(guān)的連接的,wc -l進(jìn)行連接數(shù)統(tǒng)計(jì)。

最終返回的數(shù)字就是當(dāng)前所有80端口的請(qǐng)求總數(shù)

netstat?-na|grep?ESTABLISHED|wc?-l
376

netstat -an會(huì)打印系統(tǒng)當(dāng)前網(wǎng)絡(luò)鏈接狀態(tài),而grep ESTABLISHED 提取出已建立連接的信息。然后wc -l統(tǒng)計(jì)

最終返回的數(shù)字就是當(dāng)前所有80端口的已建立連接的總數(shù)。

netstat?-nat||grep?ESTABLISHED|wc

可查看所有建立連接的詳細(xì)記錄

19、輸出每個(gè)ip的連接數(shù),以及總的各個(gè)狀態(tài)的連接數(shù)

netstat?-n?|?awk?'/^tcp/?{n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N}?END?{for(a?in?S){printf("%-20s?%s\n",?a,?S[a]);++I}printf("%-20s?%s\n","TOTAL_IP",I);for(a?in?s)?printf("%-20s?%s\n",a,?s[a]);printf("%-20s?%s\n","TOTAL_LINK",N);}'

20、其他的收集

分析日志文件下 2012-05-04 訪問(wèn)頁(yè)面最高 的前20個(gè) URL 并排序

cat?access.log?|grep?'04/May/2012'|?awk?'{print?$11}'|sort|uniq?-c|sort?-nr|head?-20

查詢(xún)受訪問(wèn)頁(yè)面的URL地址中 含有 www.abc.com 網(wǎng)址的 IP 地址

cat?access_log?|?awk?'($11~/\www.abc.com/){print?$1}'|sort|uniq?-c|sort?-nr

獲取訪問(wèn)最高的10個(gè)IP地址 同時(shí)也可以按時(shí)間來(lái)查詢(xún)

cat?linewow-access.log|awk?'{print?$1}'|sort|uniq?-c|sort?-nr|head?-10

時(shí)間段查詢(xún)?nèi)罩緯r(shí)間段的情況

cat?log_file?|?egrep?'15/Aug/2015|16/Aug/2015'?|awk?'{print?$1}'|sort|uniq?-c|sort?-nr|head?-10

分析 2015/8/15 到 2015/8/16 訪問(wèn)”/index.php?g=Member&m=Public&a=sendValidCode”的IP倒序排列

cat?log_file?|?egrep?'15/Aug/2015|16/Aug/2015'?|?awk?'{if($7?==?"/index.php?g=Member&m=Public&a=sendValidCode")?print?$1,$7}'|sort|uniq?-c|sort?-nr

($7 ~ /.php/) $7里面包含.php的就輸出,本句的意思是最耗時(shí)的一百個(gè)PHP頁(yè)面

cat?log_file?|awk?'($7~/\.php/){print?$NF?"?"?$1?"?"?$4?"?"?$7}'|sort?-nr|head?-100

列出最最耗時(shí)的頁(yè)面(超過(guò)60秒的)的以及對(duì)應(yīng)頁(yè)面發(fā)生次數(shù)

cat?access.log?|awk?'($NF?>?60?&&?$7~/\.php/){print?$7}'|sort?-n|uniq?-c|sort?-nr|head?-100

統(tǒng)計(jì)網(wǎng)站流量(G)

cat?access.log?|awk?'{sum+=$10}?END?{print?sum/1024/1024/1024}'

統(tǒng)計(jì)404的連接

awk?'($9?~/404/)'?access.log?|?awk?'{print?$9,$7}'?|?sort

統(tǒng)計(jì)http status

cat?access.log?|awk?'{counts[$(9)]+=1};?END?{for(code?in?counts)?print?code,?counts[code]}'
cat?access.log?|awk?'{print?$9}'|sort|uniq?-c|sort?-rn

每秒并發(fā)

watch?"awk?'{if($9~/200|30|404/)COUNT[$4]++}END{for(?a?in?COUNT)?print?a,COUNT[a]}'?log_file|sort?-k?2?-nr|head?-n10"

帶寬統(tǒng)計(jì)

cat?apache.log?|awk?'{if($7~/GET/)?count++}END{print?"client_request="count}'

找出某天訪問(wèn)次數(shù)最多的10個(gè)IP

cat?/tmp/access.log?|?grep?"20/Mar/2011"?|awk?'{print?$3}'|sort?|uniq?-c|sort?-nr|head

當(dāng)天ip連接數(shù)最高的ip都在干些什么

cat?access.log?|?grep?"10.0.21.17"?|?awk?'{print?$8}'?|?sort?|?uniq?-c?|?sort?-nr?|?head?-n?10

小時(shí)單位里ip連接數(shù)最多的10個(gè)時(shí)段

awk?-vFS="[:]"?'{gsub("-.*","",$1);num[$2"?"$1]++}END{for(i?in?num)print?i,num[i]}'?log_file?|?sort?-n?-k?3?-r?|?head?-10

找出訪問(wèn)次數(shù)最多的幾個(gè)分鐘

awk?'{print?$1}'?access.log?|?grep?"20/Mar/2011"?|cut?-c?14-18|sort|uniq?-c|sort?-nr|head

取5分鐘日志

if [ $DATE_MINUTE != $DATE_END_MINUTE ] ;then #則判斷開(kāi)始時(shí)間戳與結(jié)束時(shí)間戳是否相等

START_LINE=sed -n "/$DATE_MINUTE/=" $APACHE_LOG|head -n1?#如果不相等,則取出開(kāi)始時(shí)間戳的行號(hào),與結(jié)束時(shí)間戳的行號(hào)

查看tcp的鏈接狀態(tài)

netstat?-nat?|awk?'{print?$6}'|sort|uniq?-c|sort?-rn?

netstat?-n?|?awk?'/^tcp/?{++S[$NF]};END?{for(a?in?S)?print?a,?S[a]}'?

netstat?-n?|?awk?'/^tcp/?{++state[$NF]};?END?{for(key?in?state)?print?key,"\t",state[key]}'?

netstat?-n?|?awk?'/^tcp/?{++arr[$NF]};END?{for(k?in?arr)?print?k,"\t",arr[k]}'?

netstat?-n?|awk?'/^tcp/?{print?$NF}'|sort|uniq?-c|sort?-rn?

netstat?-ant?|?awk?'{print?$NF}'?|?grep?-v?'[a-z]'?|?sort?|?uniq?-c
netstat?-ant|awk?'/ip:80/{split($5,ip,":");++S[ip[1]]}END{for?(a?in?S)?print?S[a],a}'?|sort?-n?

netstat?-ant|awk?'/:80/{split($5,ip,":");++S[ip[1]]}END{for?(a?in?S)?print?S[a],a}'?|sort?-rn|head?-n?10?

awk?'BEGIN{printf?("http_code\tcount_num\n")}{COUNT[$10]++}END{for?(a?in?COUNT)?printf?a"\t\t"COUNT[a]"\n"}'

查找請(qǐng)求數(shù)前20個(gè)IP(常用于查找攻來(lái)源)

netstat?-anlp|grep?80|grep?tcp|awk?'{print?$5}'|awk?-F:?'{print?$1}'|sort|uniq?-c|sort?-nr|head?-n20?
netstat?-ant?|awk?'/:80/{split($5,ip,":");++A[ip[1]]}END{for(i?in?A)?print?A[i],i}'?|sort?-rn|head?-n20

用tcpdump嗅探80端口的訪問(wèn)看看誰(shuí)最高

tcpdump?-i?eth0?-tnn?dst?port?80?-c?1000?|?awk?-F"."?'{print?$1"."$2"."$3"."$4}'?|?sort?|?uniq?-c?|?sort?-nr?|head?-20

查找較多time_wait連接

netstat?-n|grep?TIME_WAIT|awk?'{print?$5}'|sort|uniq?-c|sort?-rn|head?-n20

找查較多的SYN連接

netstat?-an?|?grep?SYN?|?awk?'{print?$5}'?|?awk?-F:?'{print?$1}'?|?sort?|?uniq?-c?|?sort?-nr?|?more

根據(jù)端口列進(jìn)程

netstat?-ntlp?|?grep?80?|?awk?'{print?$7}'?|?cut?-d/?-f1

查看了連接數(shù)和當(dāng)前的連接數(shù)

netstat?-ant?|?grep?$ip:80?|?wc?-l?
netstat?-ant?|?grep?$ip:80?|?grep?EST?|?wc?-l

查看IP訪問(wèn)次數(shù)

netstat?-nat|grep?":80"|awk?'{print?$5}'?|awk?-F:?'{print?$1}'?|?sort|?uniq?-c|sort?-n

Linux命令分析當(dāng)前的鏈接狀況

netstat?-n?|?awk?'/^tcp/?{++S[$NF]}?END?{for(a?in?S)?print?a,?S[a]}'

watch "netstat -n | awk '/^tcp/ {++S[\$NF]} END {for(a in S) print a, S[a]}'"?#通過(guò)watch可以一直監(jiān)控

LAST_ACK 5 #關(guān)閉一個(gè)TCP連接需要從兩個(gè)方向上分別進(jìn)行關(guān)閉,雙方都是通過(guò)發(fā)送FIN來(lái)表示單方向數(shù)據(jù)的關(guān)閉,當(dāng)通信雙方發(fā)送了最后一個(gè)FIN的時(shí)候,發(fā)送方此時(shí)處于LAST_ACK狀態(tài),當(dāng)發(fā)送方收到對(duì)方的確認(rèn)(Fin的Ack確認(rèn))后才真正關(guān)閉整個(gè)TCP連接;

SYN_RECV 30 # 表示正在等待處理的請(qǐng)求數(shù);

ESTABLISHED 1597 # 表示正常數(shù)據(jù)傳輸狀態(tài);

FIN_WAIT1 51 # 表示server端主動(dòng)要求關(guān)閉tcp連接;

FIN_WAIT2 504 # 表示客戶(hù)端中斷連接;

TIME_WAIT 1057 # 表示處理完畢,等待超時(shí)結(jié)束的請(qǐng)求數(shù);

原文鏈接:https://segmentfault.com/a/1190000009745139

欄目分類(lèi)
最近更新