#!/bin/bash
resrttem=$(tput sgr0)
LOGFILE='/usr/local/apache2.4/logs/access.log'
Check_http_status()
{
http_status_codes=(`cat $LOGFILE|grep -ioE "HTTP\/1\.[0|1]\"[[:blank:]][0-9]{3}"|awk -F"[ ]+" '{
if($2>=100&&$2<200)
{i++}
else if($2>=200&&$2<300)
{j++}
else if($2>=300&&$2<400)
{k++}
else if($2>=400&&$2<500)
{l++}
else if($2>=500)
{n++}
}END{
print i?i:0,j?j:0,k?k:0,l?l:0,n?n:0,i+j+k+l+n
}'
`)
echo -e '\E[33m' "HTTP状态[100+] :" ${resrttem} ${http_status_codes[0]}
echo -e '\E[33m' "HTTP状态[200+] :" ${resrttem} ${http_status_codes[1]}
echo -e '\E[33m' "HTTP状态[300+] :" ${resrttem} ${http_status_codes[2]}
echo -e '\E[33m' "HTTP状态[400+] :" ${resrttem} ${http_status_codes[3]}
echo -e '\e[33m' "HTTP状态[500+] :" ${resrttem} ${http_status_codes[4]}
echo -e '\e[33m' "HTTP全部状态 :" ${resrttem} ${http_status_codes[5]}
}
没有调用函数
#!/bin/bash #脚本实现功能介绍 #功能一:分析HTTP状态码在100-200、200-300、300-400-400-500、500以上的,五个区间的请求条数 #功能二:分析日志中HTTP状态码为404、500的请求条数 #Program function:log analysis resettem=$(tput sgr0) Logfile_path='/usr/local/apache2/logs/access_log' Chack_http_status() { Http_status_codes=(`cat $Logfile_path|grep -ioE "HTTP\/1\.[1|0]\"[[:space:]][0-9]{3}"|awk -F"[ ]+" '{ if($2>=100&&$2<200) {i++} else if($2>=200&&$2<300) {j++} else if($2>=300&&$2<400) {k++} else if($2>=400&&$2<500) {m++} else if($2>=500) {n++} }END{ print i?i:0,j?j:0,k?k:0,m?m:0,n?n:0,i+j+k+m+n }' `) echo -e '\E[33m'"The number of http status[100+]: " ${resettem} ${Http_status_codes[0]} echo -e '\E[33m'"The number of http status[200+]: " ${resettem} ${Http_status_codes[1]} echo -e '\E[33m'"The number of http status[300+]: " ${resettem} ${Http_status_codes[2]} echo -e '\E[33m'"The number of http status[400+]: " ${resettem} ${Http_status_codes[3]} echo -e '\E[33m'"The number of http status[500+]: " ${resettem} ${Http_status_codes[4]} echo -e '\E[33m'"All request number: " ${resettem} ${Http_status_codes[5]}
我的代码是这样的,单独运行awk之前的都没问题,但是就是没反应
我也和你同样的问题,不知道你解决了没有?
估计你的apache access.log格式有点区别,因为HTTP\/1\.[0|1]\"[[:blank:]][0-9]{3}这只能匹配这样"HTTP/1.1" 200"