2011年11月8日 星期二

large partitions greater than 2TB




[CentOS] LVM on large partitions greater than 2TB
http://lists.centos.org/pipermail/centos/2007-February/032574.html

2011年10月5日 星期三

nagios linux nrpe設定(3)使用nagios-plugins

參考
http://nagios.sourceforge.net/docs/nrpe/NRPE.pdf
http://www.debianhelp.co.uk/nagiospluins.htm

比較:
nagios server端:192.168.1.2 ,已在web server根目錄放 index.html 的 nagios server端
client 端:192.168.1.133,web server根目錄沒東西,check_http 正常會回應 403 Forbidden

兩者在 nrpe.cfg 都沒定義  check_http ,但 command-plugins.cfg 有command[check_http]

nagios linux nrpe設定(2)-基本安裝


*被監視端:
從剛裝好完全沒東西的 VM開始…

CentOS5.5 用 rpmforge 的來源做 yum 安裝,plugins 是放在
/usr/lib64/nagios/plugins/


yum install nrpe
會安裝
fping
nagios-plugins
perl-Crypt-DES
perl-Net-SNMP

2011年10月4日 星期二

nagios linux nrpe設定

server 裝好的 localhost

CentOS5.5 用 rpmforge 的來源做 yum 安裝,plugins 是放在
/usr/lib64/nagios/plugins/

...首先
/etc/nagios/nrpe.cfg 裡的
server_address=
allow_hosts=
後面跟的 IP 不能用逗點加別的 ip (例如:192.168.2.3,127.0.0.1)
目前先試只定義 server_address 1個 IP
allow_hosts 註解掉

iptables 加上   -p tcp -m tcp --dport 5666 -j ACCEPT

nagios linux nrpe設定(5) 另外加 plugins & command(2)


想新增一項目來看Linux client端的記憶體使用情形(Windows的預設有此項目)
預設沒有此command可處理…
記錄改了多少東東…

實作完…問題在佈署

Client 端(安裝 nagios-nrpe ,nagios-nrpe-plugins)
*在 /usr/lib64/nagios/plugins/  新增command用的shell script,
記得chmod 755(權限要跟其它plugin一樣)

實作時用的是下列討論區用的 script:
http://serverfault.com/questions/195815/nagios-memory-configuration
另有一版本可參考
http://www.linuxquestions.org/questions/linux-software-2/nagios-check-ram-usage-on-remote-server-747531/
vi /usr/lib64/nagios/plugins/check_memeory.sh

#!/bin/bash
#
# Script to check memory usage on Linux. Ignores memory used by disk cache. 
#
# Requires the bc command
#
print_help() {
    echo "Usage:"
    echo "[-w] Warning level as a percentage" 
    echo "[-c] Critical level as a percentage" 
    exit 0
}

while test -n "$1"; do
    case "$1" in
        --help|-h)
            print_help
            exit 0
            ;;
        -w)
            warn_level=$2
            shift
            ;;
        -c)
            critical_level=$2
            shift
            ;;
        *)
            echo "Unknown Argument: $1"
            print_help
            exit 3
            ;;
    esac
    shift
done

if [ "$warn_level" == "" ]; then
    echo "No Warning Level Specified"
    print_help
    exit 3;
fi

if [ "$critical_level" == "" ]; then
    echo "No Critical Level Specified"
    print_help
    exit 3;
fi


free=`free -m | grep "buffers/cache" | awk '{print $4}'`
used=` free -m | grep "buffers/cache" | awk '{print $3}'`

total=$(($free+$used))

result=$(echo "$used / $total * 100" |bc -l|cut -c -2)

if [ "$result" -lt "$warn_level" ]; then
    echo "Memory OK. $result% used."
    exit 0;
elif [ "$result" -ge "$warn_level" ] && [ "$result" -le "$critical_level" ]; then
    echo "Memory WARNING. $result% used."
    exit 1;
elif [ "$result" -gt "$critical_level" ]; then
    echo "Memory CRITICAL. $result% used."
    exit 2;
fi

chmod 755 /usr/lib64/nagios/plugins/check_memeory.sh
Client本機端試用:
sh  /usr/lib64/nagios/plugins/check_memory.sh
sh  /usr/lib64/nagios/plugins/check_memory.sh -w 80 -c 95


*在 /etc/nagios/nrpe.cfg 新增 command
##self defined## MEMORY Check
# check_mem   = MEMORY at defined warning and critical use %.
command[check_memory]=/usr/local/nagios/libexec/check_memory.sh -w $ARG1$ -c $ARG2$

service nrpe restart


Nagios Server端
*在 /etc/nagios/objects/commands.cfg 新定義 command

define command{
command_name check_memory
command_line $USER1$/check_memory.sh -w $ARG1$ -c $ARG2$
}

*在 對象設定檔.cfg 新增 service
測試對象:localhost.cfg
  vi /etc/nagios/objects/localhost.cfg
define service{
        use                   local-service;Name of service template to use
        host_name             localhost
        service_description   Memory Use
        check_command         check_memory!80!90
        }
service nagios restart

nagios linux nrpe設定(5) 另外加 plugins & command(1)


起因:看到 check_nt 可以看 Windows Client 的 memory usage ...Linux預設沒有...
由於預設沒有此command可處理,所以開始…

小記:實作記錄見下篇
*在 /usr/lib64/nagios/plugins/  新增command用的shell script,
記得chmod 755(權限要跟其它plugin一樣)
*在 /etc/nagios/objects/commands.cfg 新增 command
*在 /etc/nagios/nrpe.cfg 新增 command
*在 /etc/nagios/objects/對象設定檔.cfg 新增 service

Service definition on the server in the host config:
define service {
       use                     generic-service
       host_name               
       service_description     Memory Usage
       check_command           check_nrpe_1arg!check_memory
}
This needs to be defined in /etc/nagios/nrpe.cfg on the client, amended for the values you want to check for:
command[check_memory]=/usr/lib/nagios/plugins/check_memory.sh -w 85 -c 90
Example output:
#:~$ ./check_memory.sh -w 80 -c 90
Memory OK. 44% used.


#:~$ ./check_memory.sh -w 40 -c 50
Memory WARNING. 44% used.
The script:
#!/bin/bash
#
# Script to check memory usage on Linux. Ignores memory used by disk cache. 
#
# Requires the bc command
#
print_help() {
    echo "Usage:"
    echo "[-w] Warning level as a percentage" 
    echo "[-c] Critical level as a percentage" 
    exit 0
}

while test -n "$1"; do
    case "$1" in
        --help|-h)
            print_help
            exit 0
            ;;
        -w)
            warn_level=$2
            shift
            ;;
        -c)
            critical_level=$2
            shift
            ;;
        *)
            echo "Unknown Argument: $1"
            print_help
            exit 3
            ;;
    esac
    shift
done

if [ "$warn_level" == "" ]; then
    echo "No Warning Level Specified"
    print_help
    exit 3;
fi

if [ "$critical_level" == "" ]; then
    echo "No Critical Level Specified"
    print_help
    exit 3;
fi


free=`free -m | grep "buffers/cache" | awk '{print $4}'`
used=` free -m | grep "buffers/cache" | awk '{print $3}'`

total=$(($free+$used))

result=$(echo "$used / $total * 100" |bc -l|cut -c -2)

if [ "$result" -lt "$warn_level" ]; then
    echo "Memory OK. $result% used."
    exit 0;
elif [ "$result" -ge "$warn_level" ] && [ "$result" -le "$critical_level" ]; then
    echo "Memory WARNING. $result% used."
    exit 1;
elif [ "$result" -gt "$critical_level" ]; then
    echo "Memory CRITICAL. $result% used."
    exit 2;
fi


2011年10月3日 星期一

nagios 小記 - Windows Client 安裝 NSClient++


* Windows Client 要安裝的 NSClient++
新版的 client 會自動加規則到內建的防火牆
但這次遇到 nagios server端的 objects/commands.cfg, 
預設 $HOSTADDRESS& -p 這段後面不是 IP,會有"server port is not interger" 之類的訊息
就算 NSClient 的設定檔不刻意設定 port ,也要在 commands.cfg 裡指定“ -p 12489

NSClient++ 安裝時(或事後更改設定檔)設定的密碼
為明碼,在 nagios server端的 objects/commands.cfg 
找到(預設應該完全沒有?)
 command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p portnumber
