Link to home
Start Free TrialLog in
Avatar of Tessando
TessandoFlag for United States of America

asked on

Commands To Help Me Understand CPU Usage in Linux

I would like to understand the CPU behavior of a linux instance I have running in AWS. This is a LAMP stack where I followed chapter-and-verse the Amazon Linux setup.

I'm noticing that at certain times of the day, there is more CPU utilization that others. Question 1: What is the best way to determine those spikes?

I've been using the "top" command to see the processes when I 'catch' the server spiking, but it moves so fast that I hardly can copy the text. Question 2: Is there a way to manipulate the "top" command so that I can see a measurable aggregate of processes running on the server? If so, can I adjust this for, say, 24 hours or 48 hours?

Question 3: Could traffic or bots be causing a CPU spike? Is there an easy way to tell how many users are on a website at a given moment?

I do have a cron job running every five minutes, which is for Amazon's CloudWatch service (a monitoring service for AWS), but it's small and I do not suspect it as a culprit.

Thanks for your help and patience as I learn more about Linux CPU Utilization.

...
Avatar of 65td
65td
Flag of Canada image

Question 0: I'm noticing that at certain times of the day, there is more CPU utilization that others.

Almost surely because of different traffic levels at different times of days.

Just look at your Apache + FPM PHP logs to determine the exact reason.

Question 1: What is the best way to determine those spikes?

Use SAR to record CPU usage or use top in batch mode to periodically record usage.

The other simple trick is to ignore CPU time (as this is fairly meaningless) + focus on log data. As logged requests increase, you know CPU is increasing.

Tip: CPU usage is fairly meaningless. I sometime see loads of 100s of load units on some high traffic sites.

What is meaningful is length of time required to process a request.

The only real site bottlenecks are either Disk I/O or Network I/O. CPU usage can be massively high + so long as requests complete near instantaneously, you'll be good.

Question 2: Is there a way to manipulate the "top" command so that I can see a measurable aggregate of processes running on the server? If so, can I adjust this for, say, 24 hours or 48 hours?

With top, no.

With SAR, yes.

Question 3: Could traffic or bots be causing a CPU spike?

This is the only likely reason.

Question 4: Is there an easy way to tell how many users are on a website at a given moment?

Yes.

Google Analytics is one easy way to see real time analytics.
Avatar of Tessando

ASKER

Thanks everyone! I've installed sysstat on the server and have been playing around with it.

@DavidFavor -> How could I use the "sar" command to see historical data, say, from 8/27 to today? Is there a way see that?

Thanks!
Avatar of noci
noci

sar is part of sysstat.  sysstat is a good toolkit for this.
Do you currently crunch, process your Apache logs,
From this you could get (extrapolate)  a fuller picture of what is going on on your system,
Based on your knowledge of what ...
You could look to correlate CPU usage and increase in traffic/requests.
ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks David - Yes, I was able to create a CRON job based off instructions I found online.

The job I setup was for every 10 minutes for sa1:

*/10 * * * *

Open in new window


And at 7 minutes before the end of the day for sa2:

53 23 * * * 

Open in new window


Now that I have this data, what's a good sar command to be able to see a given day and time. Let's say I wanted to see yesterday (9/4/19), after I created this CRON job, which was at 5:00 PM Pacific... so, how could I see info for 9/4/19 between 19:00 and 21:00?

Thanks for your help.