diff options
author | erg <uinarf@autistici.org> | 2024-01-27 19:30:14 +0100 |
---|---|---|
committer | erg <uinarf@autistici.org> | 2024-01-27 19:30:14 +0100 |
commit | 83c4d2e1b9213c78b0b472a1ed4484cf2590531f (patch) | |
tree | d3a0b0e93dfe607f96f91d20f67baca6d3f5cb75 /generate_rrd_db.sh | |
download | MQTT_for_pie-83c4d2e1b9213c78b0b472a1ed4484cf2590531f.tar.gz MQTT_for_pie-83c4d2e1b9213c78b0b472a1ed4484cf2590531f.tar.bz2 MQTT_for_pie-83c4d2e1b9213c78b0b472a1ed4484cf2590531f.zip |
Initial commit
Diffstat (limited to 'generate_rrd_db.sh')
-rwxr-xr-x | generate_rrd_db.sh | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/generate_rrd_db.sh b/generate_rrd_db.sh new file mode 100755 index 0000000..e81b64d --- /dev/null +++ b/generate_rrd_db.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +RRD_DATABASE="" +RRD_GRAPH=w1_graph.png +# Base interval in seconds with which data will be fed into the database: +STEP=15 +# One wire sensor address: +ROM=00-055300000000 # TODO: update me with a real thing! +# Seconds in a year: 31'536'000 +GRAPH=false +CREATE=false + +function Help() { + # Display help + echo "Command generating graph from rrd database" + echo + echo "Syntax: generate_rrd [-h|-g|-c|-o|-i]" + echo "options:" + echo "-h Print this help" + echo "-g Create graph" + echo "-c Create database" + echo "-i Input rrd database file" + echo "-o Output image, defaults to 'w1_graph.png'" + exit 0 +} + + +# Create rrd database if not exist, +# TODO: generate more graphs based on averages +# Keep once in 5 minutes average, min and max for a month, +# Keep once an hour for a year: +function rrd_create() { + if test -f "$RRD_DATABASE"; then + echo "Database already exists, exiting." + exit 1 + else + rrdcreate "$RRD_DATABASE" --step="$STEP" \ + DS:temp:GAUGE:45:U:U \ + RRA:AVERAGE:0.5:1:108000 \ + RRA:AVERAGE:0.5:20:5400 \ + RRA:MIN:0.5:20:5400 \ + RRA:MAX:0.5:20:5400 \ + RRA:AVERAGE:0.5:240:131400 \ + RRA:MAX:0.5:240:131400 \ + RRA:AVERAGE:0.5:240:131400 + fi +} + +# Generate graph image: +rrd_graph() { + echo "Creating graph $RRD_GRAPH" + rrdtool graph "$RRD_GRAPH" --start -86400 --end now \ + --vertical-label "Temperature C" \ + --upper-limit 40 \ + --lower-limit -30 \ + -w 640 -h 480 \ + DEF:temperature="$RRD_DATABASE":temp:AVERAGE \ + LINE1:temperature#00FF00:"temperature C" +} + +while getopts hgci:o: flag; do + case "${flag}" in + h) Help # Display help + exit;; + i) # Input rrd file + RRD_DATABASE=${OPTARG};; + o) # Output image + RRD_GRAPH=${OPTARG};; + c) # Create rrd database + echo "Creating database ..." + CREATE=true;; + g) # Create graph + echo "Creating graph ..." + GRAPH=true;; + \?) # Invalid option + echo "Invalid option" + Help;; + esac +done + +if [ -z "$RRD_DATABASE" ] +then + echo "Please specify rrd database." + Help +fi + +if [[ $CREATE == false ]] && [[ $GRAPH == false ]];then + echo "Specify either database creation (-c) or graph generation (-g) option" + Help +fi + +if $CREATE; then + rrd_create +fi + +if $GRAPH; then + rrd_graph +fi |