後面加上“ -s 密碼” 搭配使用

參考:


NSClient++ Port Forwarding and Firewall

The default port for NSClient++ is 12489
You must allow the firewall at your remote server to allow this port access.
Changing NSClient++ port:
You can change the NSClient++ port by modifying the NSC.ini file located at C:\Program Files\NSClient++ (remove “;”)
;# NSCLIENT PORT NUMBER
;  This is the port the NSClientListener.dll will listen to.
port=12489
Restart the NSClient++ service
Change check_nt -p 12489 argument in /usr/local/nagios/etc/objects/commands.cfg
# 'check_nt' command definition
define command{
        command_name    check_nt
        command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p portnumber -s cjr-nagios -v $ARG1$ $ARG2$
        }

參考來源:
http://nagios.sourceforge.net/docs/3_0/monitoring-windows.html
http://www.kernelhardware.org/nagios-nsclient-to-monitor-remote-windows-server/
http://www.thegeekstuff.com/2008/07/how-to-monitor-remote-windows-machine-using-nagios-on-linux/
http://creative1223.pixnet.net/blog/post/1302004


*檢查 D磁碟機,注意後面的參數是 -l  d    (小寫L)

        check_command           check_nt!USEDDISKSPACE!-l d -w 80 -c 90

http://nagiosplugins.org/man/check_disk
http://nagiosplugins.org/man/check_nt


待試:
http://nagiosplugins.org/man/check_nt
http://nagioswiki.com/wiki/index.php/Checking_NT_Services_with_Nagios

[轉貼]CentOS install Nagios

*這次實作卡在... 漏裝 gcc glibc glibc-common gd gd-devel 也沒啥差!?
…要先啟動 nagios service 再啟動 httpd ??

手工安裝(含服務、權限)...愈下文愈新
http://blog.haohtml.com/archives/4565
http://blog.prosight.me/index.php/2009/06/10
http://www.ibm.com/developerworks/cn/linux/l-ganglia-nagios-2/

http://forum.icst.org.tw/phpbb/viewtopic.php?t=17761

http://www.xtgly.com/2010/09/21/centos-5-5-nginx-nagios%E7%9B%91%E6%8E%A7%E7%AB%AF%E5%92%8C%E8%A2%AB%E6%8E%A7%E7%AB%AF%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E6%8C%87%E5%8D%97.htm


http://www.openfoundry.org/tw/tech-column/8308--nagios-
http://creative1223.pixnet.net/blog/category/61902

步驟詳盡…在unix下...
http://homepage.mac.com/duling/halfdozen/Nagios-Howto-p1.html
http://homepage.mac.com/duling/halfdozen/Nagios-Howto-p2.html

yum 安裝:需先下載安裝 rpmforge
http://wiki.centos.org/zh-tw/AdditionalResources/Repositories/RPMForge
http://wiki.centos.org/zh-tw/HowTos/Nagios
http://docs.cslabs.clarkson.edu/wiki/Install_Nagios_on_CentOS_5

Install

Add RPMForge Yum Repository

Install & Configure Prerequisites

  • Install Apache
    • yum install httpd php gcc glibc glibc-common gd gd-devel
  • Configure Apache to start on boot
    • /sbin/chkconfig --levels 345 httpd on
  • Configure iptables to allow Apache traffic
    • /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
    • /etc/init.d/iptables save
    • /etc/init.d/iptables restart

Install & Configure Nagios

  • Install Nagios & Plugins
    • yum install nagios nagios-plugins nagios-plugins-setuid
  • Create the default Nagios web access user & set a password
    • htpasswd -c /etc/nagios/htpasswd.users nagiosadmin
  • Verify default config files
    • nagios -v /etc/nagios/nagios.cfg
  • Start Nagios
    • /etc/init.d/nagios start
  • Start Apache
    • /etc/init.d/httpd start

Verify Install

  • Try logging into your new Nagios installation by going to http://servername/nagios/ and logging in with nagiosadmin and the password you set.


2011年9月27日 星期二

[轉貼]FTP 文摘

一堆貼文… 不同OS下設定檔路徑不一定相同

