Flipkart

Friday, September 28, 2012

Provide RegEx feature for array_key_exists function

Hi,

  Here is the function which can take the $key as a RegEx/String. This function checks whether the given string($string) is a Regular Expression or not. If yes, it will use preg_match() function else it will use array_key_exists()  function.

function preg_array_key_exists($filename, $array) {
    $available = false;  
    if (array_key_exists($filename, $
array))
    {
        $available = true;      
    }
    else
    {      
        $keys = array_keys($
array);
        foreach ($keys as $key) {        
            if (@preg_match($key, $filename) == 1) {  
              $available = true;          
          }      
        }
    }
    return $available;
}

Tuesday, September 11, 2012

Get overall CPU Usage using TOP command in Linux

Here is the command which gives the total CPU usage in %

top -bn1 | grep "Cpu(s)" | \sed "s/.*, *\([0-9.]*\)%\id.*/\1/" | \awk '{print 100 - $1"%"}'


Here is another command in which, we can write the conditions to display the results. Below is the example which display the process names which exceeds by given $cpuUtil value.

$cpuUtil = 2

ps -e -o pcpu  -o args  --no-heading | sort -k 1 -r | awk '$1 > $cpuUtil { print $2 }'