2009年10月11日 星期日

How To Count Inodes For Each Directory

Hostgator whom this website is hosted with limits you to 50,00o inodes per account. So it not unlimited as they claim but nonetheless its pretty good hosting ..

Here is a script that I wrote which helps you count each inodes in a directory

#!/bin/bash
# count inodes for each directory
LIST=`ls`
for i in $LIST; do
echo $i
find $i -printf "%i\n" | sort -u | wc -l
done
Also this line seems good for figuring out which directory is using up the most space

du -ks ./* | sort -n

2009年10月9日 星期五

Linux Ramdisk安裝教學2

我目前是使用這個方法
$ mkdir /path/to/ramdisk
$ echo "#partition of ramdisk
ramdiskTitle /path/to/ramdisk tmpfs defaults,size=2000M 0 0" >> /etc/fstab
$ mount -a

2009年10月8日 星期四

如何方便的找出容量很大的資料夾

find / -type d | xargs -i du -sh {} | sort -n > /tmp/biggggggg.log

2009年10月7日 星期三

於CentOS 5將snmpd log從messages中獨立出來

由於snmpd記錄筆數相當多,造成在閱讀messages其他log的不便,因此將snmpd的log用另一個檔案獨立紀錄。
假設將snmpd的log記錄到syslog facility local0中:

首先在syslog中設定local0
/etc/syslog.conf

local0.* /var/log/snmpd.log
接著更改snmpd的參數
/etc/sysconfig/snmpd.option

OPTIONS="-Ls 0-4 0 -A -Lf /dev/null -p /var/run/snmpd.pid"
再來重新啟動syslogd與snmpd

service syslog restart
service snmpd restart

摘自:http://kaien.wikidot.com/linux:rhel-snmp-syslog

2009年10月6日 星期二

非mmcache!Memcached的應用:多網站伺服器 PHP 共享 Session

請注意是 Memcached 不是 mmcache,很多人搞不清楚他們兩個的不同!多半玩過 PHP 的人大概都聽過 mmcache,它是一個預編譯緩衝的 PHP 加速程式,能夠提升 PHP 的執行效能。但很少人聽過 Memcached ,因為大多人乍看之下都以為它是mmcache,使得它沒什麼機會介紹自己。事實上,若您正打算架構一個真正高負載的大型網站系統,你需要了解的並不是 mmcache,而是 memcached。

Memcached 是什麼?顧名思義,他是由記憶體(Memory)和暫存(cache)所組合起來的常駐程式(Daemon),你也可以稱它為『暫存伺服器』。 Memcached 能提供一個暫存資料的服務,透過網路供其他電腦使用。Memcached 有什麼用途?最常見的應用就是在網站伺服器的叢集,它能讓許多的網站伺服器 Session 互相流通使用。如果你正在傷透腦筋煩惱這一點,恭喜你找到解決方法了!

想要在網站伺服器的叢集中,多網站伺服器 Session 互相流通使用,首先你必須將 Memcached 架起來當 Session 分享伺服器,這邊建議你使用大的記憶體,最好是能多大就有多大,因為 Memcached 並不會以硬碟當資料暫存,而是會完全跑在記憶體上,所以若記憶被用完了,Memcached 就會無法再存放更多資料。

接著,你必須修改 PHP 的 Session Save Handler,讓 PHP 懂得利用 Memcached Server 存放 PHP 的 Session 資料並能從 Memcached Server 取出 Session 的資料。PHP提供了 session_set_save_handler() 函式讓我們能輕易修改 Session Save Handler ,以下是我修改後的 PHP 程式碼,你必須在呼叫 session_start() 之前使用:




define("SHARED_SESS_TIME", 3600); // Timeout

// Session Class by Fred

class Shared_Session
{
function init()
{
ini_set("session.use_trans_sid", 0);
ini_set("session.gc_maxlifetime", SHARED_SESS_TIME);
ini_set("session.use_cookies", 1);
ini_set("session.cookie_path", "/");
ini_set("session.cookie_domain", ".yourdomain.com.tw");

session_module_name("user");
session_set_save_handler(
array("Shared_Session", "open"),
array("Shared_Session", "close"),
array("Shared_Session", "read"),
array("Shared_Session", "write"),
array("Shared_Session", "destroy"),
array("Shared_Session", "gc")
);
}

function open($save_path, $session_name) {
return true;
}

function close() {
return true;
}

function read($sesskey) {
global $memcache;

return $memcache->get($sesskey);
}

function write($sesskey, $data) {
global $memcache;

$memcache->set($sesskey, $data, SHARED_SESS_TIME);

return true;
}

function destroy($sesskey) {
global $memcache;

$memcache->delete($sesskey);
$memcache->flush_all();

return true;
}

function gc($maxlifetime = null) {
return true;
}
}

$GLOBALS["memcache"] = memcache();
$GLOBALS["memcache"]->add_server("192.168.1.1", 11211);
$GLOBALS["memcache"]->add_server("192.168.1.2", 11211);
Shared_Session::init();

?>


其中粗字體的部分,是要特別修改的地方:

3600 是 Session 的生命周期﹝以秒為單位﹞,這應該不用再做太多解釋。
yourdomain.com.tw 是你的網域名稱:想像一個情況若是 Loadbalance 在用戶第一次連線分配用戶到A伺服器,第二次連線分配給同一用戶到B伺服器,會導致 B 伺服器無法透過 cookies 取得 A 伺服器分配給用戶的 session_id,因為 cookies 無法跨網域存取,解決方法是必須修改 cookies 的網域設定,讓 www1.yourdomain.com.tw、www2.yourdomain.com.tw、www3.yourdomain.com.tw...等等,都可以共同存取同一個 cookies ,以取得同一個 session_id,故此時你必須設定成為『.yourdomain.com.tw』。
192.168.1.1 這是你的 Memcached Server 的 IP 位置,這裡值得提的是 add_server() 方法,你可以有多行設定許多 IP 做 Loadbalance 負載分配,前面也講到 Memcached 是純粹使用記憶體,若其中一台機器記憶體滿了,本方法可以從中找到另一台可用的機器使用。故你可以建立一個 Memcached 的叢集來處理 Session。

因為我偷懶, Memcached 的安裝方法就沒寫了,去求助 google 大神,它應該會告訴你更多詳細的安裝資料。其實 Memcached 除了可應用在 Session 共享上,也可以應用在資料庫的資料暫存緩充,降低SQL Server負擔以提升速度。Memcached多好用?就看你怎麼用了!

摘自:http://fred-zone.blogspot.com/2006/08/mmcachememcached-php-session.html

2009年10月5日 星期一

pause in setTimeout

Hi, I have implemented a wrapper for "pausing a timed functional call using setTimeout" function in javascript. Here is the code ..

function Animator_Class(){
this.TID = null ;
this.funvar = null;
this.interval = null;
this.start_time = null;
}

Animator_Class.prototype.start_timer = function(){
this.TID = window.setTimeout(this.funvar , this.interval);
this.start_time = (new Date()).getTime();
}


Animator_Class.prototype.pause_timer = function(){
var curr_time = (new Date()).getTime();
var elapsed_time = curr_time - this.start_time ;
this.interval = this.interval - elapsed_time;
window.clearTimeout(this.TID);
}

Animator_Class.prototype.setTimeout = function(funvar, interval){
this.funvar = funvar;
this.interval = interval;
this.TID = window.setTimeout(funvar,interval);
this.start_time = (new Date()).getTime();
}
Animator_Class.prototype.clearTimeout = function(){
window.clearTimeout(this.TID);
}

Now if you want to use it in your code, use like this,,

TOB = new Animator_Class();
TOB.setTimeout('alert("OK")', 5000);

and in html forms use like,,





simple but important hack,,,,,,,,, :)

摘自:http://techfandu.blogspot.com/2009/02/pause-in-settimeout.html

2009年10月2日 星期五

Optimize your URLs , Best practices for crawling & indexing

Agenda

1.Context: Why do you care?
2.Reduce inefficient crawling of your site
 1.Avoid maverick coding practices
 2.Remove user-specific details from URLs
 3.Optimize dynamic URLs
 4.Rein in infinite spaces
 5.Disallow actions Googlebot can’t perform
2.Get your preferred URLs indexed
3.Resources


Why do you care?
The Internet from > 10^6feet

How do search engines deal with this?

Focus on efficiency in below steps:
1.Discover unique content
2.Prioritize crawling
 1.Crawl new content
 2.Refresh old content
 3.Crawl fewer duplicates
3.Keep all the good stuff in the index
4.Return relevant search results

Funnel your crawling “budget” toward your most important content


Avoid maverick coding practices
Discourage alternative encodings
shop.example.com/items/Periods-Styles__end-table_W0QQ_catrefZ1QQ_dmptZAntiquesQ5fFurnitureQQ_flnZ1QQ_npmvZ3QQ_sacatZ100927QQ_trksidZp3286Q2ec0Q2em282
Where [W0 = ?] and [QQ= &]

Eliminate expand/collapse "parameters"
www.example.com/ABN/GPC.nsf/MCList?OpenAgent&expand=1,3,15

Remove user-specific details from URLs
Remove from the URL path
www.example.com/cancun+hotel+zone-hotels-1-23-a7a14a13a4a23.html
www.example.com/ikhgqzf20amswbqg1srbrh55/index.aspx?tpr=4&act=ela
Creates infinite URLs to crawl

Difficult to understand algorithmically
Keywords in name/value pairs are just as good as in the path
www.example.com/skates/riedell/carrera/
www.example.com/skates.php?brand=riedell&model=carrera

Optimize dynamic URLs
Dynamic URLs contain name/value pairs
skates.php?size=6&brand=riedell

Create patterns for crawlers to understand
www.example.com/article.php?category=1&article=3&sid=123
www.example.com/article.php?category=1&article=3&sid=456
www.example.com/article.php?category=2&article=3&sid=789

Use cookies to hide user-specific details
www.example.com/skates.php?query=riedell+she+devil&id=9823576
www.example.com/skates.php?ref=www.fastgirlskates.com&color=red

Rein in infinite spaces
Uncover issues in CMS
www.example.com/wiki/index.php?title=Special:Ipblocklist&limit=250&offset=423780&ip=

Disallow actions Googlebot can’t perform
Googlebot is too cheap to ‘Add to cart’
Disallow shopping carts
http://www.example.com/index.php?page=EComm.AddToCart&Pid=3301674647606&returnTo=L2luZGV4LnBocD9wYWdlPUVDb21tLlByb2QmUGlkPTMzMDE2NzQ2NDc2OTI=

Googlebot is too shy to ‘Contact us’
Disallow contact forms, especially if they have unique URLs
http://www.example.com/bb/posting.zsp?mode=newtopic&f=2&subject=Seeking%20information%20about%20roller%20derby%20training

Googlebot forgets his password a lot
Disallow login pages
https://www.example.com/login.asp?er=43d9257de47d8b08a91069cccb584ab83ff21140bd46e81656dab3507f45d1ab079cd77244231e557d724dc1df1a641

Get your preferred URLs indexed
Set your preferred domain in Webmaster Tools
www.example.com vs. example.com

Put canonical URLs in your Sitemap

Use the new rel=“canonical” on any duplicate URLs


Get feedback in Webmaster Tools

摘自:http://docs.google.com/present/view?id=dgk2ft62_18cvjx4nk4

wibiya widget