Thursday, June 30, 2011

PHP5-Memcached 釘文

Q1 31 Jun
PHP Warning: Unknown: Failed to write session data (memcached). Please verify that the current setting of session.save_path is correct (tcp://127.0.0.1:11211) in Unknown on line 0

成因:
$mcdhost = '127.0.0.1';
$mcdport = '11211';
ini_set('session.save_handler', 'memcached'); 有d
ini_set('session.save_path', $session_save_path);
其中$session_save_path = "$mcdhost:$mcdport";
不是$session_save_path = "tcp://$mcdhost:$mcdport";僅用於php5-memcache

Q2 31 Jun
PHP Warning: Unknown: Failed to write session data (memcached). Please verify that the current setting of session.save_path is correct (127.0.0.1:11211) in Unknown on line 0

成因:
故意停止Memcached 服務器後運行Memcached::addServer(),本打算用if/try{}catch 判斷若不能連接Memcached Server則用回files形式儲存,雖php手冊中指addServer()返回為TRUE或FALSE但判斷未成功,只出現上述警告
這方面用php5-memcache的Memcache::connect()較好, 因為若不成功連接會返回false。出現的錯誤用@抑止後不影响session header的輸出,退回用file儲存session方式

http://www.gocar.idv.tw/phpbb3/viewtopic.php?f=24&p=79
試例子只適用於php5-memcache
在php.ini 中預設session.save_handler = files 和 session.save_path = /path_to_file/

Q3 31 Jun
用Memcached server處理http session cookie,但有個問題是未能控制session cookie何時因閒置而過期問題。
是否與只在php程序中用ini_set()修改了session handler 而非在php.ini中修改,因此garbage collection 不會在預設1440秒後清除閒置的session?
1 July
在PHP5-MEMCACHE(NO d)的PHP script 中設了 int_set('session.gc_maxlifetime', 20); telnet查看該session 雖然不一定刪除該key,但Memcached server 還是會清去其內的value,但此轉到用PHP5-MEMCACHED(WITH D)中在Pphp.ini中設了session.save_handler = memcached及session.gc_maxlifetime = 30,當中的key-value即使逾時還是會保留,打開verbose -vv tail -f /var/log/memcached.log 觀察,有定期delete key 的紀錄(可能因client端的app定期更新行為)

2008年一篇對memcached garbage collector 的討論電郵
http://lists.danga.com/pipermail/memcached/2008-May/006849.html
Memcache(d) to store PHP sessions 比較php-memcache和php-memcached
http://beeznest.wordpress.com/2011/04/18/memcached-to-store-php-sessions/

php5-memcached 不會清除過期的session(的值, key會保留),試過改session.gc_maxlifetime session.gc_probability/session.gc_divisor =1/1 但仍然無效,估計可能在寫入memcached時expire time為0(即不過期)有關,未找到如何修改,或者需要重新override session_save_handler()。用PHP5-Memcach則會自動清理,改用這。

memcached 與 memcache比較及其它

除了結尾的一個d字差別外(有指結尾的d代表demand守護進程),因為名字混用引起混淆,今日在Debian5.0和Ubuntu10.04LTS試用後理解如下

memcached本身為一個服務守護進程,只有memcacheD没所講的memcache,獨立運行,類似apache server. 會接受客戶端如PHP擴展連線。

而php之下有兩個packges 一個名為php5-memcached 而另一個為php5-memcache,兩者角色一樣同為客戶端,功能略有不同可參考http://www.leonzhang.com/2011/06/24/memcached-vs-php-memcache/和php手冊
memcache http://www.php.net/manual/en/book.memcache.php
memcached http://www.php.net/manual/en/book.memcached.php

舊版Debian 5(backport sources.list) apt-cache 没有php5-memcached只有php5-memcache。相反較新的Ubuntu 10只有php5-memcached
Debian: ~# apt-cache search php5-memcached (有d)
没內容顯示

Debian5: ~# apt-cache search php5-memcache (没有d)
php5-memcache - memcache extension module for PHP5

Ubuntu10:~# apt-cache search php5-memcached
php5-memcached - memcached extension module for PHP5

dpkg -L php5-memcache 或dpkg --get-selections 查看已安裝軟件包

安裝
若要使用memcache,需完整安裝服務器memcached和php對應的客戶端(memcache與memcachd比較) 免混淆,皆加上php5-前綴
安裝server
apt-get install memcached

安裝php擴展
apt-get install php5-memcached

客戶端用法大致相同,會有d字之別,可參閱php手冊。

FAQ - Iterate all items - not design for
http://code.google.com/p/memcached/wiki/FAQ#Can_I_iterate_the_items_of_the_memcached_server?

wiki
http://code.google.com/p/memcached/wiki/NewStart

protocol
http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
https://github.com/trondn/memcached/blob/master/doc/protocol.txt
中英對照
http://www.jb51.net/shouce/memcached/index.html

一些memcache的特別注意地方
http://www.caiapps.com/memcached-for-load-balancing-sessions/
http://www.caiapps.com/duplicate-key-problem-in-memcache-php/

自訂session handler 用 memcache
http://www.keboola.com/blog/php-sessions-with-memcached-and-a-database-session-in-the-cloud-done-right/
Memcache(d) to store PHP sessions 比較php-memcache和php-memcached
http://beeznest.wordpress.com/2011/04/18/memcached-to-store-php-sessions/

替代品
http://www.mohawksoft.org/?q=node/8

Wednesday, June 29, 2011

Android ad-hoc tether problem

昨日升級華為tether軟件, 希望解決不能3G共享給其它android機問題,其中有句講到明原來採用ad-hoc mode 共享, android2.1的client不能以這種方式連接(apple 機無問題)。疑惑了半年的問題終於有答案,但未有解決方法。唯有等升級。

http://gfans.bryan.tw/2010/10/20/1243
任何一套wireless tether軟體都是將3.5G透過Ad-hoc模式分享

http://forum.xda-developers.com/showthread.php?t=553508
Shocking! Hero cannot see ad-hoc network
見第八貼

Thursday, June 23, 2011

Java 中Thread 和 Runnable

Java 中Thread 和 Runnable都可以作為多線程的程序中使用,兩者中不同是
Thread 是一個Class
Runnable 是一個Interface

通過extends Thread,可獲得start(), sleep(), 並覆蓋run(), 使用時直接用start(),但有時不太方便通過繼承(extends)而進行多線程,例如已繼承了其它Class(Java 不允許多重繼承)時,就應該實現(implements) Runnable寫多線程程序。

注意,implements Runnable創建的只有run()方法而没有start(),因此要利用其它機制來啟動。這要創建兩個instances,第一個是Runnable的,第二個是Thread的,然後把第一個當成參數傳給第二個

例子:
private class RunnableThread implements Runnable {
public void run(){ // Runnable's run method
//do something while run

try {
//Static Method from Thread class, coz Runnable no such method.
Thread.sleep(100);
}catch (InterruptedException e)
{ }
}
}

Thread aRunnThread = new Thread(new RunnableThread());
aRunnThread.start(); //其中start()會調用runnable中的run()

==============
一般Thread寫法
private class GenThread extends Thread{
public void run() {
//do thread job
...
try{
sleep(300); //release cpu to do other job
}catch(InterruptedException e)
{.....}
}
}

//on other class to call it
Thread theGenThread = new GenThread();
theGenThread.start();

Sunday, June 19, 2011

vi

http://blog.csdn.net/linrui6/archive/2009/02/10/3875644.aspx

http://www.tuxfiles.org/linuxhelp/vimcheat.html

http://www2.nsysu.edu.tw/csmlab/unix/vi_command.htm

http://www.vixual.net/blog/archives/234

Friday, June 17, 2011

關於DNS 網文集

鳥哥
http://linux.vbird.org/linux_server/0350dns.php

nslookup 使用示例
http://faculty.ndhu.edu.tw/~comput/computer_c/training/hbc0122/dns-1.htm
http://tw.myblog.yahoo.com/chen_10220214/article?mid=890&next=832&l=f&fid=6
http://elouai.com/nslookup-reference.php
http://www.computerhope.com/unix/unslooku.htm

Trouble Shoot
http://www.microsoft.com/taiwan/technet/itsolutions/network/support/trblshoot/dnstshot.aspx?mfr=true

Ubuntu
Bind9安裝設置指南
http://wiki.ubuntu.org.cn/index.php?title=Bind9%E5%AE%89%E8%A3%85%E8%AE%BE%E7%BD%AE%E6%8C%87%E5%8D%97&variant=zh-tw

https://help.ubuntu.com/8.04/serverguide/C/dns-configuration.html

https://help.ubuntu.com/community/BIND9ServerHowto

Round Robin DNS records for web server load balance
http://mingwang0824.pixnet.net/blog/post/27709276
http://www.zytrax.com/books/dns/ch9/rr.html

TTL 值
http://www.zytrax.com/books/dns/info/minimum-ttl.html

Thursday, June 16, 2011

JQuery Mobile 網文集

JQM與google map
http://code.google.com/p/jquery-ui-map/

JQM 範例: 混合android apps運行的新聞閱讀器
http://mobile.tutsplus.com/tutorials/mobile-web-apps/jquery_android/

其它http://miamicoder.com/2011/interesting-jquery-mobile-tutorials/

自訂ICON
http://www.interactivebynature.net/blog/wordpress/2011.04.04.jquery-mobile-custom-icons

JSON
http://www.jquery4u.com/json/15-jquery-json-plugins/
http://tomcoote.co.uk/code-bank/jquery-json-suggestsearch-box/ 完整suggest search例子
http://www.ibm.com/developerworks/xml/tutorials/x-jquerymobilejsontut/index.html

JSONP 範例
http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

JSONP 原理示例
http://www.jqueryload.com/jsonp-twitter-example-how-to-access-the-twitter-api-and-get-tweets-without-pass-through-the-server

Saturday, June 11, 2011

Debian root password reset

因為之前Debian不能正常關機而強制apci shutdown,今日再次開機原來的登入密碼報錯, 懷疑關時未正確保存. 據網上文章重設一次, 可正常登入.
貼文如下, 以備日後查找:
現在我們一般使用grub作為系統的引導程式,而lilo進入單用戶的方法很簡單,這裏就不提了。
對於一些版本的OS(如redhat)來說,編輯grub,在後面加上“single”即可進入單用戶模式。
但是對於debian來說,這種方式是無效的,因為debian進入單用戶模式也需要你輸入root用戶的密碼。

下面介紹一個在所有版本linux的grub上均可以使用的方法:

1、在Grub的引導裝載程式功能表上,選擇你要進入的條目,鍵入 “e” 來進入編輯模式。
2、在第二行(類似於kernel /vmlinuz-2.6.15 ro root=/dev/hda2 ),鍵入”e”進入編輯模式;
3、在句末加上”init=/bin/bash”,回車;
4、按“b”啟動系統。
這樣我們即可獲得一個bash的shell。

進入shell後,根分區的掛載模式默認是唯讀,我們要將其改為可寫,否則不能更改root密碼:

mount -no remount,rw /
passwd root
sync
mount -no remount,ro /
reboot
這樣即可成功更改debian的root用戶的密碼。
http://moto.debian.org.tw/viewtopic.php?t=11346

Monday, June 06, 2011

Collectd - Linux 操作系統狀態紀錄及圖示化安裝筆記

http://collectd.org/
http://wiki.debian.org/SystemMonitoring

開啟Plugin exec
<plugin exec="">
Exec user "path/to/myscript"
</plugin>
myscript 內容
#!/bin/bash
echo test message
now
在/var/log/syslog輸出如下
Jun 9 01:17:03 DBS collectd[10066]: exec plugin: Prepending `PUTVAL' to this line: Thu Jun 9 01:17:03 HKT 2011
Jun 9 01:17:13 DBS collectd[10066]: exec plugin: Prepending `PUTVAL' to this line: test message!


設定Collectd-exec示例
http://flux242.blogspot.com/2011/01/collectd-mod-exec-part-1.html

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

Mail list
http://dir.gmane.org/gmane.comp.monitoring.collectd

錯誤信息
collectd[4533]: mysql plugin: The `Port' config option needs exactly one string argument.
4.7版後 Port 3306
4.6版後 Port "3306"


停止apache2 Log檔記錄
因每十秒在/var/log/apache2/access.log產生一行紀錄如下
localhost - - [19/Jun/2011:07:00:13 +0800] "GET /server-status?auto HTTP/1.1" 200 477 "-" "collectd/4.8.2"
apache2 log 可據條件進行記錄,將以下加入/etc/apache2/sites-available/default設定檔中
# Mark requests from the loop-back interface
#SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog #來源IP, 没使用, 免影响記錄其它本機的請求
#SetEnvIf Remote_Host "localhost" dontlog #來源主機名, 没使用
SetEnvIf User-Agent "collectd/4.8.2" dontlog #User-Agent 為Collectd, 不記錄.
#SetEnvIf Request_URI "^/server-status" dontlog #未知是否成功.
CustomLog /var/log/apache2/access.log combined env=!dontlog

!!以上需在/etc/apache2/sites-available/default中註去CustomLog /var/log/apache2/access.log combined, 否則只接受最後者設定

試過在apache2.conf加入上設定, 但會視為other virtualhost 的紀錄
而混進(包括default的log!!)/var/log/apache2/other_vhosts_access.log

CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined
互相干擾, 故SetEnvIf的CustomLog不放在apache2.conf中.

Debian sources.list 使用舊版軟件

以前使用的hk.debian.org改了sources.list不能用,後來 亂改用了一段十分長的時間, 雖然可用但還是會收到一些軟件包錯誤提示,近日發現原來放置軟件包的目錄結構改動了,應改是早几年已改,主要原因是我OS依然使用舊版本(5.0乃至4.0)且好長一段時間没有升級。但設定上卻使用stable main (當前最新) deb http://ftp.hk.debian.org/debian/ stable main contrib non-free

新的目錄結構是把一些舊的版本Deabian 放在oldstable。設定如下:
deb http://ftp.hk.debian.org/debian/ oldstable main contrib non-free (後面兩類按需)
另外Debian社群為一些使用舊版系統但希望使用新版軟件包的提供了一個方法和網站,詳見:
http://backports.debian.org/

collectd 安裝筆記
原來Debian 5.0 lenny所附collectd為4.4太舊,想安裝5.0(只有源程序),查找軟件包列表只有4.10只不屬於lenny,不允許安裝。
先修改sources.list 只有一句如下:
deb http://backports.debian.org/debian-backports lenny-backports main
運行apt-cache showpkg collectd 可顯示軟件包版本和資訊
Package: collectd
Versions:
4.10.1-1+squeeze1~bpo50+1
...
安裝特定版本軟件包apt-get install package_name=version
apt-get install collectd=4.10.1-1+squeeze1~bpo50+1

注:collectd安裝後在/var/log/syslog顯示部份lib(lt_ldopen: mysql.so 等 file not found, 要自己動手
再安裝一些如libmysqlclient16(又依賴mysql-common ver.5.1.49-3~bpo50+1 )
http://collectd.org/faq.shtml