LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-11-2022, 10:26 AM   #1
bomberb17
Member
 
Registered: Jul 2005
Posts: 71

Rep: Reputation: 0
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?

Last edited by bomberb17; 12-11-2022 at 10:45 AM.
 
Old 12-11-2022, 10:55 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,714

Rep: Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734
#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.

Last edited by wpeckham; 12-11-2022 at 10:59 AM.
 
Old 12-11-2022, 11:46 AM   #3
bomberb17
Member
 
Registered: Jul 2005
Posts: 71

Original Poster
Rep: Reputation: 0
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?
 
Old 12-11-2022, 08:29 PM   #4
bomberb17
Member
 
Registered: Jul 2005
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by wpeckham View Post
#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?
 
Old 12-11-2022, 08:54 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
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.
 
1 members found this post helpful.
Old 12-11-2022, 09:47 PM   #6
bomberb17
Member
 
Registered: Jul 2005
Posts: 71

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
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
 
Old 12-12-2022, 10:18 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
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.
 
1 members found this post helpful.
Old 12-12-2022, 11:10 AM   #8
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,714

Rep: Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734Reputation: 2734
Quote:
Originally Posted by MadeInGermany View Post
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.
 
Old 12-13-2022, 02:44 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
Quote:
Originally Posted by wpeckham View Post
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!
 
  


Reply

Tags
rrdtools



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to plot a graph using cacti with rrd database? HariharanV Linux - Software 1 11-03-2011 02:56 PM
LXer: Maintaining a MYSQL Database Using mysqlcheck LXer Syndicated Linux News 0 12-05-2009 12:20 PM
mrtg & round robin database(rrd) nawuza Linux - Newbie 2 07-29-2008 08:02 PM
Script to dump and restore rrd files 0.o Programming 2 12-31-2007 12:07 PM
rrd mrtg & mrtg-rrd.cgi ducati620 Linux - Newbie 0 06-03-2004 07:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration