Apache日志分割及分析
相關軟件及下載地址:
1、 cronolog-1.6.2.tar.gz
2、 awstats-6.95.tar.gz
3、 GeoIP-1.4.6.tar.gz
4、 Geo-IP-1.38.tar.gz
5、 Net-XWhois-0.90.tar.gz
6、 QQWry.Dat
7、 IP解析插件
軟件說明:
Cronolog -apache日志分割
Awstats –apace日志分析主程序
GeoIP –IP地址分析,來至某個國家并顯示國旗
Xwhois及后面 –IP反向解析
詳細安裝過程:
?
一、安裝cronolog日志分割軟件:
[root@saas soft]# tar zxvf cronolog-1.6.2.tar.gz
[root@saas cronolog-1.6.2]# ./configure &&make &&make install
[root@saas cronolog-1.6.2]# which cronolog
/usr/local/sbin/cronolog
修改httpd.conf文件
| <VirtualHost *> ServerName www.test.cn DocumentRoot “/opt/lampp/htdocs/newtest” CustomLog “|/usr/local/sbin/cronolog /opt/lampp/logs/test_%Y%m%d.log” combined ErrorLog “|/usr/local/sbin/cronolog /opt/lampp/logs/test_error_%Y%m%d.log” </VirtualHost> |
?
重啟apache后在/opt/lampp/logs/目錄下可以看到以下格式日志:
| -rw-r–r– 1 root root 12021 6月 25 15:01 test_20100625.log -rw-r–r– 1 root root 718 6月 25 15:00 test_error_20100625.log |
日志分割比較簡單。。至此就安裝完成了。。。以后每天都會在該目錄下生成當日的日志文件。
二、安裝awstats主程序
[root@saas local]# tar zxvf awstats-6.95.tar.gz -C /usr/local/
[root@saas local]# mv awstats-6.95/ awstats
[root@saas local]# mkdir /etc/awstats
[root@saas local]# mkdir /var/lib/awstats
[root@saas local]# chown -R apache /var/lib/awstats/
[root@saas local]# chown -R apache /usr/local/awstats/
[root@saas local]# /usr/local/awstats/tools/awstats_configure.pl
| 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): ? /opt/lampp/etc/httpd.conf #http.conf文件路徑 ? —–> Need to create a new config file ? Do you want me to build a new AWStats config/profile file (required if first install) [y/N] ? y ? —–> 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: ? www.test.cn #生成配置文件的名字 ? —–> 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): >配置文件放置地址 默認即可 ? —–> 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=www.test.cn 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.www.test.cn.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for ‘www.test.cn’ with command: > perl awstats.pl -update -config=www.test.cn You can also read your statistics for ‘www.test.cn’ with URL: > http://localhost/awstats/awstats.pl?config=www.test.cn Press ENTER to finish… 回車結束 |
Configure.pl配置文件,會將:
?
| CustomLog ……………………..common 改為 CustomLog ……………………..combined #實際上面我們已經更改過來,日志切割的時候 ? 會在apache中增加如下內容: Alias /awstatsclasses “/usr/local/awstats/wwwroot/classes/” ? |
當然你也有可能會發現httpd.conf文件會變成這個樣子:(每句后面會多出^M)
| # Do NOT simply read the instructions in here without understanding^M # what they do. They’re here only as hints or reminders. If you are unsure^M # consult the online docs. You have been warned. ^M #^M # Configuration and logfile names: If the filenames you specify for many^M # of the server’s control files begin with “/” (or “drive:/” for Win32), the^M # server will use that explicit path. If the filenames do *not* begin^M # with “/”, the value of ServerRoot is prepended — so “logs/foo.log”^M # with ServerRoot set to “/opt/lampp” will be interpreted by the^M # server as “/opt/lampp/logs/foo.log”.^M ^M #^M |
使用dos2unix命令轉換下格式:
| [root@saas tools]# file /opt/lampp/etc/httpd.conf /opt/lampp/etc/httpd.conf: ASCII English text, with CRLF, LF line terminators [root@saas tools]# dos2unix /opt/lampp/etc/httpd.conf dos2unix: converting file /opt/lampp/etc/httpd.conf to UNIX format … [root@saas tools]# file /opt/lampp/etc/httpd.conf /opt/lampp/etc/httpd.conf: ASCII English text |
?
生成的配置文件,配置文件中大概要修改的如下:/etc/awstats/awstats
| LogFile=”apache日志文件 DirData=”/var/lib/awstats” DirCgi=”/awstats” AllowToUpdateStatsFromBrowser=1 |
注:由于前面我們使用過了日志分割,固每日的日志都會跟上日期。所以在LogFile處可以寫定一個日志文件名稱如:”/opt/lamp/logs/Test_acc.log” 。每日凌晨的時候利用腳本將其前日的日志復制覆蓋掉此文件再做分析。
現為了測試,先將今日的日志先復制一份命名為”Test_acc.log”
更新數據并測試awstats是否網頁訪問OK
| [root@saas ~]# usr/local/awstats/tools/awstats_updateall.pl now Running ‘”/usr/local/awstats/wwwroot/cgi-bin/awstats.pl” -update -config=www.test.cn -configdir=”/etc/awstats”‘ to update config www.test.cn Create/Update database for config “/etc/awstats/awstats.www.test.cn.conf” by AWStats version 6.95 (build 1.943) From data in log file “/opt/lampp/logs/test_acc.log”… Phase 1 : First bypass old records, searching new record… Direct access after last parsed record (after line 880) Jumped lines in file: 880 Found 880 already parsed records. Parsed lines in file: 0 Found 0 dropped records, Found 0 corrupted records, Found 0 old records, Found 0 new qualified records. |
使用瀏覽器,察看日志統計情況:http://域名/awstats/awstats.pl?config=配置文件名。。
實例如下:
http://www.test.cn/awstats/awstats.pl?config=www.test.cn
如提示:Forbidden You don’t have permission to access /awstats/awstats.pl on this server.錯誤。
1、 請確認/usr/local/awstats目錄權限
| [root@saas ~]# chmod -R 755 /usr/local/awstats |
?
2、 檢測httpd.conf文件是否應用支持CGI。。改httpd.conf為如下:
| [root@OAServer ~]# tail /opt/lampp/etc/httpd.conf # This is to permit URL access to scripts/files in AWStats directory. # <Directory “/usr/local/awstats/wwwroot”> Options FollowSymLinks +ExecCGI AddHandler cgi-script .pl AllowOverride None Order allow,deny Allow from all </Directory> |
?
三、插件安裝:
1、 GeoIP-1.4.6.tar.gz安裝
| [root@saas soft]# tar zxvf GeoIP-1.4.6.tar.gz [root@saas GeoIP-1.4.6]#./configure &&make &&make install |
2、 Geo-IP-1.38.tar.gz安裝
| [root@saas soft]# tar zxvf Geo-IP-1.38.tar.gz [root@saas Geo-IP-1.38]# perl Makefile.PL &&make &&make install [root@saas Geo-IP-1.38]# /usr/local/bin/geoipupdate -v |
?
安裝好Geoip插件后,在配置文件中啟用插件:
| [root@saas soft]# grep “geoip ” /etc/awstats/awstats.www.test.cn.conf # geoip or geoipfree plugin instead of enabled reverse DNS lookup. LoadPlugin=”geoip GEOIP_STANDARD /usr/local/share/GeoIP/GeoIP.dat” #取消掉本行注釋 |
?
在web頁面就能看到如下紅色標注部分及國旗了。。
? | ||||||||||||||||||||||||||||||||||||
| 國家或地區 | 網頁數 | 文件數 | 字節 | |||||||||||||||||||||||||||||||||
| China | cn | 4444 | 39202 | 1.83 G字節 | | |||||||||||||||||||||||||||||||
| Australia | au | 311 | 2174 | 109.83 M字節 | | |||||||||||||||||||||||||||||||
?
3、Net-XWhois-0.90.tar.gz 安裝(反向地址解析)
| [root@saas soft]# tar zxvf Net-XWhois-0.90.tar.gz [root@saas Net-XWhois-0.90]# perl Makefile.PL &&make &&make install |
改qqwry.pl文件中
| my $ipfile=”./QQWry.Dat”; 為 my $ipfile=”${DIR}/plugins/QQWry.Dat”; |
復制 QQWry.Dat, qqhostinfo.pm, qqwry.pl 等3個文件到 awstats/wwwroot/cgibin/plugins 下面。并授權755
| [root@saas soft]# cp qqhostinfo.pm QQWry.Dat qqwry.pl \ >/usr/local/awstats/wwwroot/cgi-bin/plugins/ [root@saas soft]#chmod 755 /usr/local/awstats/wwwroot/cgi-bin/plugins/* |
修改配置文件中LoadPlugin如下:
| LoadPlugin=”qqhostinfo” 順帶啟用以下插件: LoadPlugin=”tooltips” LoadPlugin=”decodeutfkeys” #解決搜索關鍵字亂碼問題 |
?
一切OK后。重新刷新下WEB查看頁面。可以看到如下:
| 主機 : 0 個解譯成功, 763 無法得知(不能反解網域名稱) | GeoIP | Location | 網頁數 | 文件數 | 字節 | 最近參觀日期 |
| 121.56.215.145 | China | 內蒙古興安盟 電信 | 205 | 1070 | 12.29 M字節 | 2010年06月27日 13:41 |
| 123.179.199.146 | China | 內蒙古赤峰市 電信 | 108 | 636 | 51.03 M字節 | 2010年06月27日 03:45 |
| 110.84.174.240 | Australia | 福建省福州市 電信 | 105 | 478 | 19.08 M字節 | 2010年06月27日 05:48 |
?
在頁面的最下面可以看到已經啟用的插件:
| Advanced Web Statistics 6.95 (build 1.943) - 創建者 awstats (插件: qqhostinfo, geoip, decodeutfkeys, tooltips) |
?
四、編寫簡單腳本。并設置定時執行
| [root@saas soft]#cat /opt/lampp/logs/awstats.sh #!/bin/bash logdir=/opt/lampp/logs date=`date +%Y%m%d -d “-1 days”` rmdate=`date +%Y%m%d -d “-7 days”` \cp $logdir/ksense.com.cn_$date.log $logdir/Test_acc.log rm -rf $logdir/*_$rmdate.log /usr/local/awstats/tools/awstats_updateall.pl now ? [root@saas soft]]# crontab -l 01 01 * * * /opt/lampp/logs/awstatus.sh #每晚一點處理前一天的日志文件 |
五、關于安全。網上很多都提倡使用密碼認證的方式。。我做得比較簡單
| <Directory “/usr/local/awstats/wwwroot”> Options FollowSymLinks +ExecCGI AddHandler cgi-script .pl AllowOverride None Order allow,deny Allow from 192.168.0.12 #只允許本IP訪問此頁面 </Directory> |
?
總結
以上是生活随笔為你收集整理的Apache日志分割及分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 熊绎:我看软件工程师的职业规划
- 下一篇: spring(一)依赖注入与 SPEL