Create a graph of your system’s performance

Use Dstat and Gnuplot to monitor performance, then turn that information into neat-looking graphs that anyone, even your manager, can understand...

This script generates a graph for your system’s CPU usage

Dstat data collection
A quick word on the usage of Dstat before we move onto the part where we collect system performance data. Run the command ‘dstat -h’ to get a dump of all the options supported by Dstat. You can customise the data collected by Dstat using these options. So, for example, if you want to only get the information about the CPU and the memory, run the command ‘dstat -cm’, where ‘c’ stands for CPU and ‘m’ stands for memory. You can also customise the time interval and the number of readings that Dstat takes. So if you want to run the command we just ran with an interval of ten seconds and get five readings, you would modify the command to look something like ‘dstat -cm 10 5’.

Now that we have Dstat installed and working, we can begin collecting the system-monitoring data. We’ll use Dstat to collect the CPU, memory and network data. We will also have Dstat write the time alongside each entry. We will get Dstat to note readings taken every five seconds. So the command we will be using to collect system performance data will look something like: ‘dstat -tcmn’. To allow Gnuplot to take this data, we will need the data to be stored in a file. For that we will redirect the output from the ‘dstat’ command to a file. The command will then look like this: ‘dstat -tcmn > dstat.dat’. Run this command for a few minutes, hit the key combination Ctrl+C to exit the command, and open “dstat.dat” in your favourite text editor. You will see a list of numbers that looks something like this:

Dstat output saved in the file ‘dstat.dat’
Dstat output saved in the file ‘dstat.dat’

If you see an output like the one above then you are good to go. Now you can run the following command to collect the actual data: ‘dstat -tcmn 10 > dstat.dat’. Let this command run for some time, at least ten minutes. You will get an output that looks something like the following in the file ‘dstat.dat’:

Dstat taking readings with ten-second intervals
Dstat taking readings with ten-second intervals

If you look at the time stamp in the output, you will see that each reading was taken with a gap of ten seconds. If you are running the command from a remote shell, such as over an SSH connection, you should make the command run in the background using the ‘nohup’ command. This way the Dstat process will not die if your SSH session expires. The new command will look something like this:

# nohup dstat -tcmn 10 > dstat.dat &
[1] 27857

When you hit the Return key on your keyboard after entering the command, you will be presented with a number. This is the process ID for the background process. Take note of this number: you will need it when you want to stop Dstat from collecting more data. Once you are done collecting data, run the following command to stop Dstat:

# kill -9 27857
[1]+  Killed      nohup dstat -tcmn 10 > dstat.dat

Replace ‘27857’ with the process ID you noted down. You should receive an output saying that the Dstat process was killed.