1. 當process CPU loading超過90%, 就先行刪除:
#!/bin/bash
CPULOADING=90
ps auxh | \
while read pro
do
set -- $pro
if [ `echo $3 | cut -d"." -f1` -gt $CPULOADING ]; then
kill $2
fi
done
2.當process數目超過50時, 就先行刪除:
#!/bin/bash
PSCOUNT=50
ps axh -o "cmd" | sort | uniq -d -c | \
while read ps
do
set -- $ps
if [ $1 -gt $PSCOUNT ]; then
killall $2
fi
done
Reference: Linux網路安全管理與監控
Saturday, June 06, 2009
Friday, June 05, 2009
[Shell script] Monitor Disk space
#!/bin/bash
USAGE_SPACE=20
USAGE_SPACE=20
df | grep '[0-9]%' | egrep 'sd|hd' | \
while read line
do
# if [ `echo $line | awk -F" " '{ print $5 }' | cut -d% -f1` -gt "$USAGE_SPACE" ]; then
# echo "Disk space of `echo $line | awk -F" " '{ print $6}'` belongs to `hostname` is over $USAGE_SPACE%" | mail -s "HD DISK is not enough" $USER
# fi
set -- $line
if [ `echo $5 | cut -d% -f1` -gt $USAGE_SPACE ]; then
echo "Disk space of `echo $6` belongs to `hostname` is over $USAGE_SPACE%" | mail -s "HD DISK is not enough" $USER
fi
done
Sunday, May 31, 2009
Eason Chen Moving on stage 21 part II
昨晚陪老婆去台北小巨蛋聽陳奕迅Moving on stage 21 part II的演唱會,老實說我並非Eason的歌迷,對他的歌比較熟悉的是只有K歌之王,你的背包,婚禮的祝福(這張專輯我有買)。
整體聽下來我可以了解為何老婆這麼喜歡Eason了,他真的很有實力,歌曲確實能打動人心, 感覺上他真的唱得很賣力,連唱了四個小時吧!有幸能參加這種實力派歌手的演唱會真是我們的幸福,可惜我只聽到11:00多就先行離場回家去哄小朋友睡覺了,不然真的很想跟Eason待到最後。
Anyway Eason期待你下一次再來台北開唱!婚禮的祝福真是太好聽了!
Wednesday, May 20, 2009
Moving SUSE Linux to another system
工作用的一台PC可能太過老舊,常常跑到一半就system freeze,懷疑是熱當,然而剛好有一台idle的機器,想說就將hard drive換到那台機器就好了,本以為應該會很順利,沒想到系統重開時SUSE回報:
waiting for device /dev/hda2 to appear
waiting for device /dev/hda4 to appear
然後整個系統就停止了.... 事情似乎沒有我想像得單純..... :p
進入rescue mode查看hard drive也沒損壞呀? 只好上google查看看吧~
後來查到原來是當更換到另一台機器時,由於主機板上的controller是不一樣的! SUSE需要重新make initrd, Redhat不用! 呵呵.... 整個救援的方法如下:
hard drive partition information:
/dev/hda1 -> /boot
/dev/hda2 -> swap
/dev/hda3 -> /home
/dev/hda4 -> /
1. 利用SUSE安裝光碟進入rescue mode
2. 將root partition掛載: mount /dev/hda4 /mnt
3. 將rescue mode的/dev與/proc分別掛載到 /mnt/dev與/mnt/proc: mount --bind /dev /mnt/dev; mount --bind /proc /mnt/proc
4. 將目前的rescue mode的single-root轉成實際上的root partition: chroot /mnt
5. 將/boot掛載: mount /dev/hda1 /boot
6. 切換到/boot目錄,並執行mkinitrd: cd /boot; mkinitrd
Accomplished!
waiting for device /dev/hda2 to appear
waiting for device /dev/hda4 to appear
然後整個系統就停止了.... 事情似乎沒有我想像得單純..... :p
進入rescue mode查看hard drive也沒損壞呀? 只好上google查看看吧~
後來查到原來是當更換到另一台機器時,由於主機板上的controller是不一樣的! SUSE需要重新make initrd, Redhat不用! 呵呵.... 整個救援的方法如下:
hard drive partition information:
/dev/hda1 -> /boot
/dev/hda2 -> swap
/dev/hda3 -> /home
/dev/hda4 -> /
1. 利用SUSE安裝光碟進入rescue mode
2. 將root partition掛載: mount /dev/hda4 /mnt
3. 將rescue mode的/dev與/proc分別掛載到 /mnt/dev與/mnt/proc: mount --bind /dev /mnt/dev; mount --bind /proc /mnt/proc
4. 將目前的rescue mode的single-root轉成實際上的root partition: chroot /mnt
5. 將/boot掛載: mount /dev/hda1 /boot
6. 切換到/boot目錄,並執行mkinitrd: cd /boot; mkinitrd
Accomplished!
Monday, May 18, 2009
快速取得路由路徑資訊
在Linux下,要確定或查看到某一個destination的方法,比較傳統的方法是執行route -n 或是netstat -rn取得routing table後,再以肉眼判斷讀出,不過我看過有些人管控的機器其routing table內容非常之可怕! 眼睛可能會看到"脫窗", 所幸我今天發現到了一個新的指令可以幫助苦命的IT/MIS.
# ip route get x.x.x.x
-> 系統就會回報到x.x.x.x會經由哪個gateway出去, e.g.,
ip route get 168.95.1.1
168.95.1.1 via 192.168.1.254 dev br0 src 192.168.1.100
cache mtu 1500 advmss 1460 hoplimit 64
解讀: 從src 192.168.1.100到dst 168.95.1.1是透過192.168.1.254出去
# ip route get x.x.x.x
-> 系統就會回報到x.x.x.x會經由哪個gateway出去, e.g.,
ip route get 168.95.1.1
168.95.1.1 via 192.168.1.254 dev br0 src 192.168.1.100
cache mtu 1500 advmss 1460 hoplimit 64
解讀: 從src 192.168.1.100到dst 168.95.1.1是透過192.168.1.254出去
Tuesday, May 12, 2009
[Shell script] 讓/etc/passwd更利於閱讀
#!/bin/bash
IFS=:
echo "System User list:"
echo "-------------------------------------"
while read username shapass uid gid comm home shell
do
echo "UserName:$username"
echo " 1.password:$shapass"
echo " 2.uid:$uid"
echo " 3.gid:$gid"
echo " 4.comment:$comm"
echo " 5.home:$home"
echo " 6.default shell:$shell"
echo "------------------------------------"
done < /etc/passwd
參考Linux-101 Hacks, hack 88. Read data file fields inside a shell script
IFS=:
echo "System User list:"
echo "-------------------------------------"
while read username shapass uid gid comm home shell
do
echo "UserName:$username"
echo " 1.password:$shapass"
echo " 2.uid:$uid"
echo " 3.gid:$gid"
echo " 4.comment:$comm"
echo " 5.home:$home"
echo " 6.default shell:$shell"
echo "------------------------------------"
done < /etc/passwd
參考Linux-101 Hacks, hack 88. Read data file fields inside a shell script
Monday, May 11, 2009
Get Ready for openSUSE Community Week!
The first openSUSE Community Week is just around the corner. May 11 through May 17 we’ll be hosting live sessions in IRC to help grow the openSUSE Community.
Community week is all about helping new contributors get started with openSUSE and getting existing contributors together to mentor new contributors, and working together on major projects.
We’ll be hosting a week of IRC tutorials, Q&A’s, and jam sessions on a number of topics. Learn to work with the testing/QA team, create packages, work with the openSUSE Build Service, help work on the wiki, or get involved with the GNOME and KDE teams. There’s something for everybody.
Schedule:
http://en.opensuse.org/Community_Week
Community week is all about helping new contributors get started with openSUSE and getting existing contributors together to mentor new contributors, and working together on major projects.
We’ll be hosting a week of IRC tutorials, Q&A’s, and jam sessions on a number of topics. Learn to work with the testing/QA team, create packages, work with the openSUSE Build Service, help work on the wiki, or get involved with the GNOME and KDE teams. There’s something for everybody.
Schedule:
http://en.opensuse.org/Community_Week
Tuesday, May 05, 2009
Ubuntu and Upstart
Ubuntu與Fedora都採用了upstart模擬跑SysV. 但是OpenSUSE11.1/SLES11 whereas.... 還是採用大家熟悉的SysV, 讓我們來看看Novell的concern吧:
http://en.opensuse.org/Ubuntu_and_Upstart
是不是差點沒說出脫褲子放屁~ XD
http://en.opensuse.org/Ubuntu_and_Upstart
是不是差點沒說出脫褲子放屁~ XD
Thursday, April 23, 2009
wget useful parameters
遇到了所以在此做個紀錄,雜記一下:
# wget -r -np -A img,txt,gz,pcap,cap http://xxx.xxx.xxx
-r --recursive
-np --no-parent
-A --accept=LIST
-> 如此一來就會對xxx.xxx.xxx的網站以遞迴的方式,抓取以img,txt,gz,pcap與cap類型的檔案,其它的都會忽視。
更多的參數可透過wget --help或是man wget獲得更進一步的資訊。
# wget -r -np -A img,txt,gz,pcap,cap http://xxx.xxx.xxx
-r --recursive
-np --no-parent
-A --accept=LIST
-> 如此一來就會對xxx.xxx.xxx的網站以遞迴的方式,抓取以img,txt,gz,pcap與cap類型的檔案,其它的都會忽視。
更多的參數可透過wget --help或是man wget獲得更進一步的資訊。
Monday, April 13, 2009
忽視記錄指定的指令於history中
上次介紹可用HISTCONTROL="ignorespace"來忽視開頭為"空白"的指令記錄於history中,今天再提供另一種可行的方法,當看到特定的指令時,也將不記錄於history中,實作的方法如下:
Usage: HISTIGNORE="cmd1:cmd2"
Example1: 忽視記錄pwd,useradd,userdel,passwd等指令:
# export HISTIGNORE="pwd:useradd:userdel:passwd"
Example2: 開機即套用:
Redhat/SUSE: echo 'export HISTIGNORE="pwd:useradd:userdel:passwd"' >> /etc/profile
Note: 如果系統不允許重開機,可以透過source /etc/profile來生效。
Friday, April 10, 2009
不紀錄開頭為space的指令於history中
直接來看如何實作吧!
舉個常會碰到的例子, 如果在console下需輸入敏感的資料時, 不要讓它記錄到history中:
#HISTCONTROL=ignorespace
# echo "redhat" | passwd --stdin lawrence (注意開頭必需為空白)
#history
如此一來echo "redhat" | passwd --stdin lawrence 就不會記錄到history中.
舉個常會碰到的例子, 如果在console下需輸入敏感的資料時, 不要讓它記錄到history中:
#HISTCONTROL=ignorespace
# echo "redhat" | passwd --stdin lawrence (注意開頭必需為空白)
#history
如此一來echo "redhat" | passwd --stdin lawrence 就不會記錄到history中.
Wednesday, April 08, 2009
Modify Linux prompt
Cocoa Packet Analyzer in OSX
以前帽客要在OSX下抓網路封包, 有兩種方法:
1. 開啟終端機, 執行tcpdump -> 只有文字模式
2. 安裝X11-> 透過某個在OSX下能安裝Linux apps的工具(Fink)安裝wireshark -> 有圖形可看了, 但是挺麻煩.
現在不用囉! 直接給它安裝Cocoa Packet Analyzer就可以在OSX下抓網路封包啦!
About Cocoa Packet Analyzer
A native Mac OS X implementation of a network protocol analyzer and packet sniffer. It is able to do basic network capturing (incl. packet filtering), analyze and display packet trace files. CPA supports the industry-standard PCAP packet capture format for reading, capturing and writing packet trace files. Further more the libPCAP/ tcpdump filter expressions may be used during capturing network traffic. It supports a variety of network protocols and can be enhanced using the Cocoa bundle plugin technology. Features a quicklook plugin so you can get an overview of you trace file even in finder.
Support libpcap format, 太棒了正是我需要的, 因為我 慣用的packet playback tools只支援libpcap format.
Download: http://www.apple.com/downloads/macosx/networking_security/cocoapacketanalyzer.html
1. 開啟終端機, 執行tcpdump -> 只有文字模式
2. 安裝X11-> 透過某個在OSX下能安裝Linux apps的工具(Fink)安裝wireshark -> 有圖形可看了, 但是挺麻煩.
現在不用囉! 直接給它安裝Cocoa Packet Analyzer就可以在OSX下抓網路封包啦!
About Cocoa Packet Analyzer
A native Mac OS X implementation of a network protocol analyzer and packet sniffer. It is able to do basic network capturing (incl. packet filtering), analyze and display packet trace files. CPA supports the industry-standard PCAP packet capture format for reading, capturing and writing packet trace files. Further more the libPCAP/ tcpdump filter expressions may be used during capturing network traffic. It supports a variety of network protocols and can be enhanced using the Cocoa bundle plugin technology. Features a quicklook plugin so you can get an overview of you trace file even in finder.
Support libpcap format, 太棒了正是我需要的, 因為我 慣用的packet playback tools只支援libpcap format.
Download: http://www.apple.com/downloads/macosx/networking_security/cocoapacketanalyzer.html
Friday, April 03, 2009
透過ac command 顯示所有使用者登入的時間
今天看書,學習到利用ac指令來獲取系統上所有使用者上線的總時間,帽客覺得這是非常實用的指令,故於此分享給大家,以下是ac的簡單說明: (man page description)
ac prints out a report of connect time (in hours) based on the logins/logouts in the current wtmp file. A total is also printed out.
ok, 再來以上線跑不久的OpenSUSE11.1來做個例子:
Example1: 顯示每天所有使用者上線的時間總和:
linux-xgby:~ # ac -d
Mar 7 total 28.81
Mar 9 total 0.14
Mar 13 total 17.35
Mar 15 total 32.28
Mar 20 total 8.09
Mar 21 total 3.00
Mar 22 total 44.08
Mar 23 total 10.44
Mar 24 total 33.19
Mar 25 total 29.43
Mar 31 total 0.03
Apr 1 total 15.17
Apr 2 total 4.47
Apr 2 total 14.60
Apr 3 total 21.09
Today total 1.67
Example2: 顯示個別使用者上線的時間總和:
linux-xgby:~ # ac -p
lawrence 258.67
root 5.31
total 263.98
如何,夠簡單又實用吧!
ac prints out a report of connect time (in hours) based on the logins/logouts in the current wtmp file. A total is also printed out.
ok, 再來以上線跑不久的OpenSUSE11.1來做個例子:
Example1: 顯示每天所有使用者上線的時間總和:
linux-xgby:~ # ac -d
Mar 7 total 28.81
Mar 9 total 0.14
Mar 13 total 17.35
Mar 15 total 32.28
Mar 20 total 8.09
Mar 21 total 3.00
Mar 22 total 44.08
Mar 23 total 10.44
Mar 24 total 33.19
Mar 25 total 29.43
Mar 31 total 0.03
Apr 1 total 15.17
Apr 2 total 4.47
Apr 2 total 14.60
Apr 3 total 21.09
Today total 1.67
Example2: 顯示個別使用者上線的時間總和:
linux-xgby:~ # ac -p
lawrence 258.67
root 5.31
total 263.98
如何,夠簡單又實用吧!
Thursday, April 02, 2009
To compile polygraph network benchmark tools on CentOS5.2
What's polygraph?
"Web Polygraph is a freely available performance testing tool for caching proxies, origin server accelerators, L4/7 switches, content filters, and other Web intermediaries. Polygraph's features include:
high-performance HTTP clients and servers
realistic HTTP and SSL traffic generation
HTTP Basic, NTLM, and Negotiate proxy authentication
LDAP-friendly generation of user credentials
flexible content simulation
ready-to-use standard workloads for benchmarking
powerful domain-specific configuration language
portable open-source implementation
friendly commercial support "
http://www.web-polygraph.org/
在CentOS5.2下編譯polygraph v3.1.5時會發生SslWrap.cc的問題,解決的方法是:
http://www.linuxquestions.org/questions/linux-server-73/problem-compiling-web-polygraph-689766/
./configure;make;make install 好搞定了! Enjoy it!
"Web Polygraph is a freely available performance testing tool for caching proxies, origin server accelerators, L4/7 switches, content filters, and other Web intermediaries. Polygraph's features include:
high-performance HTTP clients and servers
realistic HTTP and SSL traffic generation
HTTP Basic, NTLM, and Negotiate proxy authentication
LDAP-friendly generation of user credentials
flexible content simulation
ready-to-use standard workloads for benchmarking
powerful domain-specific configuration language
portable open-source implementation
friendly commercial support "
http://www.web-polygraph.org/
在CentOS5.2下編譯polygraph v3.1.5時會發生SslWrap.cc的問題,解決的方法是:
http://www.linuxquestions.org/questions/linux-server-73/problem-compiling-web-polygraph-689766/
./configure;make;make install 好搞定了! Enjoy it!
Thursday, March 26, 2009
Novell SLES11 Server/Desktop is available to download
哇哈哈~ Novell SLES11 Server/Desktop總算是釋出了,目前家裡一台P4 2.4GHz的老PC跑OpenSUSE 11.0讓我非常滿意,之前有聽說過SLES11是基於OpenSUSE 11.1開發的。
打算先找台NB裝一下看看,BTW Novell SLES11與SLES10一樣提供60天的免費更新,哈!還算ok!
謎之音: 之前在某家公司內部所架設的SLES10 <- 提供挺多services 沒當過,不知還在運作否 XD
Monday, March 16, 2009
Friday, March 13, 2009
Linux管理小技巧: CDPATH
預設上, RHEL5.x與CentOS5.x的bind有chroot的機制, 所以當系統管理者要去更改named.conf或是相關的DNS zonefile時就必需要輸入/var/named/chroot/etc/ or /var/named/chroot/var/named/ 恩... 有點長, tab要按好幾次, 於是乎帽客想到了一個好用的偷懶方法, CDPATH.
透過CDPATH可以設定parent directory然後透過cd指令切入到subdirectories, 看起來有點抽象? 實際看看例子吧:

