shell脚本分析nginx日志
shell脚本分析nginx日志:
name=`awk -F ',' '{print $13":"$32}' $file | awk -F ':' '{print $4}'`
echo "name=$name"
awk -F
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
抽取nginx日志access.log中的状态码,然后统计状态码中大于等于200小于300的数量
grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" /data/nginx/logs/access.log | awk -F "[ ]+" 'BEGIN{i=0;print "[start]i=0;"}{if($2>=200&&$2<300){i++}}END{print i?i:0}'
grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" /data/nginx/logs/access.log| awk -F "[ ]+" 'BEGIN{i=0;print "[start]i=0;"}{if($2>=200&&$2<300){i++}else if($2>=300&&$2<400){j++}}END{print i?i:0,j?j:0}'
采用慕课网上的案例:

得不出结果,经过调试发现在CentOS6.5下,if语句和上一个括号之间在同一行就好了:
脚本上用到了数组,grep,awk
#!/bin/sh
# Nginx's log analysis #控制终端的输出格式
resettem=$(tput sgr0)
#定义日志的路径
Logfile_path='/data/nginx/logs/access.log'
#i=
#j=
#grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" /data/nginx/logs/access.log| awk -F "[ ]+" 'BEGIN{i=0;print "[start]i=0;"}{if($2>=200&&$2<300){i++}else if($2>=300&&$2<400){j++}}END{print i?i:0,j?j:0}' echo "$Logfile_path" #拿到日志中所有的包含HTTP状态码的部分,拿出第二段来判断,并将结果分配到数组中
grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" $Logfile_path | awk -F "[ ]+" 'BEGIN{i=0;j=0;k=0;n=0;p=0;}{ if($2>=100&&$2<200)
{i++}
else if($>=&&$<)
{j++}
else if($>=&&$<)
{k++}
else if($>=&&$<)
{n++}
else if($>=)
{p++}
}END{
print i?i:,j?j:,k?k:,n?n:,p?p:,i+j+k+n+p
}' Check_http_status()
{
#grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" access.log
#拿到日志中所有的包含HTTP状态码的部分,拿出第二段来判断,并将结果分配到数组中
Http_status_codes=(`grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" $Logfile_path | awk -F"[ ]+" 'BEGIN{i=0;j=0;k=0;n=0;p=0;}{ if($2>=100&&$2<200) {i++} else if($>=&&$<) {j++} else if($>=&&$<) {k++} else if($>=&&$<) {n++} else if($>=) {p++} }END{ print i?i:,j?j:,k?k:,n?n:,p?p:,i+j+k+n+p }'`) echo "---------" echo -e '\E[33m'"The number of http status[100+]:" ${resettem} ${Http_status_codes[]} echo -e '\E[33m'"The number of http status[200+]:" ${resettem} ${Http_status_codes[]} echo -e '\E[33m'"The number of http status[300+]:" ${resettem} ${Http_status_codes[]} echo -e '\E[33m'"The number of http status[400+]:" ${resettem} ${Http_status_codes[]} echo -e '\E[33m'"The number of http status[500+]:" ${resettem} ${Http_status_codes[]} echo -e '\E[33m'"The number of http all status:" ${resettem} ${Http_status_codes[]} } Check_http_status
查看具体的状态码,比如403的状态码
grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" /data/nginx/logs/access.log | awk -F "[ ]+"
'BEGIN{total=0;}{if($2!=""){code[$2]++;total++}else{exit}}END{print code[404]?code[404]:0,code[403]?code[403]:0,total?total:0}'
具体脚本:
Check_http_code()
{
#grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" access.log
Http_Code=(`grep -ioE "HTTP\/1\.[1|0]\"[[:blank:]][0-9]{3}" $Logfile_path | awk -F "[ ]+" 'BEGIN{total=0;}{ if($2!="")
{code[$]++;total++}
else
{exit}
}END{
print code[]?code[]:,code[]?code[]:,total}'`)
echo "---------"
echo -e '\E[33m'"The number of http code[404]:" ${resettem} ${Http_Code[]}
echo -e '\E[33m'"The number of http code[403]:" ${resettem} ${Http_Code[]}
echo -e '\E[33m'"The number of http all status:" ${resettem} ${Http_Code[]}
}
Check_http_code
查看IP来源记录:
nginx默认配置:
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time'; access_log /var/log/nginx/access.log main buffer=32k;
shell脚本分析nginx日志的更多相关文章
- shell脚本分析 nginx日志访问次数最多及最耗时的页面
当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...
- Linux下添加shell脚本使得nginx日志每天定时切割压缩
Linux下添加shell脚本使得nginx日志每天定时切割压缩一 简介 对于nginx的日志文件,特别是access日志,如果我们不做任何处理的话,最后这个文件将会变得非常庞大 这时,无论是出现异常 ...
- shell脚本备份nginx日志
vim /data/runlog.sh #编辑一个 shell 脚本 #!/bin/bash LOGP ...
- 如何用shell脚本分析网站日志统计PV、404、500等数据
以下shell脚本能统计出网站的总访问量,以及404,500出现的次数.统计出来后,可以结合监控宝来进行记录,进而可以看出网站访问量是否异常,是否存在攻击.还可以根据查看500出现的次数,进而判断网站 ...
- shell脚本实例-nginx日志分析
统计2018/8/6 PV量 grep "06/Aug/2018" access.log|wc -l 统计当天8:00 到9:00的PV awk '$4>="[06 ...
- shell脚本分析apache日志状态码
一.首先将apache日志按天切割 vi /etc/httpd/conf/httpd.conf ErrorLog "|rotatelogs /var/log/httpd/%Y% ...
- shell 指令分析nginx 日志qps
实时分析 tail -f points.api.speiyou.cn.access.log|awk 'BEGIN{key="";cnt=0}{if(key==$5){cnt++}e ...
- 利用python分析nginx日志
最近在学习python,写了个脚本分析nginx日志,练练手.写得比较粗糙,但基本功能可以实现. 脚本功能:查找出当天访问次数前十位的IP,并获取该IP来源,并将分析结果发送邮件到指定邮箱. 实现前两 ...
- shell脚本实现网站日志分析统计
如何用shell脚本分析与统计每天的访问日志,并发送到电子邮箱,以方便每天了解网站情况.今天脚本小编为大家介绍一款不错的shell脚本,可以实现如上功能. 本脚本统计了:1.总访问量2.总带宽3.独立 ...
随机推荐
- JPA多对多关联
关于JPA多对多关系,这是使用学生与教师来表示.一个Student由多个Teacher教,同样一个Teacher也可以教多个学生.Student类如下: package com.yichun.bean ...
- js学习笔记17----元素的各种位置,尺寸
1. offsetLeft[Top] 当前元素到定位父级(即offsetParent)的距离(偏移值 ). 父级没有定位: offsetParent -> body offsetLeft[Top ...
- JS中的数字比较
if(parseInt(current_index)!=parseInt(uls)){
- [oracle] oracle-ibatis-整理
① <!-- 复用sql代码 --> <sql id="CUSTOM_CABINET_INFO.QUERY_CABINET"> <dynamic pr ...
- 关于在Android中访问和使用到上下文变量
在监听器内部实现类中要引用上下文变量this的时候 一.采用类名.this的方法 FActivity.this 二.采用全局变量当做中间变量 1.先定义一个全局变量 private Context m ...
- e670. 缓冲图像转换为图像
// This method returns an Image object from a buffered image public static Image toImage(BufferedIma ...
- VC++使用CSocket发送HTTP Request时需要注意发送数据的编码格式
VS2010以及更高版本中新建的MFC项目字符集默认是Unicode,CString创建的字符串默认是Unicode. 使用CSocket时,若以CString组织需要发送的HTTP Head时,那么 ...
- RSQLite 操作sqlite数据库
RSQLite 可以在R中方便的创建sqlite数据库,并进行检索, 这个R包依赖于DBI包 github 上的地址:https://github.com/rstats-db/RSQLite gith ...
- C# SerialPortHelper类
using System; using System.IO.Ports; class SerialPortHelper { private long _receiveByteCount = 0, _s ...
- 第四章 事务(MYBatis)
一个使用 MyBatis-Spring 的主要原因是它允许 MyBatis 参与到 Spring 的事务管理中.而不是给 MyBatis 创建一个新的特定的事务管理器,MyBatis-Spring 利 ...