鳥哥的(最詳細,最底下還有一堆 sample)
http://linux.vbird.org/linux_server/0410vsftpd.php#server_before

實作時需要此主題的內容“實體帳號的 SELinux 議題” -> 偷懶先disable selinux 也可....
http://www.linuxquestions.org/questions/fedora-35/vsftpd-error-553-could-not-create-file-390569/
run the command ( SELinux 有在run的情況下 )
setsebool -P ftp_home_dir 1



http://machiko.pixnet.net/blog/post/33116049


vsftpd官方faq

http://machiko.pixnet.net/blog/post/33116021


Ubuntu vsftp - FTP Server輕鬆架


http://mybeauty.pixnet.net/blog/post/26400610

vstfp server 設定
http://uiop7890.pixnet.net/blog/post/24739504

2011年9月26日 星期一

[轉貼]CentOS 5 install ntfs-3g

先check 版本(32bit /64bit)
uname -i
安裝  rpmforge-release
http://packages.sw.be/
i386 http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i386.rpmx86_64 http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm


可先安裝 DAG 的 GPG 金鑰
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt


審核下載的套件
rpm -K rpmforge-release-0.5.2-2.el6.rf.*.rpm


安裝套件
rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm


安裝下列套件。
yum install fuse fuse-ntfs-3g 


若 rpmforge 軟件庫預設為停用的,
yum --enablerepo=rpmforge install fuse fuse-ntfs-3g 


如果是 CentOS 5.3 或更舊版本,那麼你需要安裝來自 ELRepo 的 kmod-fuse


http://wiki.centos.org/zh-tw/TipsAndTricks/NTFS
http://wiki.centos.org/zh-tw/AdditionalResources/Repositories/RPMForge


2011年7月21日 星期四

讓別台主機存取MySQL

vi /etc/sysconfig/iptables

-A RH-Firewall-1-INPUT -i eth1 -p tcp -s 內網網段/24  --dport 3306  -j ACCEPT
-A RH-Firewall-1-INPUT -i eth0 -p tcp -s 外網網段/24  --dport 3306  -j ACCEPT



mysql -p

CREATE USER 'root'@'10.20.%.%';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'10.20.%.%' IDENTIFIED BY '同localhost用的密碼';
flush privileges;

CREATE USER 'root'@'外網網段.%';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'外網網段.%' IDENTIFIED BY '同localhost用的密碼';
flush privileges;

參考:
http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html
http://www.debianhelp.co.uk/remotemysql.htm

2011年6月24日 星期五

[轉貼]grep 包含子目錄檔案的方法

http://blog.xuite.net/jyoutw/xtech/46682931

在所有的網頁中,找出含有某特定字串的檔案。
例如說要找出含有 localhost 字串的檔案,記得 grep 沒有 recursive 的參數,可以搜尋包含子目錄
所以就要利用 find 的功能來協助

# find ./ -type f -name \*.php |xargs grep 'localhost'


用上面的方式連是哪個檔案都會列出來,若用下列的寫法,速度既慢而且也無法得知是哪個檔案有含該字串:
# find ./ -type f -name \*.php -exec grep 'localhost' {} \;
 

2011年6月12日 星期日

[轉貼]解析度過大導致螢幕黑畫面

http://www.ubuntu-tw.com/modules/newbb/viewtopic.php?topic_id=20514&viewmode=flat&order=ASC&type=&mode=0&start=0&menumode=2

(要改的東東在 wubi 的 img檔裡,還要做掛 img檔的步驟…)


按ctrl+alt+F1進入文字介面
sudo nano /etc/X11/xorg.conf
看看有沒有內容,沒有的話Ctrl+X跳出
照順序執行:
sudo /etc/init.d/gdm stop
sudo X -configure
sudo mv /root/xorg.conf.new /etc/X11/xorg.conf
sudo nano /etc/X11/xorg.conf
到此xorg.conf已有內容,
找出"Monitor"這一個區塊
加入HorizSync和VertRefresh兩項,
數值請參照螢幕說明書找 水平同步 跟 垂直更新
然後Ctrl+X按y存檔,
最後sudo /etc/X11/gdm start

改完類似這樣:
Section "Monitor"
Identifier "Monitor0"
HorizSync 30.0 - 80.0
VertRefresh 60.0 - 75.0
EndSection

