2013年8月1日星期四

Awstats analysis Nginx log

 

1.nginx log format setting

 
  
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' 
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
 
 

Parameter Description:

 

$ remote_addr with $ http_x_forwarded_for to record the client's ip address;
$ remote_user: used to record the client user name;
$ time_local: used to record the access time and time zone;
$ request: used to record the request url with the http protocol;
$ status: used to record the status of the request; success is 200 ,
$ body_bytes_s ent: Records sent to the client file size of the main content;
$ http_referer: used to record the link from that page to access over;
$ http_user_agent: record customer poison ah browser-related information;

 

2.nginx log cutting script

 
  
#!/bin/bash 

logs_path
="/home/wwwlogs/" #设置日志文件存放目录

pid_path
="/usr/local/nginx/logs/nginx.pid" #设置pid文件

mv ${logs_path}access.log ${logs_path}access_$(date
-d "yesterday" +"%Y%m%d").log #重命名日志文件,将access.log切割为每天的日志文件如access_20130728.log

kill
-USR1 `cat ${pid_path}` #向nginx主进程发信号重新打开日志
 
 

add timing tasks, night 00:00 cutting, crontab-e

 
  
0 0 * * * bash /usr/local/nginx/nginx_log.sh
 
 

3. install and configure awstats

 
  
wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz  
tar -zxvf awstats-7.0.tar.gz
mv awstats
-7.0 /usr/local/awstats
chown
-R root:root /usr/local/awstats
chmod
-R =rwX /usr/local/awstats
chmod
+x /usr/local/awstats/tools/*.pl
chmod +x /usr/local/awstats/wwwroot/cgi-bin/*.pl
 
 

execution tools directory awstats_configure.pl configuration wizard to create a new statistic

 
  
cd /usr/local/awstats/tools 
.
/awstats_configure.pl
 
 The following are some tips

will:

 
  
-----> Running OS detected: Linux, BSD or Unix    

-----> Check for web server install
Enter full config file path of your Web server.
Example:
/etc/httpd/httpd.conf
Example:
/usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf Config file path (
'none' to skip web server setup):
>none #这里添none并回车,因为我们没有使用apache
 
 Enter the next option after

:

 
  
Your web server config file(s) could not be found.  
You will need to setup your web server manually to declare AWStats
script
as a CGI, if you want to build reports dynamically. See AWStats setup documentation (file docs/index.html)
-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' File awstats.model.conf updated.

-----> Need to create a new config file ? Do you want me to build a new AWStats config/profilefile (required if first install) [y/N] ?
 
 

here to choose Y, create a new configuration file

 
  
-----> Define config file name to create 
What
is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site,
virtual server or profile name:
>nginx #这里输入你要分析的域名,或是随便一个你易记的配置名并回车
 
 

next step is to define your configuration file stored in the path, use the default

 
  
-----> Define config file path In which directory do you plan to store your config file(s) ? 
Default:
/etc/awstats
Directory path to store config file(s) (Enter
for default):
> #直接回车,使用默认路径/etc/awstats
 
 

carriage return after the prompt

 
  
-----> Create config file '/etc/awstats/awstats.nginx.conf'  
Config file
/etc/awstats/awstats.nginx.conf created.
-----> Add update process inside a scheduler Sorry, configure.pl does not support automatic add to cron yet.
You can
do it manually by adding the following command to your cron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
Or
if you have several config files and prefer having only one command: /usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to
continue...#按回车继续
A SIMPLE config file has been created:
/etc/awstats/awstats.nginx.conf You should have a look inside to check and change manually main parameters.
You can then manually update your statistics
for 'yuyuanchun.com' with command: > perl awstats.pl -update -config=nginx
You can also build
static report pages for 'nginx' with command: > perl awstats.pl -output=pagetype -config=nginx
Press ENTER to finish... #回车完成配置文件的创建
 
 

will generate a default configuration file named awstats.nginx.conf / etc / awstats / directory, modify the configuration file log location

 
  
vi /etc/awstats/awstats.nginx.conf 
LogFile
="/home/wwwlogs/access_%YYYY-0%MM-0%DD-24.log"
 
 

Log Description

 
  
# "LogFile" contains the web, ftp or mail server log file to analyze. 
# 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...
n表示时间间隔,我这里就是分析距离当前今天24小时前(昨日)的日志,改成48就是前两天的日志
 
 

create a directory for recording data awstats

 
  
mkdir -p /var/lib/awstats
 
 

then run awstats in the wwwroot directory awatsts.pl to test

 
  
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
 
 

If you see a prompt similar to the following description of the configuration file are correct on the

 
  
Create/Update database for config "/etc/awstats/awstats.nginx.conf" by AWStats version 7.0 (build 1.964) From data 
in log file "/home/wwwlogs/access.log_20130727"... Phase 1 : First bypass old records, searching new record...
Direct access after last parsed record (after line
43260) Jumped lines in file: 43260 Found 43260 already parsed records.
Parsed lines
in file: 0
Found
0 dropped records,
Found
0 comments,
Found
0 blank records,
Found
0 corrupted records,
Found
0 old records,
Found
0 new qualified records
 
 

4.Nginx for Perl support is not good, so here awstats tool will use the survey results to generate static files

 

first in the webroot directory, create a folder. Example :/ home / www / awstats

 
  
mkdir -p /home/www/awstats
 
 

write a script to run periodically so Awstats static page generation to the directory, vi / usr / local / nginx / sbin / awstats.sh

 
  
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update   
\
-config=nginx -lang=cn -dir=/home/www/awstats/nginx
\
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
 
 

/ usr / local / awstats / tools / awstats_buildstaticpages.pl Awstats static page generation tool
-update-config = nginx update configuration items
-lang = cn language to Chinese
- dir = / home / www / awstats statistics output directory
-awstatsprog = / usr / local / awstats / wwwroot / cgi-bin / awstats.pl Awstats log update program path

 

adding scripts execute permissions

 
  
chmod +x /usr/local/nginx/sbin/awstats.sh
 
 

run the script to generate a bunch of pages if there is a similar

 
  
Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -update -configdir= ...... 
Build keywords page:
"/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -staticlinks
-lang=cn -output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl"
-config=nginx -staticlinks -lang=cn -output=errors404 20 files built.
Main HTML page
is 'awstats.nginx.html'.
 
 

indicates success, then you can add timed task crontab-e

 
  
00 1 * * * /usr/lcoal/nginx/sbin/awstats.sh #每天凌晨1:00执行
 
 

5. modify nginx configuration file, add the site log analysis results

 
  
server 
{
listen
80;
server_name 192.168.75.28;
index index.html index.htm index.php
default.html default.htm default.php;
root
/home/wwwroot/awstats;
location
^~ /icon/ {
root
/usr/local/awstats/wwwroot;
index index.html;
access_log off;
}
autoindex on;
access_log off;
}
 
 

this visit http://192.168.75.28 you can see the statistical results of the static pages.

 

 

 

 

 

 

 

 

 

 

 

 

没有评论:

发表评论