summaryrefslogtreecommitdiff
path: root/generate_rrd_db.sh
blob: e81b64d666bb38925882c0e79b1809984abd33c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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