Search code examples
phplinuxunixps

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.


Solution

  • 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}'