上面的例子是設定CDPATH為DNS Server chroot的路徑(/var/named/chroot), 並將它變成環境變數, 之後只要輸入cd etc就會切入到/var/named/chroot/etc, 而cd var/named就會切入到/var/named/chroot/var/named.
看起來方便多了吧!

透過CDPATH可以設定parent directory然後透過cd指令切入到subdirectories, 看起來有點抽象? 實際看看例子吧:

上面的例子是設定CDPATH為DNS Server chroot的路徑(/var/named/chroot), 並將它變成環境變數, 之後只要輸入cd etc就會切入到/var/named/chroot/etc, 而cd var/named就會切入到/var/named/chroot/var/named.
看起來方便多了吧!
update:
讓CDPATH重開機時就生效吧:
echo "CDPATH=/var/named/chroot" >> /root/.bash_profile
2009.3.16 update:
今天發現到bash內建的command,pushd與popd也有著類似的功能:

先將/var/named/chroot 透過pushd新增至stack中,並用dirs確定目前stack的內容(第一筆永遠是current directory),之後透過cd就可以切換到subdirectories。
Thursday, March 12, 2009
Tuesday, March 10, 2009
Compile Linux kernel "2.6.28.7" on CentOS5.2
想玩看看EXT4,所以下載了最新版有支援EXT4的kernel於CentOS5.2上,以下是compile的雜記:
1. 於/usr/src/kernels/解開2.6.28.7的kernel source (download: www.kernel.org)
2. 將目前的kernel config 從/boot下copy到/usr/src/kernels/2.6.28.7/.config
2. 將目前的kernel config 從/boot下copy到/usr/src/kernels/2.6.28.7/.config
3. 於/usr/src/kernels/2.6.28.7執行make menuconfig
-> Add EXT4
-> Remove Bluetooth
-> Remove wireless
4. 執行make bzImage
5. 執行make modules
6. 執行make modules_install
7. 執行make install
8. 檢查/boot/*, /boot/grub/grub.conf
-> 開機選用kernel 2.6.28.7開機。
2009.3.25 update:
Tested ext3 and ext4 performance on kernel 2.6.29 and result as below:
It looks like ext4 is better than ext3.
Note: To clean HD cache for each test processing by hdparm -f command.
Note: To clean HD cache for each test processing by hdparm -f command.
Subscribe to:
Posts (Atom)


