2008年11月21日星期五

bash parse line by line

尋日寫個script去一行行咁process一個list(你可以當係ls 嘅result),上google search "bash parse line by line"搵極都搵唔到個啱我用嘅example.以我嘅性格,通常明知係得嘅我會堅持,亦即係正所謂嘅死硬派,今次我就選擇錯試(即係try an error).以我所知,bash會split object based on IFS,即係Input File Separator.於是我就寫左一個script如下:-

OLDIFS=${IFS}
IFS=\n
for line in `ls` ; do
echo "Line read is : ${line}"
done
IFS=${OLDIFS}

但係個output就唔太似樣:-

Line read is :-rw-r--r-- 1 root other 2797 Oct 21 2003 file1.txt -rw-r--r-- 1 root other 32797 Oct 22 2003 file2.txt ...

一睇返ls嘅result:-
ls
-rw-r--r-- 1 root other 2797 Oct 21 2003 file1.txt
-rw-r--r-- 1 root other 32797 Oct 22 2003 file2.txt

就知原來佢仲唔識睇\n,以為係"\"同"n"都係一個separator,之後當然試下用single quote, double quote, xx quote....去quote住個\n,但係result當然係一樣啦....真係比佢激死...玩足成日都無咩inspiration,於是就決定明日愁來明日當,立刻打道回府,照顧BB去也.今朝返到公司,突然就比我醒起,原來自己N個月前正好寫左個script,又係要line by line咁parse個file.所以都話,有時當你諗唔起點做嘅時候,最好都係唔好開OT,即刻返屋企hea下就反而會有新靈感.亦即係唔係比個死胡同困住,退一步,諗一諗,唔得嘅話就再停一停,先再諗一諗...哈哈...一睇之下,就發現原來自己太大意,其實只要睇下Advanced Bash Scripting Guide嘅Chapter 5 (Quoting),就會知道要OS真正了解\n=newline,就要用$加single quote...之後只係改左少少如下:-


OLDIFS=${IFS}
IFS=$'\n'
for line in `ls` ; do
echo "Line read is : ${line}"
done
IFS=${OLDIFS}

就成功左....真係有時最基本嘅嘢就最唔記得,比自己玩死..

2008年4月28日星期一

mailgraph on RHEL 3

為免mon住第二部mail server,我其實之前已經install過一次mail graph,但係好神奇咁成個mailgraph唔見左...

Anyway,可能自己之前發神經install錯左响第二部server上面啦,其實而家真係講咩都無意思,嘅然無左就再install過咪算囉...其實mailgraph嘅install都幾易,只係上去佢個website download個tar.gz落嚟.然後跟住佢個README做就得.不過世事點會咁易就搞得掂架...

為左唔駛下次又R哂頭都唔知點算,我就將今次嘅經驗寫低落嚟:-

cd tmp
mkdir mailgraph
cd mailgraph
wget http://mailgraph.schweikert.ch/pub/mailgraph-1.14.tar.gz

tar -xvzf mailgraph-1.14.tar.gz
cd mailgraph-1.14

cp mailgraph.cgi /var/www/cgi-bin/
cp mailgraph.pl /usr/local/bin
cp maiilgraph-init /etc/init.d

chkconfig --add mailgraph-init

本來以為已經搞掂,但係當然無咁易啦,一行"server maiilgraph-init start"就比佢鬧”mailgraph-init: unrecognized service”...但係我”chkconfig --list”又見”mailgraph-init 0:off 1:off 2:on 3:on 4:on 5:on 6:off”,之後試吓"chkconfig --del mailgraph-init"再用"chkconfig --add mailgraph-init" add佢一次,但係個result都係一樣.check左/etc/init.d/mailgraph-init响度,不過一睇"ls /etc/init.d"就發覺有d唔對路,點解個"mailgraph-init"咁特別唔同色嘅?哈哈,原來無有execute flag,行完"chmod +x /etc/init.d/mailgraph-init"之後當然搞掂啦,其實加多一句响個help度啫,都咁難?定expect人地一定知道呢?不過我估佢係要我地一定要清楚個permission setting,好事嚟嘅,不過都可以加一句架....唉!

Anyway,事情當然無咁快就完結啦,一行”service mailgraph-init start”佢就鬧:-

Starting mail statistics grapher: mailgraphCan't locate RRDs.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at /usr/local/bin/mailgraph.pl line 360.BEGIN failed--compilation aborted at /usr/local/bin/mailgraph.pl line 360.

