#!/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