stevepedwards.com/DebianAdmin linux mint IT admin tips info

Gnuplot – Basic Starter Example Data File for System Profile Plots

This example is my starter data.dat file for building complexity if required, later. It combines the basics required for a time of day x axis against y axis numerical data, with x/y autoscaling and title functions etc. Paste it into some file name:

vi plot1.gnu

set title 'user CPU time'
set autoscale
set terminal png medium size 640,480
set xdata time
set xlabel 'Time of Day' font 'Arial,14'
set ylabel '%CPU util' font 'Arial,14'
set timefmt '%Y-%m-%d %H:%M:%S'
set output 'userCPU.png'
plot 'data1.dat' using 1:3 title 'user cpu%' with linespoints

This requires the data file "data1.dat" to be available in your home dir, if running gnuplot from there. The 1:3 reads the 3rd column data from left to right to plot it against the pre-defined x time axis. It outputs a PNG of the plot, also in the home directory. The rest is self evident - titles etc.

The data can be a column list of numbers created from any source - by hand etc. For this example I'm collecting data from sar/sadf.

sudo apt-get install sysstat

Turn on sadc logging in

sudo vi /etc/default/sysstat

ENABLED="true"

sar
Cannot open /var/log/sysstat/sa17: No such file or directory
Please check if data collecting is enabled in /etc/default/sysstat

After a "linux jiffie" or two, and the creation of a binary log folder e.g: ls /var/log/sysstat/sa17, sar will be able to output data from 10 min intervals, as will sadf:

sar1.png

stevee@AMDA8 ~ $ sadf -p -P 0,1,2,3 | head 
AMDA8 599 2016-08-17 11:55:01 UTC cpu0 %user 10.28
AMDA8 599 2016-08-17 11:55:01 UTC cpu0 %nice 0.07
.....

This is the data I will strip columns from for that data.dat file as the y axis in is already configured to read time/date info in this format of columns 3 and/or 4 above: set timefmt '%Y-%m-%d %H:%M:%S'

sadf -p -P 0,1,2,3 | grep user | grep cpu0 | awk '{print $3,$4,$8}'

2016-08-17 11:55:01 10.28
2016-08-17 12:45:01 7.67....

3 columns allows you to see where the 1:3 fits in the gnuplot conf file, and will allow an extra date on the x axis to be added if you want later.

Direct this into data.dat: sadf -p -P 0,1,2,3 | grep user | grep cpu0 | awk '{print $3,$4,$8}' > data1.dat

cat data1.dat
2016-08-17 11:55:01 10.28
2016-08-17 12:05:01 5.27
2016-08-17 12:15:01 17.73
2016-08-17 12:25:01 13.63
2016-08-17 12:35:01 4.38
2016-08-17 12:45:01 7.67
2016-08-17 12:55:01 8.40
2016-08-17 13:05:01 11.02
2016-08-17 13:15:01 13.57
2016-08-17 13:25:01 10.07
2016-08-17 13:35:01 5.68
2016-08-17 13:45:01 3.20
2016-08-17 13:55:01 4.32
2016-08-17 14:05:01 2.40
2016-08-17 14:15:01 9.42
2016-08-17 14:25:01 10.45
2016-08-17 14:35:01 14.02
2016-08-17 14:45:01 14.45
2016-08-17 14:55:01 16.20

Now you just run gnuplot with the conf file:

gnuplot plot1.gnu

An aptly named userCPU.png should appear in your home dir:

ls
bin Cprogs Desktop Downloads Pictures Public userCPU.png
Contacts data1.dat Documents Music plot1.gnu Templates Videos

Open it:

xdg-open userCPU.png 

userCPU.png

Compare the data values from the data.dat file and check their graph points:

cat data1.dat | awk '{print $3}' | sed ':a;N;$!ba;s/\n/; /g'
10.28; 5.27; 17.73; 13.63; 4.38; 7.67; 8.40; 11.02; 13.57; 10.07; 5.68; 3.20; 4.32; 2.40; 9.42; 10.45; 14.02; 14.45; 16.20; 8.94 

Nice! Now you can tart it up with colours and whatever you want by adding more functions to the conf file etc.

