2012-04-29 21:16

[Linux] 檢查 SAMBA 與 NFS Server 是否存在

通常會透過 /etc/fstab 來處理掛載的設定,然後再使用 mount -a 來重新確認掛載,最好在排程的程序用到掛載目錄時也執行一次 mount -a,掛載目錄在斷線後是不會自動回復的,mount -a 的 Timeout 其實還蠻久的,尤其是 Server 不存在的時候,所以最好還是用對應的 client 先確認 server 是否存在。

而檢查 NFS 的 client 可以用 showmount 來處理,在 Ubuntu 上的安裝方式如下:
sudo aptitude install nfs-common

而 SAMBA 的 client 則是用 smbclient,在 Ubuntu 上的安裝方式如下:
sudo aptitude install smbclient


檢查 NFS Server 是否存在的流程

以 Shell 的方式檢查
  1. # 先以 client 確認 server 是否存在 
  2. /sbin/showmount 192.168.0.6 >/dev/null 2>&1 
  3. if [ "j$?" != "j0" ]; then   
  4.    echo "NFS Server is not exist" 
  5.    exit 1 
  6. fi 
  7.  
  8. # 重新確認掛載  
  9. mount -a >/dev/null 2>&1 
  10. if [ "j$?" != "j0" ]; then 
  11.    echo "NFS Server mount failed" 
  12.    exit 1; 
  13. fi 

以 PHP 的方式檢查
  1. /*先以 client 確認 server 是否存在*/  
  2. $state = shell_exec('/sbin/showmount 192.168.0.6 >/dev/null 2>&1; echo $?'); 
  3. if(trim($state)!='0'){ 
  4.    echo "NFS Server is not exist"; 
  5.    exit; 
  6. } 
  7.  
  8. /*重新確認掛載*/  
  9. if(shell_exec('mount -a 2>&1')){ 
  10.    echo "NFS Server mount failed" 
  11.    exit; 
  12. } 



檢查 SAMBA Server 是否存在的流程

以 Shell 的方式檢查
  1. # 先以 client 確認 server 是否存在 
  2. smbclient -NL //192.168.0.6 >/dev/null 2>&1 
  3. if [ "j$?" != "j0" ]; then   
  4.    echo "SAMBA Server is not exist" 
  5.    exit 1 
  6. fi 
  7.  
  8. # 重新確認掛載  
  9. mount -a >/dev/null 2>&1 
  10. if [ "j$?" != "j0" ]; then 
  11.    echo "SAMBA Server mount failed" 
  12.    exit 1; 
  13. fi 

以 PHP 的方式檢查
  1. /*先以 client 確認 server 是否存在*/  
  2. $state = shell_exec('smbclient -NL //192.168.0.6 >/dev/null 2>&1; echo $?'); 
  3. if(trim($state)!='0'){ 
  4.    echo "SAMBA Server is not exist"; 
  5.    exit; 
  6. } 
  7.  
  8. /*重新確認掛載*/  
  9. if(shell_exec('mount -a 2>&1')){ 
  10.    echo "SAMBA Server mount failed" 
  11.    exit; 
  12. } 
2012-04-23 16:47

[PHP] 簡易的圖片相似度比較

由於相似图片搜索的php实现的 API 不怎麼符合我的用途,所以我重新定義 API 的架構,改寫成比較簡單的函數方式,雖然還是用物件的方式包裝。

  1. <?php 
  2. /** 
  3. * 圖片相似度比較 
  4. * 
  5. * @version     $Id: ImageHash.php 4429 2012-04-17 13:20:31Z jax $ 
  6. * @author      jax.hu 
  7. * 
  8. * <code> 
  9. *  //Sample_1 
  10. *  $aHash = ImageHash::hashImageFile('wsz.11.jpg'); 
  11. *  $bHash = ImageHash::hashImageFile('wsz.12.jpg'); 
  12. *  var_dump(ImageHash::isHashSimilar($aHash, $bHash)); 
  13. * 
  14. *  //Sample_2 
  15. *  var_dump(ImageHash::isImageFileSimilar('wsz.11.jpg', 'wsz.12.jpg')); 
  16. * </code> 
  17. */ 
  18.  
  19. class ImageHash { 
  20.  
  21.    /**取樣倍率 1~10 
  22.     * @access public 
  23.     * @staticvar int 
  24.     * */ 
  25.    public static $rate = 2; 
  26.  
  27.    /**相似度允許值 0~64 
  28.     * @access public 
  29.     * @staticvar int 
  30.     * */ 
  31.    public static $similarity = 80; 
  32.  
  33.    /**圖片類型對應的開啟函數 
  34.     * @access private 
  35.     * @staticvar string 
  36.     * */ 
  37.    private static $_createFunc = array( 
  38.        IMAGETYPE_GIF   =>'imageCreateFromGIF', 
  39.        IMAGETYPE_JPEG  =>'imageCreateFromJPEG', 
  40.        IMAGETYPE_PNG   =>'imageCreateFromPNG', 
  41.        IMAGETYPE_BMP   =>'imageCreateFromBMP', 
  42.        IMAGETYPE_WBMP  =>'imageCreateFromWBMP', 
  43.        IMAGETYPE_XBM   =>'imageCreateFromXBM', 
  44.    ); 
  45.  
  46.  
  47.    /**從檔案建立圖片 
  48.     * @param string $filePath 檔案位址路徑 
  49.     * @return resource 當成功開啟圖片則回傳圖片 resource ID,失敗則是 false 
  50.     * */ 
  51.    public static function createImage($filePath){ 
  52.        if(!file_exists($filePath)){ return false; } 
  53.  
  54.        /*判斷檔案類型是否可以開啟*/ 
  55.        $type = exif_imagetype($filePath); 
  56.        if(!array_key_exists($type,self::$_createFunc)){ return false; } 
  57.  
  58.        $func = self::$_createFunc[$type]; 
  59.        if(!function_exists($func)){ return false; } 
  60.  
  61.        return $func($filePath); 
  62.    } 
  63.  
  64.  
  65.    /**hash 圖片 
  66.     * @param resource $src 圖片 resource ID 
  67.     * @return string 圖片 hash 值,失敗則是 false 
  68.     * */ 
  69.    public static function hashImage($src){ 
  70.        if(!$src){ return false; } 
  71.  
  72.        /*缩小圖片尺寸*/ 
  73.        $delta = 8 * self::$rate; 
  74.        $img = imageCreateTrueColor($delta,$delta); 
  75.        imageCopyResized($img,$src, 0,0,0,0, $delta,$delta,imagesX($src),imagesY($src)); 
  76.  
  77.        /*計算圖片灰階值*/ 
  78.        $grayArray = array(); 
  79.        for ($y=0; $y<$delta; $y++){ 
  80.            for ($x=0; $x<$delta; $x++){ 
  81.                $rgb = imagecolorat($img,$x,$y); 
  82.                $col = imagecolorsforindex($img, $rgb); 
  83.                $gray = intval(($col['red']+$col['green']+$col['blue'])/3)& 0xFF; 
  84.  
  85.                $grayArray[] = $gray; 
  86.            } 
  87.        } 
  88.        imagedestroy($img); 
  89.  
  90.        /*計算所有像素的灰階平均值*/ 
  91.        $average = array_sum($grayArray)/count($grayArray); 
  92.  
  93.        /*計算 hash 值*/ 
  94.        $hashStr = ''; 
  95.        foreach ($grayArray as $gray){ 
  96.            $hashStr .= ($gray>=$average) ? '1' : '0'; 
  97.        } 
  98.  
  99.        return $hashStr; 
  100.    } 
  101.  
  102.  
  103.    /**hash 圖片檔案 
  104.     * @param string $filePath 檔案位址路徑 
  105.     * @return string 圖片 hash 值,失敗則是 false 
  106.     * */ 
  107.    public static function hashImageFile($filePath){ 
  108.        $src = self::createImage($filePath); 
  109.        $hashStr = self::hashImage($src); 
  110.        imagedestroy($src); 
  111.  
  112.        return $hashStr; 
  113.    } 
  114.  
  115.  
  116.    /**比較兩個 hash 值,是不是相似 
  117.     * @param string $aHash A圖片的 hash 值 
  118.     * @param string $bHash B圖片的 hash 值 
  119.     * @return bool 當圖片相似則回傳 true,否則是 false 
  120.     * */ 
  121.    public static function isHashSimilar($aHash, $bHash){ 
  122.        $aL = strlen($aHash); $bL = strlen($bHash); 
  123.        if ($aL !== $bL){ return false; } 
  124.  
  125.        /*計算容許落差的數量*/ 
  126.        $allowGap = $aL*(100-self::$similarity)/100; 
  127.  
  128.        /*計算兩個 hash 值的漢明距離*/ 
  129.        $distance = 0; 
  130.        for($i=0; $i<$aL; $i++){ 
  131.            if ($aHash{$i} !== $bHash{$i}){ $distance++; } 
  132.        } 
  133.  
  134.        return ($distance<=$allowGap) ? true : false; 
  135.    } 
  136.  
  137.  
  138.    /**比較兩個圖片檔案,是不是相似 
  139.     * @param string $aHash A圖片的路徑 
  140.     * @param string $bHash B圖片的路徑 
  141.     * @return bool 當圖片相似則回傳 true,否則是 false 
  142.     * */ 
  143.    public static function isImageFileSimilar($aPath, $bPath){ 
  144.        $aHash = ImageHash::hashImageFile($aPath); 
  145.        $bHash = ImageHash::hashImageFile($bPath); 
  146.        return ImageHash::isHashSimilar($aHash, $bHash); 
  147.    } 
  148.  
  149. } 
2012-04-12 14:03

[Ubuntu] 時間同步設定

繼上次Ubuntu 時間及時區設定 [Linux]那篇文章後,最近有學到新的時間同步的設定方式。

vim /etc/cron.daily/ntpdate
  1. #!/bin/bash 
  2.  
  3. # Sync NTP Server 
  4. ntpdate -s tock.stdtime.gov.tw watch.stdtime.gov.tw time.stdtime.gov.tw clock.stdtime.gov.tw tick.stdtime.gov.tw ntp.ubuntu.com pool.ntp.org 
  5.  
  6. # Update BIOS time 
  7. hwclock --systohc 

chmod +x /etc/cron.daily/ntpdate
2012-04-11 21:09

[MySQL] 整數型態的大小

類型Byte肯定位數最大位數最大值無符號最大值
TINYINT123127255
SMALLINT2453276765535
MEDIUMINT378838860716777215
INT491021474836474294967296
BIGINT81920922337203685477580718446744073709551615