user1263746
user1263746

Reputation: 6308

Linux : Get total cpu usage by httpd

I need to display the total Percentage of CPU utilized by httpd processes on a server in a php report.

I am calling following from exec :

ps -e -o %mem,%cpu,cmd | grep httpd | awk ' {memory+=$1;cpu+=$2} END {printf("%05.2f ",memory);printf("%05.2f\n",cpu)}'

But the above command's reported CPU usage and the one reported by top command are not matching.

I need to report --> If CPU is busy at 40%, 10% of httpd processes, 20% of mysqld processes, 10% of perl processes, then I need to report the 10% of httpd. (Assuming that there are no other processes).

I saw this : get apache total cpu usage in (linux)

But I understand that ps command returns the percentage of CPU consumed by a process out of the total percentage of CPU consumed. I understand that it is getting messy, so the below example should help.

If httpd is consuming 10% of CPU which is busy at 60% then the actual contribution of httpd to make CPU busy was ((100/60)*10) = 16.66 %. Is this correct? What else are the best way to get cpu usage by a group of processes by the same name.

Upvotes: 0

Views: 3174

Answers (3)

Mustafa
Mustafa

Reputation: 48

try this in ssh

ps aux | grep "httpd"  | awk '{sum1 +=$3}; END {print sum1}'

output is:

10.5

and this for sum of memory

ps aux | grep "httpd"  | awk '{sum1 +=$4}; END {print sum1}'

Upvotes: 3

user1467267
user1467267

Reputation:

This works for me on OSX:

<?php
    exec('ps -e -o %mem,%cpu,command | grep httpd', $output);

    $proc_data = [];

    foreach($output as $key => $value) {
        // Make sure it's only path httpd and not the grep included
        if (strstr($value, '/httpd')) {
            $info = explode(' ', trim($value), 5);
            unset($info[1]);
            unset($info[2]);
            $proc_data[] = array_merge($info);
        }
    }

    echo '<pre>';
    print_r($proc_data);
    echo '</pre>';



    // Caclulate total CPU percentages
    $total_cpu = 0;

    foreach ($proc_data as $key => $value) {
        $total_cpu += $value[1];
    }

    echo $total_cpu;
?>

This is the Terminal output for the bash:

MacBook-Pro:~ user$ ps -e -o %mem,%cpu,command | grep httpd
 0,2   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,1   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,1   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   9,0 /Applications/MAMP/Library/bin/httpd -k start
 0,0   0,0 grep httpd

I noticed ps -e -o %mem,%cpu,cmd has to be command, but it might be an OSX-only thing tho. Hope you can work with this.

Good luck!

Upvotes: 0

Vineet1982
Vineet1982

Reputation: 7918

I'm not 100% sure on what you're asking, but if I'm right, this answer might help you:

<?php
    exec('ps -aux', $processes);
    foreach($processes as $process){
        $cols = split(' ', ereg_replace(' +', ' ', $process));
        if (strpos($cols[2], '.') > -1){
            $cpuUsage += floatval($cols[2]);
        }
    }
    print($cpuUsage);
?>

and after searching many forms also found the another way:

after searching on forums and trying many methods but I have not tried it:

$stat1 = file('/proc/stat'); 
sleep(1); 
$stat2 = file('/proc/stat'); 
$info1 = explode(" ", preg_replace("!cpu +!", "", $stat1[0])); 
$info2 = explode(" ", preg_replace("!cpu +!", "", $stat2[0])); 
$dif = array(); 
$dif['user'] = $info2[0] - $info1[0]; 
$dif['nice'] = $info2[1] - $info1[1]; 
$dif['sys'] = $info2[2] - $info1[2]; 
$dif['idle'] = $info2[3] - $info1[3]; 
$total = array_sum($dif); 
$cpu = array(); 
foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 1);

Upvotes: 1

Related Questions