Real-time plots in 20 lines
Simple Line Chart
A line chart presentation can be created from a data file (GPU.dat
), as well:
# GPU.dat - a time stamp with two data points 18:48:30 51.0 49.0 18:48:40 50.5 49.5 18:48:45 51.5 49.0 18:48:50 50.0 50.5 18:48:55 50.5 49.5
The interactive Gnuplot commands to show a line chart of this data are shown in Listing 2. This Gnuplot script requires a few extra lines: The plot needs to know that the x-axis is time data, and it needs to know the format of the time data and the x labels.
Listing 2
Line Chart from a File
$ gnuplot gnuplot> # plot 2 variables in the file GPU.dat gnuplot> # gnuplot> set xdata time gnuplot> set timefmt "%H:%M:%S" gnuplot> set format x "%H:%M:%S" gnuplot> plot "GPU.dat" using 1:2 with line title "GPU temp" , "GPU.dat" using 1:3:3 with line title "CPU temp"
Multiple data points can be plotted at the same time (Figure 4). The using
argument tells Gnuplot how to reference the <x>:<y>
columns in the data file. (If the data file had a third column of data points, the using
reference to get the last column of data would be 1:4:4
).
Real-Time Line Chart
Linux has a lot of useful command-line troubleshooting tools, such as iostat
, vmstat
, and top
, to name just a few. For the line chart example, I use the sensors
utility [3] to get the fan speed and CPU temperature of my Linux server. The sensors
command returns a number of lines of information.
$ sensors dell_smm-virtual-0 Adapter: Virtual device Processor Fan: 2676 RPM CPU: +47.0~ZâC Ambient: +38.0âC SODIMM: +37.0âC ...
With some Bash and Awk commands, you can get just the fan speed and CPU temperature (Listing 3).
Listing 3
Parsing Data
$ sensors | grep RPM Processor Fan: 2685 RPM $ sensors | grep RPM | awk '{print $3}' 2685 $ sensors | grep CPU CPU: +48.0âC $ sensors | grep CPU | awk '{print $2}' +48.0âC $ sensors | grep CPU | awk '{print substr($2,2,4)}' 48.0
Awk supports a systime()
call to return the present date/time, and a strftime()
call to customize the presentation. (Note: You might have to install gawk
– sudo apt-get install gawk
– on the Raspberry Pi to get this added functionality.)
Once the measurements have been parsed, the next step is to format the sensor output with a timestamp:
$ sensors | grep RPM |awk '{print strftime("%H:%M:%S ",systime()) $3}' 10:26:46 2685 $sensors | grep CPU |awk '{print strftime(\"%H:%M:%S \",systime()) substr($2,2,4)}' 10:27:46 48.0
After a time and value string have been generated, you can create a Gnuplot script, line_fan_cpu.txt
, to show real-time data (Listing 4). To make the script a little easier, I create two data files, fan.dat
and cpu.dat
.
Listing 4
Dynamic Line Chart Script
01 # Create a Plot or User and System CPU Usage, update every 5 seconds 02 # 03 set title "GnuPlot - Fan Speed and CPU Temperature" 04 set yrange [2650:2700] 05 set ylabel "Fan Speed" 06 set y2range [43:49] 07 set y2label "CPU Temp (C)" 08 set y2tics 09 set xdata time 10 set timefmt "%H:%M:%S" 11 set format x "%H:%M:%S" 12 13 system "sensors | grep RPM | awk '{print strftime(\"%H:%M:%S \", systime()) $3}' > fan.dat" 14 system "sensors | grep CPU | awk '{print strftime(\"%H:%M:%S \",systime()) substr($2,2,4)}' > cpu.dat" 15 16 plot "fan.dat" using 1:2 with lines axes x1y1 title "fan speed (RPM)", "cpu.dat" using 1:2 with lines axes x1y2 title "CPU Temp (C)" 17 while (1) { 18 pause 5 19 system "sensors | grep RPM | awk '{print strftime(\"%H:%M:%S \", systime()) $3}' >> fan.dat" 20 system "sensors | grep CPU | awk '{print strftime(\"%H:%M:%S \", systime()) substr($2,2,4)}' >> cpu.dat" 21 replot 22 }
The plot accounts for different scale ranges with y2range
and y2label
definitions. The final addition is to include an axis (x1y2
or x1y2
) to each plot point that lines up the data values to the right or left y-axis.
The complete Gnuplot script to show fan speed and CPU temperature is only 20 lines of code! The command
$ gnuplot -persist line_fan_cpu.txt
runs this script (Figure 5).
Final Comments
I won't give up using plotting packages like Matplotlib or ggplot, but I was very impressed with how easy it was to create real-time plots with Gnuplot.
Manipulating the Bash/Awk script can be a little complex, but it's incredibly useful to be able to use output from almost any command-line utility in Gnuplot.
Gnuplot can plot a large number of data points, but it makes sense to do a tail
command to create a sliding view of the latest information.
Infos
- Gnuplot documentation: http://www.gnuplot.info/
- gpio command-line utility: http://wiringpi.com/the-gpio-utility/
- sensors command-line utility: https://wiki.archlinux.org/index.php/Lm_sensors
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.