LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script for maintaining system load in RRD database (https://www.linuxquestions.org/questions/linux-general-1/script-for-maintaining-system-load-in-rrd-database-4175719663/)

bomberb17 12-11-2022 10:26 AM

Script for maintaining system load in RRD database
 
I want to make a script which saves the system load average over the last 1/5/15 minutes in a .rrd file every minute.
I created the rrd file as follows:

Code:

rrdtool create cpu_load.rrd --start now --step 60 --no-overwrite  DS:load1:GAUGE:120:U:U DS:load5:GAUGE:120:U:U DS:load15:GAUGE:120:U:U RRA:AVERAGE:0.5:1:525600
My script is as follows:

Code:

#!/bin/bash

READ_LOAD=$(cat /proc/loadavg)
LOAD1_VALUE=$(echo $READ_LOAD | awk '{print $1}')
LOAD5_VALUE=$(echo $READ_LOAD | awk '{print $2}')
LOAD15_VALUE=$(echo $READ_LOAD | awk '{print $3}')

rrdtool update /home/user/rrd/cpu_load.rrd ${TIMESTAMP}:${LOAD1_VALUE}:${LOAD5_VALUE}:${LOAD15_VALUE}

However I am getting the following error from the script:

Code:

ERROR: /home/user/rrd/cpu_load.rrd: error while parsing time in get_time_from_reading - Cannot convert '' to float
Can someone help me with fixing this?

wpeckham 12-11-2022 10:55 AM

#1 you are not showing us where you set TIMESTAMP, and since the error states that is where the problem exists we need to see that code.

#2 Why are you calling AWK? Bash string handling can handle this with less overhead, memory footprint, and disk I/O.
Code:

LOADS=`/bin/cat /proc/loadavg`
LOAD1_VALUE=${LOADS:0:4}
LOAD5_VALUE=${LOADS:5:4}
LOAD15_VALUE=${LOADS:10:4}

would work as well and faster. Note that this would only work as long as loads were less than 10.00. If they will be greater than calling cut, or awk, starts to make better sense. There is a way to use bash string handling by searching for the positions of the spaces to set the indexing, but doing that right would require a few more lines.

bomberb17 12-11-2022 11:46 AM

Ah right, so stupid mistake, I accidentally deleted the code for timestamp!

So I get it now through
Code:

TIMESTAMP=$(date +%s)
and I don't get the error :)

Is the rrdtool update command correct?

bomberb17 12-11-2022 08:29 PM

Quote:

Originally Posted by wpeckham (Post 6397274)
#1 you are not showing us where you set TIMESTAMP, and since the error states that is where the problem exists we need to see that code.

#2 Why are you calling AWK? Bash string handling can handle this with less overhead, memory footprint, and disk I/O.
Code:

LOADS=`/bin/cat /proc/loadavg`
LOAD1_VALUE=${LOADS:0:4}
LOAD5_VALUE=${LOADS:5:4}
LOAD15_VALUE=${LOADS:10:4}

would work as well and faster. Note that this would only work as long as loads were less than 10.00. If they will be greater than calling cut, or awk, starts to make better sense. There is a way to use bash string handling by searching for the positions of the spaces to set the indexing, but doing that right would require a few more lines.

Here's my equivalent script for recording memory usage:

Code:

#!/bin/bash
LOAD_RRD=/home/user/rrd/mem.rrd

READ_MEM=$(free -m)
USED_MEM_VALUE=$(echo $READ_MEM | awk '{print $9}')
FREE_MEM_VALUE=$(echo $READ_MEM | awk '{print $10}')
BUFF_MEM_VALUE=$(echo $READ_MEM | awk '{print $12}')
AVAIL_MEM_VALUE=$(echo $READ_MEM | awk '{print $13}')
USEDSW_MEM_VALUE=$(echo $READ_MEM | awk '{print $16}')
FREESW_MEM_VALUE=$(echo $READ_MEM | awk '{print $17}')
TIMESTAMP=$(date +%s)

rrdtool update $LOAD_RRD ${TIMESTAMP}:${USED_MEM_VALUE}:${FREE_MEM_VALUE}:${BUFF_MEM_VALUE}:${AVAIL_MEM_VALUE}:${USEDSW_MEM_VALUE}:${FREESW_MEM_VALUE}

Is there a more efficient way to do this in the way you suggested above?

syg00 12-11-2022 08:54 PM

You were given excellent advice in another thread for that memory script - all of which you appear to have chosen to ignore.

Study it and apply similar here.

bomberb17 12-11-2022 09:47 PM

Quote:

Originally Posted by syg00 (Post 6397360)
You were given excellent advice in another thread for that memory script - all of which you appear to have chosen to ignore.

Study it and apply similar here.

Yup sorry I forgot about it, I didn't choose to ignore though, I'm a bit confused with all those scripts I'm working on.
Thanks

MadeInGermany 12-12-2022 10:18 AM

And of course
Code:

read LOAD1_VALUE LOAD5_VALUE LOAD15_VALUE x < /proc/loadavg
A file -> a simple redirection.
A single line -> one read, no { code } group needed.

wpeckham 12-12-2022 11:10 AM

Quote:

Originally Posted by MadeInGermany (Post 6397446)
And of course
Code:

read LOAD1_VALUE LOAD5_VALUE LOAD15_VALUE x < /proc/loadavg
A file -> a simple redirection.
A single line -> one read, no { code } group needed.

Best answer for loading up those variables.

MadeInGermany 12-13-2022 02:44 AM

Quote:

Originally Posted by wpeckham (Post 6397459)
Best answer for loading up those variables.

See the general multi-line and from-a-command processing.
https://www.linuxquestions.org/quest...1/#post6396413
I wish more people would use the read command and the redirection of a code block. All shell books and tutorials should present it!


All times are GMT -5. The time now is 09:33 PM.