Archive for September, 2010

gearmand vs php的server&client&job安装,使用

wget http://launchpad.net/gearmand/trunk/0.9/+download/gearmand-0.9.tar.gz
tar zxvf gearmand-0.9.tar.gz
cd gearmand-0.9/
./configure
make
make install
/sbin/ldconfig
cd ../
wget http://pecl.php.net/get/gearman-0.7.0.tgz
tar zxvf gearman-0.7.0.tgz
cd gearman-0.7.0
/usr/local/php/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config –with-gearman
make
make install
cd ../

configure php的时候出现这个
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
http://sourceforge.net/projects/re2c/files/
wget http://sourceforge.net/projects/re2c/files/re2c/0.13.5/re2c-0.13.5.tar.gz/download
tar -zxvf re2c-0.13.5.tar.gz
cd re2c-0.13.5
./configure && make && make install

1 Comment

php array占用内存测试

<?php
function convert($size)
 {
    $unit=array(‘b’,'kb’,'mb’,'gb’,'tb’,'pb’);
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).’ ‘.$unit[$i];
 }

$start=memory_get_usage(true);
$test=array();
for($i=0;$i<100000;$i++)
        $test[md5($i)]=1;
echo convert(memory_get_usage(true) – $start);

结果:17.25 mb

意思就是10万个32位的array key,占用内存17.25M

No Comments

php利用syslog函数分布式将log集中到中央log服务器

使用php的syslog函数,调用本地的syslog服务,然后从本地syslog发送到中央syslog服务器进行分析整理,配置如下

首先我们先写一个php的测试文件

<?php
define_syslog_variables();
openlog(“TextLog”, LOG_PID, LOG_LOCAL0);

$data = date(“Y/m/d H:i:s”);
syslog(LOG_DEBUG,”Messagge: $data”);

closelog();
?>
注意,我们用local0这个openlog所以在本地的syslog.conf中需配置local0具体配置如下

vi /etc/syslog.conf
local0.*                @index-server
保存退出以后/etc/init.d/syslog restart

index-server是中央服务器的hostname也可以是IP地址

然后我们配置中央服务器的syslog
vi /etc/sysconfig/syslog
将SYSLOGD_OPTIONS=”-m 0″改成SYSLOGD_OPTIONS=”-m 0 -r” 
存盘退出
vi /etc/syslog.conf
local0.*                        /var/log/php.log
#这里就是从各个机器打过来的log放在哪个文件里。。这个文件可能会比较大,建议对这个文件作每日存档
#还有local0.debug 可以打在/var/log/php_debug.log中类似这种。。。可以自由发挥。。
存盘退出以后/etc/init.d/syslog restart

在远端机执行t.php
Sep  2 04:17:58 app01 TextLog[2408]: Messagge: 2010/09/02 04:17:56
你看。。app01的log已经写入index-server的php.log文件中了!是不是很简单,很好用??至于t.php如何封装一下,优化一下。。这个也太简单了。。自己琢磨吧!

No Comments

和php的getmypid相关的一些函数,挺有用的

<?php

/**
 * Check for a current process by filename
 * @param $file[optional] Filename
 * @return Boolean
 */
function processExists($file = false) {

    $exists     = false;
    $file       = $file ? $file : __FILE__;

    // Check if file is in process list
    exec(“ps -C $file -o pid=”, $pids);
    if (count($pids) > 1) {
        $exists = true;
    }
    return $exists;
}

?>

&lt;?php
/*

mixed getpidinfo(mixed pid [, string system_ps_command_options])

this function gets PID-info from system ps command and return it in useful assoc-array,
or return false and trigger warning if PID doesn't exists

$pidifo=getpidinfo(12345);

print_r($pidifo);

Array
(
    [USER] =&gt; user
    [PID] =&gt; 12345
    [%CPU] =&gt; 0.0
    [%MEM] =&gt; 0.0
    [VSZ] =&gt; 1720
    [RSS] =&gt; 8
    [TT] =&gt; ??
    [STAT] =&gt; Is
    [STARTED] =&gt; 6:00PM
    [TIME] =&gt; 0:00.01
    [COMMAND] =&gt; php someproces.php &gt; logfile
)

*/

//////////////////////////////////////////////

function getpidinfo($pid, $ps_opt="aux"){

   $ps=shell_exec("ps ".$ps_opt."p ".$pid);
   $ps=explode("\n", $ps);
  
   if(count($ps)&lt;2){
      trigger_error("PID ".$pid." doesn't exists", E_USER_WARNING);
      return false;
   }

   foreach($ps as $key=&gt;$val){
      $ps[$key]=explode(" ", ereg_replace(" +", " ", trim($ps[$key])));
   }

   foreach($ps[0] as $key=&gt;$val){
      $pidinfo[$val] = $ps[1][$key];
      unset($ps[1][$key]);
   }
  
   if(is_array($ps[1])){
      $pidinfo[$val].=" ".implode(" ", $ps[1]);
   }
   return $pidinfo;
}

?&gt;

No Comments