Gnuplot – Basic Ideas to Show Live System Data

One thing I noticed early with gnuplot is that the png is almost immediately updated in the viewer as the png file is over-written when the command is re-run.

This gave me an idea of using those updates as a live plot mechanism for traces that update per second or so - ideally just as dstat prints them - using what has been learned so far in the prior two Posts.

This is a totally amateur learning exercise and experiment, but it works - to give you some ideas and make you think about the mechanisms that are used in professional live graphing, such as the fab little widgets in System Monitor/Resources:


By combining commands and processes in the Unix tradition, but at a very basic user level, without specific internal kernel tools knowledge like perf et al or C scripts, the idea was to get the live data file fed per second by dstat, then periodically re-run gnuplot via the watch command as quickly as it could update the png. So..what I have so far is a working live graph - albeit a primitive one!


Start term 1 running for live user CPU load in column 3 and moving the mouse to check usage number increase :

dstat -tc | awk '{if(NR>3) print $0 fflush()}' | tee dstatlive.txt

19-08 00:42:22| 6 2 92 0 0 00
19-08 00:42:23| 6 2 92 0 0 00
19-08 00:42:24| 7 2 92 0 0 00
19-08 00:42:25| 11 3 86 1 0 00
19-08 00:42:26| 20 5 75 0 0 00
19-08 00:42:27| 36 6 58 0 0 00
19-08 00:42:28| 30 7 61 2 0 00

Start term 2 running:

watch -n 1 gnuplot livedstat.gnu

This re-runs gnuplot every second to update the png file in close sync with dstat out putting new data per second.

Open the dstatlive.png and watch the updates.

The gnuplot conf was amended to: vi livedstat.gnu

set title 'dstat cpu live'
set autoscale
set terminal png large size 1280,960
set xdata time
set xlabel 'Time of Day' font 'Arial,14' offset -2,0
set ylabel '% cpu' font 'Arial,12'
set timefmt '%d-%m %H:%M:%S'
set xtics rotate
set format x "%d-%m %H:%M:%S"
set output 'dstatlive.png'
plot 'dstatlive.txt' using 1:3 with linespoints

Obviously, the system overhead is probably massive in comparison to SysMon's code with this setup - but it works - so shows what can be done by plumbing things together as you see it, using what you know.

I think it may be ambitious to add two further columns...

plot 'dstatlive.txt' using 1:3 with linespoints,'dstatlive.txt' using 1:4 with linespoints,'dstatlive.txt' using 1:5 with linespoints

Actually, no!

Now - wouldn't your boss be more impressed with that instead of a printout? You could preconfigure many gnuplot confs for particular system processes then run them as required.

Just for your interest, it was left running by accident for about 15 mins:

CPU% utilization; red:usr; green sys; blue idle.



Actually not much overhead running this as %idle is around 75% still. This may be useful enough as a first (USE) step to catch a CPU resource hog and whether it is a user or a sys process, for further investigation. USE - utilization, saturation, errors.

Cool eh?

Just to answer a concern of mine about data accuracy - the output from dstat is almost the same as that from mpstat (decimal points), so the graphed points are easily accurate enough for this purpose. I ran both commands alongside mpstat, while graphing again, and the dstat figures lag those of mpstat per readout, but are similar enough to be effectively equal. I also added a line weight of 5, and changed the res in the gnuconf for better visuals:

set terminal png large size 2560,1024

plot 'dstatlive.txt' using 1:3 with linespoints lw 5,'dstatlive.txt' using 1:4 with linespoints lw 5,'dstatlive.txt' using 1:5 with linespoints lw 5 

Just found how to wrap each cmd line in a function - each can be called in their own terminal:

function CPUloadavg() { dstat -tc | awk '{if(NR>3) print $0 fflush()}' | tee dstatlive.txt; }

function avgCPUwatch() { watch -n 1 gnuplot livedstat.gnu; }

You can list available functions with function [TAB]
Display all 150 possibilities? (y or n)

__all_modules _pacat
_allowed_groups _pacat_file_formats
_allowed_users _pacmd
_apt _pactl
_available_interfaces _padsp
_axi_cache _parse_help
__cards __parse_options
_cd _parse_usage
_cd_devices _pasuspender
_command _pci_ids
command_not_found_handle _perl
_command_offset _perldoc
_complete_as_root _perlfunctions
_completion_loader _perlmodules
_configured_interfaces _pgids
_count_args _pids
CPUloadavg _pnames....

Or check your function:

function CPUloadavg [TAB]

() {
dstat -tc | awk '{if(NR>3) print $0 fflush()}' | tee dstatlive.txt

You still have to run 2 terms, but it's more compact than 2 shell scripts:



Click on the png in Files as above to view image...

Comments are closed.

Post Navigation