但係我ls /usr/local就見有個rrdtool-1.0.49 install左,不過當然去睇/usr/lib/perl5就搵極都唔見果d "RRDx.pm" (x for 好多個咁解),又唔想自己copy過去, source又唔見左,於是咪去http://oss.oetiker.ch/諗住download latest stable嚟install囉,本來佢都有一份好詳細嘅documentation比我跟住嚟install,但係當install到最last一個step嘅時候就比佢鬧:-

/usr/bin/ld: cannot find -lpngcollect2: ld returned 1 exit status

有無搞錯啊...search左一陣internet都搵唔到咩頭緒,於是咪上去佢個website download返個舊version (1.0.50)嚟install,諗住實得添.Anyway,上網search到個relevant嘅installation help,不過當然無咁易放過我啦,當我行:-

perl -MCPAN -e shell
install Time::HiRes

佢就鬧我:-

NOTE: if you get an error like this (the Makefile line number may vary):Makefile:91: *** missing separatorthen set the environment variable LC_ALL to "C" and retryfrom scratch (re-run perl "Makefile.PL").(And consider upgrading your Perl to, say, at least Perl 5.8.8.)(You got this message because you seem to have an UTF-8 locale active in your shell environment, this used to cause broken Makefiles to be created from Makefile.PLs)Makefile:91: *** missing separator. Stop. /usr/bin/make -- NOT OKRunning make test Can't test without successful makeRunning make install make had returned bad status, install seems impossible

吓?係唔係玩咁咁盡啊?唉,又唔識點manual install,又唔想update去5.8.8,費事行梗嘅嘢唔work都唔知吖嗎...試過加"force install Time::HiRes"都係一樣.好彩,search左一陣就比我搵到有教我點manual install.於是我就跟住佢教咁:-

wget http://search.cpan.org/CPAN/authors/id/J/JH/JHI/Time-HiRes-1.9715.tar.gz
tar -xvzf Time-HiRes-1.9715.tar.gz
cd Time-HiRes-1.9715.tar.gz
perl Makefile.PL

點知都未make就比佢鬧

NOTE: if you get an error like this (the Makefile line number may vary):
Makefile:91: *** missing separator
then set the environment variable LC_ALL to "C" and retry
from scratch (re-run perl "Makefile.PL").
(And consider upgrading your Perl to, say, at least Perl 5.8.8.)
(You got this message because you seem to have
an UTF-8 locale active in your shell environment, this used
to cause broken Makefiles to be created from Makefile.PLs)
[root@mmsmtp Time-HiRes-1.9715]# make
Makefile:91: *** missing separator. Stop.

但係今次清楚要做咩:-

LC_ALL="C"
export LC_ALL

跟住都係真係跟住佢嘅instruction:-

perl Makefile.PL
make
make test
make install

然後就咁install File::Tail

perl -MCPAN -e shell
install File::Tail
quit

YEAH,點不知一行佢就無嘢睇,所有圖都corrupt哂,check過"/var/logl/httpd/access_log”同"/var/log/httpd/error_log"都無咩meaningful嘅message.好彩我有第二部server行梗,咁就梗係對吓兩邊嘅mailgraph.cgi睇吓有咩唔妥,點不知一睇就見比兩行錯path陰到添.

my $rrd = 'mailgraph.rrd'; # path to where the RRD database is
my $rrd_virus = 'mailgraph_virus.rrd'; # path to where the Virus RRD database is

大佬,要改你就話聲我聽咪得囉,唔駛咁陰濕架...搵到果d rrd database原來係一樣都係响/var/lib,於是就改果兩句,用返條啱嘅path.

my $rrd = '/var/lib/mailgraph.rrd'; # path to where the RRD database is
my $rrd_virus = '/var/lib/mailgraph_virus.rrd'; # path to where the Virus RRD database is

之後點?梗係work啦,唔好玩我啦...

postfix原來仲未好返。。。

上次update完個perl之後,個amavisd唔work,搞左成日終於都work返,之後又發覺個postfix仲係傻吓傻吓,佢成日話"Apr 28 11:30:09 gateway postfix/smtpd[22203]: NOQUEUE: reject: RCPT from abc.cde.com[108.118.128.138]: 450 4.1.1 <info@mydomain.com>: Recipient address rejected: User unknown in local recipient table; from=<user@cde.com> to=<info@mydomain.com> proto=ESMTP helo="....有無搞錯啊???User unknown in local recipient table?個aliases明明有呢個entry喎。。。

唉。。。睇吓個aliases同個aliases.db嘅timestamp又match喎,無理由,試吓reload佢睇吓點?點知一樣。去盡d咪又試吓restart都唔得,再試埋stop之後再restart都唔得。。。

