Jul 31

linux – the meaning of the cpu summary info from top command

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
What does these things mean? Here is a small explanation for each section :-

  1. us -> User CPU time: The time the CPU has spent running users’ processes that are not niced.
  2. sy -> System CPU time: The time the CPU has spent running the kernel and its processes.
  3. ni -> Nice CPU time: The time the CPU has spent running users’ proccess that have been niced.
  4. wa -> iowait: Amount of time the CPU has been waiting for I/O to complete.
  5. hi -> Hardware IRQ: The amount of time the CPU has been servicing hardware interrupts.
  6. si -> Software Interrupts.: The amount of time the CPU has been servicing software interrupts.
  7. id is idle, in other words CPU idle status
  8. st is Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown

This shows a breakup of CPU usage, depending on your servers role, you need to optimize it. If you have a lot of disk writing keep a watch on iowait. If might be wondering what does “The time the CPU has spent running users’ processes that are not niced.” mean? If you do a “man nice”, it will say “nice – run a program with modified scheduling priority“. It is called “nice” because the number that is given to a process determines how willing a task is to step aside and let other tasks monopolize the processor. The number varies from -20 to 19. The default value is 0, higher values lower the priority and lower values increase it. If you want to read more about nice, visit http://wiki.linuxquestions.org/wiki/Nice.

http://aji.techshu.com/top/

Jul 25

javascript – how to add/remove css class from a dom element

To change css style of a dom element,we usually operate directly on the style property of the element.for example:

document.getElementById('Id'').style.visibility = "visible";

what if we don’t want to operate in this way,we need to apply a list of css rule to the element.in this case,we just need to specify a new css class for the element.

document.getElementById('Id'').class = "newClass";

The code above will not work at all.

document.getElementById('Id').className = "newClass";

The code above will set the className to newClass.To only add or remove,the code will be a little more:

Source code    
document.getElementById("MyElement").className += " MyClass";
Source code    
document.getElementById("MyElement").className =
   document.getElementById("MyElement").className.replace
      ( /(?:^|\s)MyClass(?!\S)/ , '' )

or we can just

Source code    
document.getElementById('id').classList.add('class');
document.getElementById('id').classList.remove('class');
Jul 25

grep – how to use grep command

First,let’s take a look at the example,

Source code    
grep -rn 'order->info\[' .

This grep command search recursively in the current directory for all files which containing the string order->info[ . From this example, we  know:
-r option instruct grep to search recursively in all directory and sub-directory.
with -n option, grep will output the number of the line in the text file from which it was matched.
and what's more, the special character need to be escaped. like the [ character.

Source code    
grep -c 'word' /path/to/file

The command above with -c optionwill count how many times the word was matched in the specified file.
To let grep to ignore case while searching, we need to add -i option,so we can adapt the above command to be:

Source code    
grep -ic 'word' /path/to/file

From grep command's man page,we can get more detail:
--color option will hep to highlight the matched string in the out with color(defined by GREP_OPTIONS environment variables)
-v option will invert the grep (output/action) result. by default,grep will print only the matched lines.with -v option,it will only print those lines that do not matched.
-I  option process a binary file as if it did not contain matching data; that is not to process the binary file(i think so)
-m the max time matched. grep will stop searching after specified time match.
-E work together with -e option,treat the string as a pattern.search as an extended regular expression.Keep in mind that -e option is not necessary if the pattern(search string) not start with -.Let's take a look at the following example:

Source code    
grep -E 'DIR_WS_IMAGES|DIR_WS_INCLUDES' includes/configure.php

search both two string in the file.as the pattern not start with -,so -e option can be omitted.
Now comes the last three option i would like to introduce:
-A NUM print NUM line of trailing context after matching lines.
-B NUM Print NUM lines of leading context before matching lines.
-C NUM Print NUM lines of output context.

Source code    
grep /home/oakleysu/ -m 1 -B 5 /usr/local/apache/conf/httpd.conf

 

Well,grep is really powerful and helpful in linux server admin and programming.but this post is just my own opinion on using grep to help in my work. you can take a look at the man page to get more info.

 

Jul 23

linux – how to strip leading components from file names before extraction tar ball

just in case you meet the following problem:

we have a tar ball source.tar.gz containing the following files,

/abc/includes/zen-cart/a.php
/abc/includes/zen-cart/b.php
/abc/includes/zen-cart/c.php
/abc/includes/zen-cart/includes/function.php
/abc/includes/zen-cart/classes/db.php

you only need to extract all php file to current directory,you don’t need any other directory from the tar ball.in the other words,you do not want to keep the leading directory structure,but only the content itself.how to do that.yes,you can just extract it and do some mv and rm command.in fact tar has a builtin option which can strip components from file name before extraction,it’s the –strip-components NUMBER, –strip-path NUMBER.so you can do that with the following command:

Source code    
tar -xvzf surce.tar.gz  --strip-components  3

pls be noted tar-1.14 uses –strip-path and tar-1.14.90+ uses –strip-components.

Jul 20

zen cart — sort the product attribute option value

With the help of zen cart easy populate module,we can easy import products to zen cart store.however,the problem come when we tried to import attribute data(size,color etc.).we don’t know how to specified the attribute option order.and we don’t want to specify order for each attribute option.why not just sort the arribute option by its id.then in the product view page,the attribute’s option will be listed in the same order as the data sheet we imported.To implement this feature,we just need to make a change as below:
—- includes/modules/attributes.php

Source code    
<?php
...
...
//change the $order_by  variable
$order_by = ' order by pov.products_options_values_id';
 
while (!$products_options_names->EOF) {
    $products_options_array = array();
 
    /*
    pa.options_values_price, pa.price_prefix,
    pa.products_options_sort_order, pa.product_attribute_is_free, pa.products_attributes_weight, pa.products_attributes_weight_prefix,
    pa.attributes_default, pa.attributes_discounted, pa.attributes_image
    */
 
    $sql = "select    pov.products_options_values_id,
            pov.products_options_values_name,
            pa.*
  from      " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov
  where     pa.products_id = '" . (int)$_GET['products_id'] . "'
  and       pa.options_id = '" . (int)$products_options_names->fields['products_options_id'] . "'
  and       pa.options_values_id = pov.products_options_values_id
  and       pov.language_id = '" . (int)$_SESSION['languages_id'] . "' " .
    $order_by;
 
    $products_options = $db->Execute($sql);

 

Jul 19

zen cart – apply discount based upon whole shopping cart info

zen cart has some plugins that can apply discount based upon product.what if we need to give customer discount based upon the total item number of the order?For example,we need to implement the following discount rules on the zen cart website:

if the total item number is equal 2,then the customer get a discount of 5% (from order total)
when the total item number is bigger than 2, 10% discount apply to whole cart automatically
Both the two discount rule above apply discount based upon whole cart(not single product).You may also want to apply discount based on the order total,see the following two rules:

if the order total is over $400,then the customer get 5% discount (or a fixed discount of $20)
8% discount will apply if the order total is over $1000(also you may just want to apply a fixed discount)

how to implement all of those discount rules?

There are two place show the order total summary info.one is in the zen cart shopping cart page.The other one is the order confirmation page.This is the common case,it depends on the template designer/programmer.

The order total summary on shopping cart page is grab from shopping_cart class located in the file includes/classes/shopping_cart.php.So to implement discount and show discount info in the shopping cart bottom,we need to  do the following change:

Source code    
-------includes/classes/shopping_cart.php
...
...
  var $contents;
  /**
   * shopping cart total price
   * @var decimal
   */
  var $total;
  /**
   * shopping cart total weight
   * @var decimal
   */
 
 //added by george
 var $ototal;
 
  /**
   * Method to calculate cart totals(price and weight)
   *
   * @return void
   * @global object access to the db object
   */
  function calculate() {
    global $db;
    $this->total = 0;
    $this->weight = 0;
     ...
     ...
     ...
          // + or blank adds
          if ($attribute_weight->fields['products_attributes_weight_prefix'] == '-') {
            $this->weight -= $qty * $new_attributes_weight;
          } else {
            $this->weight += $qty * $new_attributes_weight;
          }
        }
      } // attributes weight
 
    }
     //added by george to apply discount
    $this -> ototal = $this -> total; //save origianl order total
    if ( $this -> count_contents() == 2) {
        $this -> total = $this -> total * 0.95;
    } elseif ( $this -> count_contents() >= 3) {
        $this -> total = $this -> total * 0.9;
    }
   //end of discount code
   }
  /**
   * Method to calculate price of attributes for a given item
 ...
 ...
   /**
   * Method to calculate total price of items in cart
   *
   * @return decimal Total Price
   */
  function show_total() {
    $this->notify('NOTIFIER_CART_SHOW_TOTAL_START');
    $this->calculate();
    $this->notify('NOTIFIER_CART_SHOW_TOTAL_END');
    return $this->total;
  }
 
 //added by george
   function show_ototal() {
    $this->notify('NOTIFIER_CART_SHOW_TOTAL_START');
    $this->calculate();
    $this->notify('NOTIFIER_CART_SHOW_TOTAL_END');
    return $this->ototal;
  }

 

Source code    
----includes/modules/pages/shopping_cart/header_php.php
 
$flagHasCartContents = ($_SESSION['cart']->count_contents() > 0);
$cartShowTotal = $currencies->format($_SESSION['cart']->show_total());
//added by george
$cartOtotal = $currencies->format($_SESSION['cart']->show_ototal());
$discountTotal = $cartOtotal - $cartShowTotal;

 

Source code    
----includes/templates/[current_template]/templates/tpl_shopping_cart_default.php
 
<div id="cartSubTotal"><?php echo SUB_TITLE_ORI_TOTAL;echo $cartOotal;?></div>
<div id="cartSubTotal"><?php echo SUB_TITLE_DIS_TOTAL;echo ''.$discountTotal;?></div>
<div id="cartSubTotal"><?php echo SUB_TITLE_SUB_TOTAL;echo $cartShowTotal;?></div>

 

The code above all apply and show discount info in the shopping cart,which only save the discount info in session.when user navigate to order confirmation page,zen cart generate order total summary from the order class.so we need to implement discount logic once again:

Source code    
------includes/classes/order.php
 
 function cart() {
 
 ...
 ...
 ...
 
    if (isset($GLOBALS[$class]) && is_object($GLOBALS[$class])) {
      if ( isset($GLOBALS[$class]->order_status) && is_numeric($GLOBALS[$class]->order_status) && ($GLOBALS[$class]->order_status > 0) ) {
        $this->info['order_status'] = $GLOBALS[$class]->order_status;
      }
    }
 
 //added by george
    if ($n == 2) {
        $this->info['total'] *= 0.95;
    } elseif ($n >=3) {
        $this->info['total'] *= 0.9;
    }
 
  }
 
  function create($zf_ot_modules, $zf_mode = 2) {
    global $db, $zco_notifier;
Jul 18

linux — rsync to backup data to remote server

Rsync is  widely used for backups and mirroring and as an improved copy command for everyday use.Rsync  finds  files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed in size or in last-modified time.  Any changes in the other preserved attributes (as requested by options) are made  on  the  destination file directly when the quick check indicates that the file’s data does not need to be updated.

There are two different ways for rsync to contact a remote system: using a remote-shell program as the transport (such as ssh or  rsh)   or contacting an rsync daemon directly via TCP.

“exclude and exclude-from options similar to GNU tar” — This is the top one feature i like best.As usually we do not need to rsync all files in a folder,we can just skip the cache and log file.Let’s take some simple example for quick learning and practice:

rsync -avz /data/site1 root@server1.com:/data

This will transfer all files in the directory /data/site to  the directory /data on remote server server1.com.At last /data/site1 on the server1.com will be same as the one on current server.

rsync -avz  root@server1.com:/data/site1  /data

This command will make a copy of /data/site1 directory from the server1.com to current server’s /data dirctory.Note that both command are using a remote shell.Typically, rsync is configured to use ssh by default.if you need to specify a port or use another shell,you will need to make use of rsync’s -e option:

rsync -avze  'ssh -p5990' root@server1.com:/data/site1  /data

This command will connect to server1.com 5990 port over ssh.

rsync -avze  'ssh -p5990' --exclude 'cache' root@server1.com:/data/site1  /data

 

 

Jul 17

php — my experience of send post request via fsockopen function

i need to send post request to remote server without redirecting the client browser.With the help of php manual,i got the following code to work:

Source code    
$req = '';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}
 
$post_url = 'www.abcdomain.com';
$header = '';
$header .= "POST /cgi-bin/search HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ($post_url, 80, $errno, $errstr, 60);
if (!$fp) {
    //do something
} else {
     fputs ($fp, $header . $req);
     while (!feof($fp)) {
         $res = fgets ($fp, 1024);
        //   
  }
}

yes,the code can send post request but it not always work well.sometimes,it can not go to the specified host(domain) because from the apache error log,i found the apache server route the request to the default server.It was obviously that the script  need to add a host header info.

Source code    
$req = '';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}
 