http://gnuplot.sourceforge.net/docs_4.2/node1.html

Hmmm...just noticed...the latest sar data is 1hr 08 behind real time...

date
Wed Aug 17 16:43:37 BST 2016

cat data1.dat | tail -n1
2016-08-17 15:35:01 6.11

OK, a few hrs later, a thing to note is that you need to increase the PNG size as you add more data else the x axis time gets squished, so I changed:

set terminal png large size 1280,960

userCPUlg.png

The x axis time fields (with seconds) are set by the data, not the conf file, so if you wanted to ignore the seconds you have to strip that colon separated column from the output. Then you could remove the %S from

set timefmt '%Y-%m-%d %H:%M:%S'

If you study the data, the time is taken every 01 secs, so that is what needs removal:

2016-08-17 17:05:01 24.73
2016-08-17 17:15:01 7.24
2016-08-17 17:25:01 2.75

Appending sed to strip these chars from the line does the job:

sadf -p -P 0,1,2,3 | grep user | grep cpu0 | awk '{print $3,$4,$8}' | sed 's/:01//g'

2016-08-17 11:55 10.28
2016-08-17 12:05 5.27
2016-08-17 12:15 17.73

Now there is data with only 10 min intervals. Amending the conf file for that also gives month and day, but very squashed so needs rotation:

set format x "%m-%d %H:%M"

nosecs.png

set format x "%H:%M"

nosecs2.png

set xtics rotate

rotatedef.png

That defaults to 90 degs, but 45 may be better for added info but I cannot get this "by 45" to work:

set title 'user CPU time'
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 util' font 'Arial,12'
set timefmt '%Y-%m-%d %H:%M:%S'
set xtics rotate
set format x "%H:%M"
set output 'userCPU.png'
plot 'data1.dat' using 1:3 title 'user cpu%' with linespoints

Finally, to include the full ymd/time date:
set format x "%Y-%m-%d %H:%M"

userCPU.png

Each time you want an instant updated plot viewed, run the three cmds on one line e.g:

sadf -p -P 0,1,2,3 | grep user | grep cpu0 | awk '{print $3,$4,$8}' | sed 's/:01//g' > data1.dat; gnuplot plot1.gnu; xdg-open userCPU.png

userCPU.png

It's easy to view the other CPU% usage options like idle and system just by changing the grepped term and/or the png name. Swap user for idle for example - note the y axis autoscale increase:

sadf -p -P 0,1,2,3 | grep idle | grep cpu0 | awk '{print $3,$4,$8}' | sed 's/:01//g' > data1.dat; gnuplot plot1.gnu; mv -v userCPU.png idleCPU.png ; xdg-open idleCPU.png

idleCPU.png

The idle is almost a mirror image of the user graph! The system must be similar to the user stats then?

sadf -p -P 0,1,2,3 | grep system | grep cpu0 | awk '{print $3,$4,$8}' | sed 's/:01//g' > data1.dat; gnuplot plot1.gnu; mv -v userCPU.png system.png; xdg-open system.png

system.png

Very similar:

usersystem.png

If xdg-open complains with:

(eog:18950): EOG-WARNING **: Couldn't load icon: Error writing

stick it in the bit bucket to get the cmd line back:

sadf -p -P 0,1,2,3 | grep system | grep cpu0 | awk '{print $3,$4,$8}' | sed 's/:01//g' > data1.dat; gnuplot plot1.gnu; mv -v userCPU.png system.png; xdg-open system.png &2> /dev/null
‘userCPU.png’ -> ‘system.png’
[1] 19094
stevee@AMDA8 ~ $

With a change of log folder at 2400hrs, you may want to run data from a prior day with the same format for these commands:

sadf /var/log/sysstat/sa17 -P 0,1,2,3 | grep nice | grep cpu0 | awk '{print $3,$4,$8}' | sed 's/:01//g' > data1.dat; gnuplot plot1.gnu; mv -v userCPU.png nice.png; xdg-open nice.png &2> /dev/null

Running for niceness also shows the autoscaling smaller y axis:

nice.png

Finally, just for completeness of terminology, the iowait field (steal is all 0%):

iowait.png

Comments are closed.

Post Navigation