awstats – 로그 분석/통계 툴

 설치


apt-get install awstats -y

 

 

 설정파일/etc/awstats/awstats.conf.local

LogFile="/var/log/nginx/wp-access.log"
SiteDomain="andrwj.com"
DirData="/var/www/andrwj.com/stats/"
DirIcons="/icon"
HostAliases="www.andrwj.com"
LogFormat = "%host - %host_r %time1 %code %methodurl %bytesd %refererquot %uaquot %otherquot"
DNSLookup = 1
LoadPlugin="geoip GEOIP_STANDARD /usr/share/GeoIP/GeoIP.dat"
DefaultFile="index.html"
SkipHosts="10.0.5.2 10.0.1.3"
LogoLink="http://stat.andrwj.com/"
AllowFullYearView=3
LoadPlugin="tooltips"
LoadPlugin="graphgooglechartapi"
SiteDomain="andrwj.com"

 

 NginX 설정파일 수정


# In HTTP section
map $remote_addr $log_ip {   
    "127.0.0.1" 0;
    "10.0.0.2" 0;
    "10.0.0.3" 0;
    default 1;
}
# for awstats
log_format main '$remote_addr - $remote_user [$time_local] $status '
  '"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' if=$log_ip;

 

 cgi-bin.php 생성


$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("pipe", "w")   // stderr is a file to write to
);
$newenv = $_SERVER;
$newenv["SCRIPT_FILENAME"] = $_SERVER["X_SCRIPT_FILENAME"];
$newenv["SCRIPT_NAME"] = $_SERVER["X_SCRIPT_NAME"];
if (is_executable($_SERVER["X_SCRIPT_FILENAME"])) {
   $process = proc_open($_SERVER["X_SCRIPT_FILENAME"], $descriptorspec, $pipes, NULL, $newenv);
   if (is_resource($process)) {
       fclose($pipes[0]);
       $head = fgets($pipes[1]);
       while (strcmp($head, "\n")) {
           header($head);
           $head = fgets($pipes[1]);
       }
       fpassthru($pipes[1]);
       fclose($pipes[1]);
       fclose($pipes[2]);
       $return_value = proc_close($process);
   } else {
       header("Status: 500 Internal Server Error");
       echo("Internal Server Error");
   }
} else {
   header("Status: 404 Page Not Found");
   echo("Page Not Found");
}
?>

 

  logrotate 설정파일 수정/etc/logrotate.d/nginx

/var/log/nginx/*.log {
        daily
        missingok
        rotate 30
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
    /usr/share/doc/awstats/examples/awstats_updateall.pl now -awstatsprog=/usr/lib/cgi-bin/awstats.pl
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

 

 Crontab 항목 추가

#!/bin/bash
/usr/bin/awstats \
        -config=andrwj.com \
        -update \
        -awstatsprog=/usr/lib/cgi-bin/awstats.pl \
        -output | sed -e 's/awstats\.awstats/awstats\.pl/g' >  /var/www/andrwj.com/stats/index.html