News

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

You should have Gnuplot installed on your computer before you proceed. We’ll give you a quick starter to give you some basic familiarity with the workings of Gnuplot before we proceed with creating a script to generate graphs from the data we collected in the previous step.

Gnuplot script
We will use a few scripts like the ones on the next page  to build a graph from the data we gathered using Dstat.
First, before we get to the graph-building scripts, you need to run the following shell command to strip the first two lines of the data in the ‘dstat.dat’ file, as they will hamper the graph-building process. The following script will search for lines containing the terms ‘time’ and ‘date’, delete these lines and save the output in a new file called ‘stat.dat’.

# grep -Ev ‘time|date’ dstat.dat > stat.dat

Now we’ll write three Gnuplot scripts which will take the data file ‘stat.dat’ as input and generate three graphs, one each for the CPU, memory and network bandwidth usage. In brief, what we do in these scripts is we first set the title, the x axis and the y axis, and the export format settings as we want them to be. Then we tell Gnuplot to use the file ‘stat.dat’ as data input and which columns to use to plot the respective graphs. Gnuplot takes care of the rest.

#!/usr/bin/gnuplot
set terminal png
set output “cpu.png”
set title “CPU usage”
set xlabel “time”
set ylabel “percent”
set xdata time
set timefmt “%d-%m %H:%M:%S”
set format x “%H:%M”
plot “stat.dat” using 1:3 title “system” with lines,
“stat.dat” using 1:2 title “user” with lines,
“stat.dat” using 1:4 title “idle” with lines

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


The second script…

#!/usr/bin/gnuplot
set terminal png
set output “memory.png”
set title “memory usage”
set xlabel “time”
set ylabel “size(MB)”
set xdata time
set timefmt “%d-%m %H:%M:%S”
set format x “%H:%M”
plot “stat.dat” using 1:8 title “used” with lines,
“stat.dat” using 1:9 title “buff” with lines,
“stat.dat” using 1:10 title “cach” with lines,
“stat.dat” using 1:11 title “free” with lines

This one generates a graph showing the system’s usage of memory

And the third one…

#!/usr/bin/gnuplot
set terminal png
set output “network.png”
set title “network”
set xlabel “time

set ylabel size(k)
set xdata time
set timefmt
%d-%m %H:%M:%S
set format x %H:%M
plot stat.dat using 1:11 title send with lines,
stat.dat using 1:12 title recv with lines


And this script creates a graph for the network bandwidth usage.

Network bandwidth usage graph generated by the Gnuplot script
Network bandwidth usage graph generated by the Gnuplot script

Copy these scripts into the folder that contains ‘stat.dat’, which has the monitoring results from Dstat. Grant executable permissions to the shell scripts with the following command:

# chmod +x cpu.sh memory.sh network.sh
Now run the three scripts to generate the graphs:
# ./cpu.sh; ./memory.sh; ./network.sh

If all went well you should see three new files: cpu.png, memory.png, and network.png. Congratulations, you now have your system-monitoring information in neat-looking graphs!

×