当前位置:文档之家› 【精编范文】shell脚本分析log日志-范文模板 (12页)

【精编范文】shell脚本分析log日志-范文模板 (12页)

本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!== 本文为word格式,下载后可方便编辑和修改! ==shell脚本分析log日志篇一:nginx日志分析解决方案nginx日志分析解决方案- Awstats很多PHP搭建的网站都在由apache转向了nginx。

nginx的日志信息如何分析呢?当然你可以自己写一个,但是这里也推荐一款结果信息非常详尽的开源工具——Awstats,它基于perl编写,它的介绍如下:AWStats is a free powerful and featureful tool that generates advanced web, streaming, ftp or mail server statistics, graphically. This log analyzer works as a CGI or from command line and shows youall possible information your log contains, in few graphical web pages. It uses a partial information file to be able to process large log files, often and quickly. It can analyze log files from all major server tools like Apache log files (NCSA combined/XLF/ELF log formatorcommon/CLF log format), WebStar, IIS (W3C log format) and a lot of other web, proxy, wap, streaming servers, mail servers and some ftp servers.点击这里去官方网站第一步,日志的处理。

最好每天分割一下日志,一开始没注意这个工作,结果不久日志文件就上G了,很痛苦。

分割日志很简单,首先把日志文件复制到别的地方,然后再通知nginx重新生成日志就可以了。

shell脚本如下:#!/bin/bashlogs_path="/nginx/logs/"mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").logkill -USR1 `cat /nginx/logs/nginx.pid`代码中的/nginx/logs指的是nginx的log日志文件所在目录,生成了以昨天日期命名的日志文件。

为了达到每天自动分割的目的,在crontab中加入以下部分:1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh这样就每天的0点1分把nginx日志重命名为日期格式,并重新生成今天的新日志文件。

第二步,Awstats的配置。

日志文件分割好了,接下来就是分析了,也就是Awstats的使用了。

Awstats的配置文件默认会存储在/etc/awstats/目录下,包括你安装时设置的域名如:awstats..conf。

在这个配置文件中修改这个地方:LogFile="/nginx/logs/access_%YYYY-0%MM-0%DD-24.log"这个意思是要去读取nginx昨天的日志文件,关于后边%YYYY-0%MM-0%DD-24的设置,规则如下:# You can also use tags in this filename if you need a dynamic file name# depending on date or time (Replacement is made by AWStats at the beginning # of its execution). This is available tags :#%YYYY-n is replaced with 4 digits year we were n hours ago#%YY-n is replaced with 2 digits year we were n hours ago#%MM-n is replaced with 2 digits month we were n hours ago#%MO-n is replaced with 3 letters month we were n hours ago#%DD-n is replaced with day we were n hours ago#%HH-n is replaced with hour we were n hours ago#%NS-n is replaced with number of seconds at 00:00 since 1970#%WM-n is replaced with the week number in month (1-5)#%Wm-n is replaced with the week number in month (0-4)#%WY-n is replaced with the week number in year (01-52)#%Wy-n is replaced with the week number in year (00-51)#%DW-n is replaced with the day number in week (1-7, 1=sunday)#use n=24 if you need (1-7, 1=monday)#%Dw-n is replaced with the day number in week (0-6, 0=sunday)#use n=24 if you need (0-6, 0=monday)#Use 0 for n if you need current year, month, day, hour第三步,开始分析、生成结果。

最后,可以执行分析了。

使用这个命令:/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=这个命令会把结果生成到/var/lib/awstats目录下 awstatsXXXX..txt文件。

当然啦,这样看起来不太方便哦,呵呵,可以再用下面的命令来生成html页面,相当漂亮:perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update \-config= -lang=cn \-dir=/html/awstats \-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl这样就会在/html/awstats目录下生成很漂漂的分析结果页,很暴力很强大。

第四步,自动化。

要是每天都去服务器上运行几条命令肯定是件令人烦燥的事情,所以呢,linux 的世界里有crontab这样的好东东,很简单,下面是我的crontab1 0 * * * sh /home/zyf/sh/cut_nginx_log.sh0 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=0 2 * * * perl /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config= -lang=cn -dir=/html/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl-------------------------------------------------------------------------大功告成,打完收功……篇二:Linux常见的日志文件及查看命令Linux常见日志和常用命令Linux 日志都以明文形式存储,所以我们不需要特殊的工具就可以搜索和阅读它们。

Linux 日志存储在 /var/log 目录中,我们可以编写脚本,来扫描这些日志,并基于它们的内容去自动执行某些功能。

一、Linux常用的日志文件# /var/log/boot.log该文件记录了系统在引导过程中发生的事件,就是Linux系统开机自检过程显示的信息。

# /var/log/cron该日志文件记录crontab守护进程crond所派生的子进程的动作,前面加上用户、登录时间和PID,以及派生出的进程的动作。

CMD的一个动作是cron派生出一个调度进程的常见情况。

REPLACE(替换)动作记录用户对它的cron文件的更新,该文件列出了要周期性执行的任务调度。

RELOAD动作在REPLACE动作后不久发生,这意味着cron注意到一个用户的cron文件被更新而cron需要把它重新装入内存。

该文件可能会查到一些反常的情况。

# /var/log/maillog该日志文件记录了每一个发送到系统或从系统发出的电子邮件的活动。

它可以用来查看用户使用哪个系统发送工具或把数据发送到哪个系统。

# /var/log/messages该日志文件是许多进程日志文件的汇总,从该文件可以看出任何入侵企图或成功的入侵。

该文件的格式是每一行包含日期、主机名、程序名,后面是包含PID或内核标识的方括号、一个冒号和一个空格,最后是消息。

相关主题