Code Done Right!

Basic data collection

The below script will allow for basic data collection. You can set up a crontab so that it runs every five minutes and updates a file, from which you can copy the data to Excel or Libre Offiec Calc to create a chart showing overall performance.

The data collection script gathers the following information

  • Date
  • Time
  • CPU temperature
  • RAM usage
  • Average 5 minute CPU load

Additional packages required

  • bc

In order for the script to neatly display temperature, you have to install a package that helps with math.

Run the following code

sudo apt install bc

Data collection script code

#!/bin/bash

data_output=/var/zzz_log/performance.log

date="$(which date)"
timestamp=`$date '+%Y-%m-%d %H:%M'`

cur_temp=$(cat /sys/class/thermal/thermal_zone0/temp)
cur_temp="$(echo "$cur_temp / 1000" | bc -l | xargs printf "%1.0f")"

ram_used="$(free -t -m | grep "Mem" | awk '{print $3}')"

read -r loadavg_five rest < /proc/loadavg

echo "$timestamp ; $cur_temp ; $ram_used ; $loadavg_five" 1>> $data_output

Performance data will be saved in the following location

data_output=/var/zzz_log/performance.log

CAUTION! Folder, in which data is supposed to be saved has to exist. If you do not have /var/zzz_log/ folder, create it or change the script so that data is written to an existing place. If the file is missing, it will be created on the first run.

After you run the script for the first time, check if the data is actually being written to the file with the following command

cat /var/zzz_log/performance.log

You should see output similar to this

2020-02-05 20:32 ; 41 ; 251 ; 0.00

Once you have enough data, you can construct a chart to showcase the performance of your server

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.