mount wubi disk

http://ubuntuforums.org/showthread.php?t=1564156

Code:
sudo mount -o loop //root.disk /mnt
nautilus /mnt   # to browse
If you have problems mounting it, you can fsck it (don't do it if you mounted it already):

Code:
sudo fsck //root.disk


http://ubuntuforums.org/showthread.php?t=1037874
Make a new text file:

Code:
#!/bin/sh
sudo mkdir -p /media/WindowsXP
echo "Mounting NTFS Partition"
sudo mount -t ntfs /dev/sda1 /media/WindowsXP
sleep 1
sudo mkdir -p /media/root.disk
echo "Mounting Wubi Disk"
sudo mount -o loop /media/WindowsXP/ubuntu/disks/root.disk /media/root.disk
sleep 1
echo "Done :)"
gksu nautilus /media/root.disk
exit 0 &
Save this as mountwubi.sh. Right click this new file, select Permissions tab and put a tick in the Execute box.

Create another text file:

Code:
#!/bin/sh
echo "Unmounting Wubi Disk"
sudo umount /media/root.disk
sleep 1
echo "Unmounting NTFS Partition"
sudo umount /media/WindowsXP
sleep 1
echo "Done :)"
exit 0 &
Save this as umountwubi.sh. Right click this new file, select Permissions tab and put a tick in the Execute box.


Double clicking mountwubi.sh and selecting Run in Terminal will mount your old wubi install and open a root nautilus for full access to the files.
The function of umountwubi.sh is pretty obvious

2011年5月19日 星期四

[轉貼]過濾 log 資訊的 script

仲佑的網誌
http://yowlab.shps.kh.edu.tw/wordpress/?p=1294


檢查誰在亂踹:
1.grep 'login error' /var/log/openwebmail.log > auth_error.log
2.sed ' s/^.* (\[0-9]*\.[0-9]*.\[0-9]*\.[0-9]*\).*/\1/g 'auth_error.log > ip_list.txt
3.sort ip_list.txt > sort_ip_list.txt
4.uniq -c sort_ip_list.txt


檢查誰在寄信:
1.grep 'send message' /var/log/openwebmail.log.1 > send-mail.log
2.sed ' s/^.* (\[0-9]*\.[0-9]*.\[0-9]*\.[0-9]*) \ ( [a-zA-Z].*\ ) -send message .*/\1/g ' send-mail.log > send-list.log
3.sort sender-list.log > sort-sender.log
4.uniq -c sort-sender.log


http://www.ruanyifeng.com/blog/2012/01/a_bash_script_of_apache_log_analysis.html


反复查看手册,确认用法和合适的参数。下面就是我的日志分析脚本,虽然它还不是通用的,但是我相信里面用到的命令,足以满足一般的日志分析需求,同时也是很好的学习Bash的实例。如果下面的每一个命令你都知道,我觉得可以堪称熟练使用Bash了。
一、操作环境
在介绍脚本之前,先讲一下我的服务器环境。
我的网络服务器软件是Apache,它会对每一个http请求留下记录,就像下面这一条:
  203.218.148.99 - - [01/Feb/2011:00:02:09 +0800] "GET /blog/2009/11/an_autobiography_of_yang_xianyi.html HTTP/1.1" 200 84058 "http://www.ruanyifeng.com/blog/2009/11/freenomics.html" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"
它的意思是2011年2月1日,IP地址为203.218.148.99的访问者,向服务器请求访问网址/blog/2009/11/an_autobiography_of_yang_xianyi.html。
当天所有的访问记录,组成一个日志。过去一年,一共生成了365个日志文件。它们存放在12个目录中,每一个目录表示一个月(2011-01、2011-02、......2011-12),里面的日志文件依次为www-01.log、www-02.log、......www-31.log(假定该月有31天)。
在不压缩的情况下,365个日志文件加起来,要占掉10GB空间。我的目标就是分析这10GB日志,最后得到一个如下形式的访问量排名:
  访问量 网址1
  访问量 网址2
  访问量 网址3
  ...... ......
二、为什么要用Bash
很多计算机语言,都可以用来完成这个任务。但是,如果只是简单的日志分析,我觉得Bash脚本是最合适的工具。
主要原因有两个:一是"开发快",Bash脚本是各种Linux命令的组合,只要知道这些命令怎么用,就可以写脚本,基本上不用学习新的语法,而且它不用编译,直接运行,可以边写边试,对开发非常友好。二是"功能强",Bash脚本的设计目的,就是为了处理输入和输出,尤其是单行的文本,所以非常合适处理日志文件,各种现成的参数加上管道机制,威力无穷。
前面已经说过,最终的脚本我只用了20多行,处理10GB的日志,20秒左右就得到了结果。考虑到排序的巨大计算量,这样的结果非常令人满意,充分证明了Bash的威力。
三、总体思路
我的总体处理思路是这样的:
  第一步,处理单个日志。统计每一天各篇文章的访问量。
  第二步,生成月度排名。将每一天的统计结果汇总,得到月度访问量。
  第三步,生成年度排名。将12个月的统计结果汇总,进行年度访问量的排序。
四、处理单个日志
以2011年1月1日的日志为例,它在目录2011-01之中,文件名是www-01.log,里面有10万条如下格式的记录:
  203.218.148.99 - - [01/Feb/2011:00:02:09 +0800] "GET /blog/2009/11/an_autobiography_of_yang_xianyi.html HTTP/1.1" 200 84058 "http://www.ruanyifeng.com/blog/2009/11/freenomics.html" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"
处理这个日志,我只用了一行代码:
  awk '$9 == 200 {print $7}' www-01.log | grep -i '^/blog/2011/.*\.html$' | sort | uniq -c | sed 's/^ *//g' > www-01.log.result
它用管道连接了5个命令,每一个都很简单,我们依次来看:
(1) awk '$9 == 200 {print $7}' www-01.log
awk命令默认用空格,将每一行文本分割成若干个字段。仔细数一下,我们需要的只是第7个字段,即http请求的网址,{print $7}表示将第7个字段输出,结果就是:
  /blog/2009/11/an_autobiography_of_yang_xianyi.html
考虑到我们只统计成功的请求,因此再加一个限制条件,服务器的状态代码必须是200(表示成功),写成"$9 == 200",即第9个字段必须是200,否则不输出第7个字段。
更精细的统计,还应该区分网络蜘蛛和真实访问者,由于我想不出简单的分辨方法,这里只好忽略了。
(2)grep -i '^/blog/2011/.*\.html$'
在输出的所有记录的第7个字段之中,并不是每一条记录都需要统计的。根据我的文章的命名特点,它们的网址应该都以"/blog/2011/"开头,以".html"结尾。所以,我用一个正则表达式"^/blog/2011/.*\.html$",找出这些记录。参数i表示不区分大小写。
(3)sort
这时,所有需要统计的记录应该都列出来了,但是它们的次序是杂乱的。接着,使用sort命令,不过目的不是为了排序,而是把相同的网址排列在一起,为后面使用uniq命令创造条件。
(4)uniq -c
uniq的作用是过滤重复的记录,只保留一行。c参数的作用,是在每行的开头添加该记录的出现次数。处理之后的输出应该是这样的:
  32 /blog/2011/01/guidelines_for_english_translations_in_public_places.html
  32 /blog/2011/01/api_for_google_s_url_shortener.html
  30 /blog/2011/01/brief_history_of_arm.html
它表示以上三篇文章,在1月1日的日志中,分别有32条、32条、30条的访问记录(即访问次数)。
(5)sed 's/^ *//g' > www-01.log.result
上一步uniq命令添加的访问次数,是有前导空格的。也就是说,在上例的32、32、30之前有一连串空格,为了后续操作的方便,这里把前导空格删去。sed命令是一个处理行文本的编辑器,'s/^ *//g'是一个正则表达式(^和*之间有一个空格),表示将行首的连续空格替换为空(即删除)。接着,将排序结果重定向到文件www-01.result。单个日志分析就完成了。
五、月度汇总排名
经过上一步之后,1月份的31个日志文件,生成了31个对应的分析结果文件。为了汇总整个月的情况,必须把这31个结果文件合并。
(6)合并分析结果
  for i in www-*.log.result
  do
    cat $i >> log.result
  done
这是一个循环结构,把所有www-01.log.result形式的文件,都写进log.result文件。
然后,我用一行语句,计算月度排名。
  sort -k2 log.result | uniq -f1 --all-repeated=separate |./log.awk |sort -rn > final.log.result
这行语句由3个命令和1个awk脚本组成:
(7)sort -k2 log.result
由于是31个文件汇总,log.result文件里面的记录是无序的,必须用sort命令,将相同网址的记录归类在一起。但是此时,访问次数是第一个字段,网址是第二个字段,因此参数k2表示根据第二个字段进行排序。
(8)uniq -f1 --all-repeated=separate
uniq的作用是过滤重复的记录,参数f1表示忽略第一个字段(访问次数),只考虑后面的字段(网址);参数表示all-repeated=separate,表示过滤掉所有只出现一次的记录,保留所有重复的记录,并且每一组之间用一个空行分隔。这一步完成以后,输出结果变成如下的形式:
  617 /blog/2011/01/guidelines_for_english_translations_in_public_places.html
  455 /blog/2011/01/guidelines_for_english_translations_in_public_places.html
  223 /blog/2011/01/2010_my_blogging_summary.html
  253 /blog/2011/01/2010_my_blogging_summary.html
相同网址都归在一组,组间用空行分割。为了简洁,上面的例子每一组只包含两条记录,实际上每一组都包含31条记录(分别代表当月每天的访问次数)。
(9)log.awk脚本
为了将31天的访问次数加总,我动了很多脑筋。最后发现,唯一的方法就是用awk命令,而且必须另写一个awk脚本。
  #!/usr/bin/awk -f
  BEGIN {
    RS="" #将多行记录的分隔符定为一个空行
  }
  {
    sum=0 #定义一个表示总和的变量,初值为0
    for(i=1;i<=NF;i++){ #遍历所有字段
      if((i%2)!=0){ #判断是否为奇数字段
        sum += $i #如果是的话,累加这些字段的值
      }
    }
    print sum,$2 #输出总和,后面跟上对应的网址
  }
我已经对上面这个log.awk脚本加了详细注释。这里再说明几点:首先,默认情况下,awk将"\n"作为记录的分隔符,设置RS=""表示改为将空行作为分隔符,因此形成了一个多行记录;其次,NF是一个awk的内置变量,表示当前行的字段总数。由于输入文件之中,每一行都包含两个字段,第一个是访问数,第二个是网址,所以这里做一个条件判断,只要是奇数字段就累加,偶数字段则一律跳过。最后,每个记录输出一个累加值和网址,它们之间用空格分割。
(10)sort -rn > final.log.result
对awk脚本的处理结果进行排序,sort默认使用第一个字段,参数r表示逆序,从大往小排;参数n表示以数值形式排序,不以默认的字典形式排序,否则会出现10小于2的结果。排序结果重定向到final.log.result。至此,月度排名完成。
六、脚本文件
用一个脚本,包含上面两节所有的内容。
  #!/bin/bash
  if ls ./*.result &> /dev/null #判断当前目录中是否有后缀名为result的文件存在
  then
    rm *.result #如果有的话,删除这些文件
  fi
  touch log.result #创建一个空文件
  for i in www-*.log #遍历当前目录中所有log文件
  do
    echo $i ... #输出一行字,表示开始处理当前文件
    awk '$9 == 200 {print $7}' $i|grep -i '^/blog/2011/.*\.html$'|sort|uniq -c|sed 's/^ *//g' > $i.result #生成当前日志的处理结果
    cat $i.result >> log.result #将处理结果追加到log.result文件
    echo $i.result finished #输出一行字,表示结束处理当前文件
  done
  echo final.log.result ... #输出一行字,表示最终统计开始
  sort -k2 log.result | uniq -f1 --all-repeated=separate |./log.awk |sort -rn > final.log.result #生成最终的结果文件final.log.result
  echo final.log.result finished #输出一行字,表示最终统计结束
这就是月度排名的最终脚本。编写的时候,我假定这个脚本和log.awk脚本与日志文件在同一个目录中,而且这两个脚本都具有执行权限。
年度排名的处理与此类似,就不再赘述了。
=================================================================

BloggerAds