System monitoring with top
The 'top' command gives a real-time overview of what is happening on your server. Easy configuration and a simple interface allow you to change the emphasis of the summary. For example, choose to concentrate on CPU use or memory use.
Let's get stuck straight in by logging into your server and issuing the 'top' command:
top
If you've never seen a system stats output before then you may be in for a little shock:
Once you get used to a page full of ever changing numbers (by default 'top' updates itself every 3 seconds) you can see the output broken up into two main areas - a top section and columns of information under the highlighted bar.
Unfortunately, the output shown above is on my test server so there isn't a great deal happening. Your server may be very busy and look quite different to the sedate usage shown above.
The very first line gives uptime and load. In this case my test server has been 'up' for 5 days, 18 hours and 49 minutes. Next to that is the load average. Mine is at 0.00 over the past 5, 10 and 15 minutes. We'll talk more about loads in a later article.
The next line summarises the task list - again, this is pretty sedate.
You may well recognise the next 2 lines from the memory management article - it's part of the KB output from 'free'. Do note this output says I have 10388k free but this figure does include all cached and buffered memory.
Now to the interesting stuff: The column headers summarise what the column represents. In this introduction, we will concentrate on %CPU and %MEM. I think those are self explanatory: one shows the CPU usage as a percentage and the other Memory usage.
Let's customise the output a bit and look at the top 10 processes only. We do this by pressing 'n' and then the number of processes you want to look at. In this case enter the number '10' and press enter:
That's a bit more sane looking. However, we want to specifically look at the memory usage as the 'free' command (from the memory management article) may have told us that we are using a great deal of memory. This can be accomplished by pressing 'Shift' and 'm' (for 'memory').
Notice how the %MEM column is now the 'master' column:
Well, mysql seems to be taking the most memory followed by lsphp (lsphp is the Litespeed PHP process). Each process has a separate entry so if there were 20 lsphp processes running, there would be 20 entries. It's not a combined result.
Well, not much happening there but we still want to check the CPU usage and, more specifically, what process is using the CPU and by how much.
We start this process of choosing which field (column) to study by pressing 'Shift' and 'o' (the letter 'o', not the number '0'):
This rudimentary interface shows what processes are available for inspection. The left hand columns show a letter, the column code and a summary of that code.
Scan down the list to:
j: P = Last used cpu (SMP)
k: %CPU = CPU usage
l: TIME = CPU Time
We want to concentrate on CPU usage so press 'k' (note how the asterix (*) moves next to the 'k: %CPU' row indicating the CPU field has been selected) and then press enter.
Now we've returned to the processes page and the %CPU column is the ordered field.
As you saw from the interface page ('Shift' and 'o') there is a lot of scope for monitoring exactly what is happening on your server. Also, as 'top' automatically updates every 3 seconds, you have a quick and easy real-time overview.
If you are anything like me, you will have a favourite view. For me, my 'default' view is 20 rows with the %MEM column being the master field.
That's all well and good but I'd like this favourite view to be shown as soon as I start 'top'.
No problem. First of all configure 'top' to your favourite view and then press 'Shift' and 'w'. That's it.
If you are quick you will see a message as follows (Wrote configuration....):
As shown, my favourite configuration has been saved to .toprc in my home directory. Nice.
Let's test this by quitting top (press 'q') and restart it. It should go straight to your saved configuration.
Naturally, all the options are still available to you so you're not 'stuck' with your saved view.
As with the vast majority of commands, there is far more available than an introduction can even point to. Try asking the man about top and although you may think the man entry seems to be ridiculously large, just take it a section at a time. I find it useful to locate what I do know, get used the syntax and then try to extend my options.
Of course, top is not the only way of monitoring system usage and I'd like to thank Ron for this simple command:
ps auxk-rss
I think that gives a very nice 'snapshot' of memory usage and what command it's related to.
Although future articles will concentrate on more advanced monitoring tools such as nagios, never dismiss the simple and straightforward commands like 'top' and 'free'.
PickledOnion.
Digg it |
del.icio.us |
reddit |
StumbleUpon

Subscribe to Feed