If you have a Linux server running at your office or at a data center for which you are responsible, you want to maintain an uptime of as close to a hundred percent. In such a case you want to make sure you keep an eye on how the system is running. To be precise you want to monitor all the system resources that contribute to the system running fine which then results in a high uptime. Memory, CPU, disk usage… are some of the things you want to observe. We would usually use a combination of the tools that come with a Linux or UNIX installation, such as “free”, “top”, “vmstat”… I’ll introduce you to a tool that gives you just about all the info that the other tools combined give you, all under one roof – Dstat. The developer of this command line tool, Dag Wieers, calls it “a versatile replacement for vmstat, iostat, netstat and ifstat”. He adds that “Dstat overcomes some of their limitations and adds some extra features…” To me Dstat is the mother of all command line system monitoring tools. It’s simple to install, easy to use, can be tweaked with ease, and it generates reports that you can plot as a graph to impress your boss.
Installing Dstat
Start by downloading the Dstat installer. (#yum install dstat – in the command line works also if you have the dag repositories enabled) Point your web browser to the Dstat project’s homepage – http://dag.wieers.com/home-made/dstat/. Scroll down to the section of the page where the downloads are listed. Pick the flavor of Linux on which you want to install the application and click on the download link. Now download the latest version of Dstat for the version of the Linux distribution you are running. I’ll show you how to do it for a Red Hat Enterprise Linux version 4 machine:
(latest version is dstat-0.7.2-12.el7.noarch.rpm available from http://dag.wieers.com/home-made/dstat/ and https://github.com/dagwieers/dstat)
wget http://dag.wieers.com/rpm/packages/dstat/dstat-0.7.2-1.el6.rfx.noarch.rpm
Now install Dstat:
rpm -Uvh dstat-0.7.2-1.el6.rfx.noarch.rpm
If the installation went though without errors, that’s it, you have Dstat installed and ready for use. If there were some dependencies that came up during the installation just install the required packages and try again. I don’t think that Dstat has too many dependencies, so you should not face any problems.
Using Dstat
With Dstat installed on your system you should be good to go. Begin by launching the command from a terminal:
dstat
The output would be something like the following. Hit the key combination of ctrl+c to exit.
dstat
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
3 1 95 0 0 0| 31k 166k| 0 0 | 0 0 |1184 3715
26 8 66 0 0 0| 16k 88k|8467B 11k| 0 0 |1174 49k
33 13 54 0 0 0| 0 80k|2388B 2037B| 0 0 |1068 133k
28 9 63 0 0 0| 0 0 | 52k 102k| 0 0 |1957 51k
23 7 70 0 0 0| 0 0 | 20k 43k| 0 0 |1418 20k
23 6 70 0 0 0| 0 264k|8298B 11k| 0 0 |1170 22k
30 10 60 0 0 0| 0 0 |2623B 3049B| 0 0 |1075 93k
There are a number of options available for Dstat. As I mentioned earlier Dstat is quite easy to tweak. So if you want to limit the data reading to the CPU, disk, and network, run the following command:
dstat -cdn
----total-cpu-usage---- -dsk/total- -net/total-
usr sys idl wai hiq siq| read writ| recv send
3 1 95 0 0 0| 31k 165k| 0 0
0 0 100 0 0 0| 0 80k| 240B 332B
0 0 100 0 0 0| 0 0 | 360B 332B
0 0 100 0 0 0| 0 0 | 120B 332B
15 5 79 0 0 0| 0 0 | 15k 32k
20 5 75 1 0 0| 0 776k| 14k 33k
20 5 75 0 0 0| 0 0 |7217B 28k
18 5 77 0 0 0| 0 0 |4181B 13k
You can find more options in the application’s help document which you can access by entering the following:
dstat -h
Play with the options a little so you get comfortable with them. The default interval between data reads is two seconds. You can change that interval if you need to. To increase the interval to ten seconds enter the following:
dstat 10
Another useful feature is to get aggregated updates for each entry. So you can have Dstat give you an update every ten seconds in a new line, but as the data changes your line itself gets updated every second.
You might optionally want Dstat to give you five updates with a frequency of three seconds. Here’s how you would go about doing that:
dstat 3 5
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read writ| recv send| in out | int csw
3 1 95 0 0 0| 31k 165k| 0 0 | 0 0 |1185 3744
3 1 95 0 0 0| 0 88k| 11k 39k| 0 0 |1255 312
1 0 98 0 0 0| 0 115k| 12k 16k| 0 0 |1266 316
3 1 95 0 0 0| 0 27k| 10k 20k| 0 0 |1235 292
6 2 92 0 0 0| 0 32k|8482B 16k| 0 0 |1190 237
1 0 99 0 0 0| 0 2731B| 11k 174k| 0 0 |1258 306
The feature that I find most useful is the output. Dstat allows you to have the output of a session written into a comma separated file which can later be imported into a spreadsheet application such as Microsoft Excel and the data can be plotted into a graph. Here is how it can be used.
dstat –output /tmp/dstat_data.csv -CDN
Let the above command run while you run your applications or do your testing on this machine. Hit the key combination of ctrl+c when you are done. Open the file dstat_data.csv with a spreadsheet application such as Microsoft Excel or Open Office. You can then select the columns you want plotted into a graph and let your spreadsheet application do the magic.
Get a Report by Mail
There may be cases wherein you want to observe how your server is performing over a period of time. You can setup a background process in Linux that will give you a reading with a certain interval, generate a report, and mail out the file to you. This can be especially useful during a stress-test. Here’s how you could do that. The following script will run Dstat for three hours, reading the data every 30 seconds, and will mail out the report to me(@)myemailid.com.
#!/bin/bash
dstat –output /tmp/dstat_data_mail.csv -CDN 30 360
mutt -a /tmp/dstat_data_mail.csv -s “Dstat Report for 3 hour run” me@myemailid.com < /dev/null
Save the above script in a file called dstat_script.sh on your server, give it executable permission and then run it as a background process:
# chmod +x dstat_script.sh
# nohup ./dstat_script.sh &
Done. Yes, it’s that simple. You will now be mailed this report after it’s done running. You can optionally schedule this script as a daily cron job so that you can receive this data every day.
As you may already have realized Dstat is a wonderful tool when applied to performance monitoring and debugging. The granularity, frequency, and nature of the data collected are completely up to you. Dstat empowers you to know what is going on on your server. Used wisely this power can result in a lot less sleepless nights for you.
#!/bin/bash
dstat --output /root/dstat-3hour-data.csv -cdgilmnpsty --top-cpu -d --top-bio --top-latency 30 360
cron
0 7 * * * /usr/src/dstat_script.sh
7am run
Pingback: How to Investigate Server Load: Part 2 – Testing web