$post_url = 'www.abcdomain.com';
$header = '';
$header .= "POST /cgi-bin/search HTTP/1.0\r\n";
$header .= "Host: www.abcdomain.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ($post_url, 80, $errno, $errstr, 60);
if (!$fp) {
    //do something
} else {
     fputs ($fp, $header . $req);
     while (!feof($fp)) {
         $res = fgets ($fp, 1024);
        //   
  }
}

it works like a charm!!

 

Jul 16

linux — got lots of TIME_WAIT connection from netstat output

with the follow command,

# netstat -antp|grep TIME_WAIT

i found there are lots of TIME_WAIT connection(about 1k).Anything wrong?

From netstat man page,

TIME_WAIT
The socket is waiting after close to handle packets still in the network.

TIME_WAIT is just one state of tcp connections life cycle.it’s normal.The number of the  TIME_WAIT connection was affected by /proc/sys/net/ipv4/tcp_fin_timeout setting.

# echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout

will tune the tcp_fin_timeout setting to 30.the lower the value,the less TIME_WAIT state connection.but this only temporally change the setting.you need to config it from the file below.

/etc/sysctl.conf

net.ipv4.tcp_fin_timeout=30

 

 

Jul 07

linux – how to print single quote in awk

i often use awk command to generate command to run batch task on multiple user acct or domain.but how to print single quote in awk if i need to use single quote in the generated command.take the following command as an example:

ls -ald /home/*/www/includes/configure.php|cut -d/ -f3|awk ‘{print “mysql -u “$1″ -pxxxxxx “$3″_db < “$3″_db.sql”}’

the mysql command have -p option,its value may contain special character which need to be escaped.but if we can quote the password with single quote it will be ok.

after some search i got the solution.i can use \” to print double quote.but i can not use \’ to print single quote.to output single quote,we can use single quote ascii value.so we can rewrite last command as below

ls -ald /home/*/www/includes/configure.php|cut -d/ -f3|awk ‘{print “mysql -u “$1″ -p\x27xxxxxx\x27 “$3″_db < “$3″_db.sql”}’