真係有無搞錯,之後唯有試吓postalias hash:aliases用newaliases啦,點知真係work喎。。。無啦啦同個aliases斷左link?有無搞錯啊。。。真係比佢玩死。。。

諗諗吓,好似上次行yum update,佢update左個postfix,原本已經backup哂d config,update完即刻check都一樣,restart左個postfix諗住無咩事添,點知出左事都唔知,所以話don't fix when it aint break係真係至理明言。。。

2008年4月22日星期二

Compress::Zlib 無啦啦corrupt左

今日比人投訴,話成個月都收唔到email...入去mail server度睇,初初就以為咁啱無人send比佢啫,因為睇d log都似正常,懶懶地咪用個gmail send張嚟試吓,點知等左成個鐘都收唔到添...於是就再慢慢比多d心機睇吓個maillog,點知見到”mailgateway postfix/lmtp[17734]: connect to 127.0.0.1[127.0.0.1]: Connection refused (port 10024)”吓???10024咪就係amavisd囉...跟住仲發現個process真係無左添...

明明行左半年都無事,又會發啦啦嘅...諗吓諗吓,又真係喎,點解近排我無收到worm嘅通知呢?(因為config到一收到有worm嘅email就通知我,雖然我都無嘢做到,不過得個知字都好啊...)於是就梗係試吓start返個process啦,點不知又比佢鬧鬼我:

Starting amavisd: ERROR: MISSING REQUIRED BASIC MODULES: Compress::ZlibBEGIN failed--compilation aborted at /usr/local/sbin/amavisd line 160. [FAILED]
我梗係睇吓有無install到啦,做左個"yum list all grep perl"
perl-Compress-Zlib.i386 1.42-1.fc6 installed
吓?咁即係點啊?
我先remove "yum remove perl-Compress-Zlib.i386"之後再install,結果一樣...

又係唔理性嘅時候,唔理佢咁多再用"perl -MCPAN -e shell"之後行"install Compress::Zlib"梗係fail啦,到左咁唔理性嘅時候就梗係來硬嚟啦,今次仲”force install Compress::Zlib".之後梗係install ok架,不過個case都係一樣囉....唔理性到極點就去試吓download最新嘅amavis stable version 2.5.4試吓啦:-

Starting amavisd: ERROR: MISSING REQUIRED BASIC MODULES: Compress::ZlibBEGIN failed--compilation aborted at /usr/local/sbin/amavisd line 171. [FAILED]

呢個做法,只係將個complain嘅line no 由160變左做171...即係問題仲”未解決”到啦...

R哂頭,明明响度架,又比我搵到有人教’perl -MCompress::Zlib -e'print "Found\n"'’哈哈,行完終於都有少少頭緒喇,因為佢鬧:-

is only avaliable with the XS version at /usr/lib/perl5/site_perl/5.8.8/Compress/Zlib.pm line 9BEGIN failed--compilation aborted at /usr/lib/perl5/site_perl/5.8.8/Compress/Zlib.pm line 9.Compilation failed in require.BEGIN failed--compilation aborted.
yeah,睇嚟要做一次recompile,於是就走入CPAN個shell做一次recompile,即係”perl -MCPAN -e shell"之後打"recompile".等左半個鐘左右,哈哈,真係work喎!!Yeah...但係唔明點解無啦啦d library會corrupt左,不過真係比佢玩死...

諗吓諗吓,我好似行過”yum update”去update部機喎,又好似有d perl library update左喎...如果係唔得嘅話,仲咩比我update啊???

2008年4月14日星期一

ArcServe Installation Part 2

上回講到installation script行完左,不過究竟點先行到個GUI呢?

其實佢個GUI係一個web interface(如果network down左又start唔到個x咪等死?).先用cstatus睇吓係唔係所有service都start哂.

[root@server sbin]# ./cstatus
caservd 20020
cadiscovd 20022 20021
cauthd 20024
caloggerd 20025
cadbd 20026
camediad 20127 20126 20027
caqd 20028
staging 20139
cprocess

cacommd 1763 20015
httpd 20005 19891 1755

dbclean
MergeCat

如果httpd未start呢,就去csetup check吓enable左個http未?如果ready哂呢,就可以用cstop去stop哂d service然後就用cstart去start返哂.之後就係最tricky嘅地方.如果你係有用iptable嘅話,就要enable下面果d port:-

TCP 6072,6060,6051
UDP 6051, 41524

之後就應該搞掂哂,不過都係果句,本manual有差唔多等於無,你唔睇哂係唔會識用...無得試試吓架...