Skip to content

Instantly share code, notes, and snippets.

@lukasMega
Last active December 19, 2023 15:00
Show Gist options
  • Save lukasMega/7cd13fcc78d6a9cbc0039128b9aba1ac to your computer and use it in GitHub Desktop.
Save lukasMega/7cd13fcc78d6a9cbc0039128b9aba1ac to your computer and use it in GitHub Desktop.
Create simple graph of CPU temperature using RRDTool

๐Ÿ“ˆ Create simple graph of CPU temperature using RRDTool

RRDTool

RRDtool is the OpenSource industry standard, high performance data logging and graphing system for time series data. RRDtool can be easily integrated in shell scripts, perl, python, ruby, lua or tcl applications.

  • more info:

    RRDtool refers to Round Robin Database tool. Round robin is a technique that works with a fixed amount of data, and a pointer to the current element. Think of a circle with some dots plotted on the edge. These dots are the places where data can be stored. Draw an arrow from the center of the circle to one of the dots; this is the pointer. When the current data is read or written, the pointer moves to the next element. As we are on a circle there is neither a beginning nor an end, you can go on and on and on. After a while, all the available places will be used and the process automatically reuses old locations. This way, the dataset will not grow in size and therefore requires no maintenance. RRDtool works with Round Robin Databases (RRDs). It stores and retrieves data from them.

Install

sudo apt install rrdtool

1. โž• Create RRD Database

rrdtool create database2.rrd --start -1h \
  --step 10 \
  --no-overwrite \
  DS:num:GAUGE:30:0:100 \
  RRA:AVERAGE:0.5:5:9500
explanation:
param description
--step 10 I want to collect updates every 10 seconds
--no-overwrite No action takes place if the database already exists
DS:num:GAUGE:30:0:100 Create a dataset num with values between 0 and 100. If no data comes during heartbeat 30sec store UNDEFINED
RRA:AVERAGE:0.5:5:9500 Calculates and stores 9500 records and store the AVERAGE in case multiple values are supplied during the same interval (step 10 seconds).
  1. Create 9500 items long
  2. round-robin archive (RRA)
  3. where every item is an AVERAGE
  4. of 5 data points from the data source.
  5. If more than half (>0.5) of those 5 are UNDEFINED, assume aggregated value UNDEFINED as well.

or: (https://codeblog.dotsandbrackets.com/quick-intro-to-rrdtool/)

1.1 ๐Ÿšฎ Insert data into RRD Database

#!/bin/bash

#################### start.sh ####################

while true
do
    temp=$(sensors | grep "Core 0" | awk '{print $3}' | cut -c "2-5")
    rrdtool update database2.rrd N:$temp
    echo $temp
    sleep 5
done
  • script runs infinitely and every 5 seconds it updates DB

2. โšก Do some stress CPU

  • Infinitely:

    stress-ng --cpu 4 --cpu-method matrixprod  --metrics-brief --perf
  • With timeout 60 min:

    stress-ng --cpu 4 --cpu-method matrixprod  --metrics-brief --perf --timeout 3600s

3. ๐Ÿš€ Create graph (PNG image)

#!/bin/bash

#################### plot.sh ####################

rrdtool graph ./graph5.png \
--start -1h \
--title "CPU Temperature" \
--vertical-label "Temperature (ยฐC)" \
-w 900 -h 400 \
DEF:temp=./database2.rrd:num:AVERAGE \
LINE1:temp#FF0000:"CPU Temperature"
  • To see updated graph every 10 seconds:

    watch -n 10 ./plot.sh 

๐Ÿ“ˆ Example graph

You will get something like this:

img

another example with bigger time window and resolution:

img

  • Graph shows 3,5 hod full load of CPU Intel G3220T in fully passive (fanless) PC, limited to 25W in BIOS - ambient temperature in room was 23โ„ƒ
    pc@pc-exone:~/cputmp$ neofetch 
                 .-/+oossssoo+/-.               pc@pc-exone 
             `:+ssssssssssssssssss+:`           ----------- 
           -+ssssssssssssssssssyyssss+-         OS: Ubuntu 23.10 x86_64 
         .ossssssssssssssssssdMMMNysssso.       Kernel: 6.5.0-10-generic 
        /ssssssssssshdmmNNmmyNMMMMhssssss/      Uptime: 3 mins 
       +ssssssssshmydMMMMMMMNddddyssssssss+     Packages: 1926 (dpkg), 11 (flatpak) 
      /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Shell: bash 5.2.15 
     .ssssssssdMMMNhsssssssssshNMMMdssssssss.   Resolution: 1920x1080 
     +sssshhhyNMMNyssssssssssssyNMMMysssssss+   DE: GNOME 45.0 
     ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM: Mutter 
     ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM Theme: Adwaita 
     +sssshhhyNMMNyssssssssssssyNMMMysssssss+   Theme: Yaru-dark [GTK2/3] 
     .ssssssssdMMMNhsssssssssshNMMMdssssssss.   Icons: Yaru [GTK2/3] 
      /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/    Terminal: gnome-terminal 
       +sssssssssdmydMMMMMMMMddddyssssssss+     CPU: Intel Pentium G3220T (2) @ 2.60 
        /ssssssssssshdmNNNNmyNMMMMhssssss/      GPU: Intel HD Graphics 
         .ossssssssssssssssssdMMMNysssso.       Memory: 1895MiB / 3777MiB 
           -+sssssssssssssssssyyyssss+-
             `:+ssssssssssssssssss+:`                                   
                 .-/+oossssoo+/-.                                       

๐Ÿ“š Some useful info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment