The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.

This URL has Read-Only access.

Statistics
| Revision:

root / trunk / DomotiGa / RRDTool.module @ 238

History | View | Annotate | Download (15.2 kB)

1
' Gambas module file
2
3
' Description:
4
' RRDTool.module
5
' Support for RRDTool graphing.
6
7
' Development Status:
8
' Working, make hardcoded heartbeat and step values configurable, need better error checking.
9
' Maybe create separate graphs table in db.
10
11
' DomotiGa - an open source home automation program.
12
' Copyright(C) 2008-2009 Ron Klinkien
13
14
' Read file called COPYING for license details.
15
16
PUBLIC tRRDTool AS NEW Timer
17
18
PUBLIC SUB Run()
19
20
  RRDTool.CreateRRDs() ' check for missing rrd databases
21
22
  ' start poll timer for RRDTool
23
  tRRDTool = NEW Timer AS "tRRDTool"
24
  tRRDTool.Delay = Main.iRRDToolPollTime * 1000 ' multiply for seconds
25
  tRRDTool.Start
26
27
END
28
29
PUBLIC FUNCTION DeleteRRD(sDeviceName AS String)
30
31
  DIM sRRDName, sRRD AS String
32
33
  sRRDName = LCase(Replace$(sDeviceName, " ", ""))
34
  sRRDName = Replace$(sRRDName, "/", "")
35
  sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
36
  IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Delete rrd database " & sRRD & ".")
37
  SHELL "rm -f " & sRRD WAIT
38
39
END
40
41
PUBLIC FUNCTION DeleteAllRRDs()
42
43
  IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Delete rrd databases.")
44
  SHELL "rm -f " & Main.sBaseDir &/ "rrd/*.rrd" WAIT
45
46
END
47
48
PUBLIC FUNCTION DeleteAllGraphs()
49
50
  IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Delete rrd graphs.")
51
  SHELL "rm -f " & Main.sBaseDir &/ "rrd/graphs/*.png" WAIT
52
53
END
54
55
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
' find devices with graph boolean enabled,
57
' check for existance of rrd file, create one if missing
58
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59
PUBLIC FUNCTION CreateRRDs()
60
61
  DIM rDevice, rResult AS Result
62
  DIM sRRD, sRRDCmd, sRRDName AS String
63
64
  IF NOT Main.bRRDToolEnabled THEN RETURN
65
66
  TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE graph is TRUE AND enabled is TRUE")
67
  IF (rDevice.Count > 0) THEN
68
    FOR EACH rDevice
69
      IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled.")
70
      sRRDName = LCase(Replace$(rDevice!name, " ", ""))
71
      sRRDName = Replace$(sRRDName, "/", "")
72
      sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
73
      ' rrdtool create name.rrd -s 60 DS:temp:GAUGE:600:U:U DS:press:GAUGE:600:U:U DS:hum:GAUGE:600:U:U RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:6:600 RRA:AVERAGE:0.5:24:600 RRA:AVERAGE:0.5:288:600
74
      IF NOT Exist(sRRD) THEN
75
        sRRDCmd = "rrdtool create " & sRRD & " -s " & Main.iRRDToolPollTime & " "
76
        IF rDevice!valuerrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!valuerrddsname & ":" & rDevice!valuerrdtype & ":600:U:U "
77
        IF rDevice!value2rrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!value2rrddsname & ":" & rDevice!value2rrdtype & ":600:U:U "
78
        IF rDevice!value3rrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!value3rrddsname & ":" & rDevice!value3rrdtype & ":600:U:U "
79
        IF rDevice!value4rrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!value4rrddsname & ":" & rDevice!value4rrdtype & ":600:U:U "
80
        sRRDCmd = sRRDCmd & "RRA:AVERAGE:0.5:1:600 RRA:AVERAGE:0.5:2:600 RRA:AVERAGE:0.5:7:600 RRA:AVERAGE:0.5:30:600 RRA:AVERAGE:0.5:365:600"
81
        IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] " & sRRDCmd)
82
        SHELL sRRDCmd WAIT
83
      END IF
84
    NEXT
85
  ELSE
86
    IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] No device(s) with graphing enabled found!")
87
  END IF
88
89
END
90
91
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92
' find devices with graph boolean enabled, create graphs
93
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
PUBLIC FUNCTION CreateGraphs(sGroup AS String)
95
96
  DIM rDevice, rResult AS Result
97
  DIM sRRD, sRRDCmd, sRRDCmd2, sRRDCmd3, sRRDCmd4, sGraphImage, sRRDName, sTime AS String
98
  DIM aTimeFrame AS String[] = ["1h", "1d", "1w", "1m", "1y"]
99
  DIM aTimeStr AS String[]
100
101
  IF NOT Main.bRRDToolEnabled THEN RETURN
102
103
  TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE groups LIKE &1 AND graph is TRUE AND enabled is TRUE", "%" & sGroup & "%")
104
  IF (rDevice.Count > 0) THEN
105
    FOR EACH rDevice
106
      sRRDName = LCase(Replace$(rDevice!name, " ", ""))
107
      sRRDName = Replace$(sRRDName, "/", "")
108
      IF rDevice!valuerrddsname THEN
109
        IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for " & rDevice!valuerrddsname & ".")
110
        sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
111
        ' rrdtool graph /home/ron/domotiga/rrd/graphs/powerusage-hour.png --lazy --start -1h -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t "Power Usage - Last Hour" -v "Watt" DEF:watt=/home/ron/domotiga/rrd/powerusage.rrd:watt:AVERAGE LINE2:watt#00FF00:"Watt"
112
        FOR EACH sTime IN aTimeFrame
113
          sGraphImage = Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!valuerrddsname & "-" & sTime & ".png"
114
          sRRDCmd = "rrdtool graph " & sGraphImage & " --lazy --start -" & sTime & " -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t '" & rDevice!name & " " & Main.Caps(rDevice!valuerrddsname) & "' -v '" & rDevice!label & "' DEF:" & rDevice!valuerrddsname & "=" & sRRD & ":" & rDevice!valuerrddsname & ":AVERAGE LINE1:" & rDevice!valuerrddsname & "#00FF00:'" & Main.Caps(rDevice!valuerrddsname) & "'"
115
          sRRDCmd &= " GPRINT:" & rDevice!valuerrddsname & ":MIN:'Min\\:%1.1lf' GPRINT:" & rDevice!valuerrddsname & ":MAX:'Max\\:%1.1lf' GPRINT:" & rDevice!valuerrddsname & ":AVERAGE:'Avg\\:%1.1lf' GPRINT:" & rDevice!valuerrddsname & ":LAST:'Last\\:%1.1lf'"
116
          IF Main.bRRDToolDebug THEN
117
            Main.WriteDebugLog("[RRDTool] " & sRRDCmd)
118
          ELSE
119
            sRRDCmd &= " >/dev/null" & " 2>&1"
120
          END IF
121
          SHELL sRRDCmd WAIT
122
        NEXT
123
      END IF
124
      IF rDevice!value2rrddsname THEN
125
        IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for " & rDevice!value2rrddsname & ".")
126
        sRRDName = LCase(Replace$(rDevice!name, " ", ""))
127
        sRRDName = Replace$(sRRDName, "/", "")
128
        sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
129
        FOR EACH sTime IN aTimeFrame
130
          aTimeStr = Scan(sTime, "*,*")
131
          sGraphImage = Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value2rrddsname & "-" & sTime & ".png"
132
          ' rrdtool graph /home/ron/domotiga/rrd/graphs/powerusage-hour.png --lazy --start -1h -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t "Power Usage - Last Hour" -v "Watt" DEF:watt=/home/ron/domotiga/rrd/powerusage.rrd:watt:AVERAGE LINE2:watt#00FF00:"Watt"
133
          sRRDCmd2 = "rrdtool graph " & sGraphImage & " --lazy --start -" & sTime & " -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t '" & rDevice!name & " " & Main.Caps(rDevice!value2rrddsname) & "' -v '" & rDevice!label2 & "' DEF:" & rDevice!value2rrddsname & "=" & sRRD & ":" & rDevice!value2rrddsname & ":AVERAGE LINE1:" & rDevice!value2rrddsname & "#00FF00:'" & Main.Caps(rDevice!value2rrddsname) & "'"
134
          sRRDCmd2 &= " GPRINT:" & rDevice!value2rrddsname & ":MIN:'Min\\:%1.1lf' GPRINT:" & rDevice!value2rrddsname & ":MAX:'Max\\:%1.1lf' GPRINT:" & rDevice!value2rrddsname & ":AVERAGE:'Avg\\:%1.1lf' GPRINT:" & rDevice!value2rrddsname & ":LAST:'Last\\:%1.1lf'"
135
          IF Main.bRRDToolDebug THEN
136
            Main.WriteDebugLog(sRRDCmd2)
137
          ELSE
138
            sRRDCmd2 &= " >/dev/null" & " 2>&1"
139
          END IF
140
          SHELL sRRDCmd2 WAIT
141
        NEXT
142
      END IF
143
      IF rDevice!value3rrddsname THEN
144
        IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for " & rDevice!value3rrddsname & ".")
145
        sRRDName = LCase(Replace$(rDevice!name, " ", ""))
146
        sRRDName = Replace$(sRRDName, "/", "")
147
        sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
148
        FOR EACH sTime IN aTimeFrame
149
          aTimeStr = Scan(sTime, "*,*")
150
          sGraphImage = Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value3rrddsname & "-" & sTime & ".png"
151
          ' rrdtool graph /home/ron/domotiga/rrd/graphs/powerusage-hour.png --lazy --start -1h -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t "Power Usage - Last Hour" -v "Watt" DEF:watt=/home/ron/domotiga/rrd/powerusage.rrd:watt:AVERAGE LINE2:watt#00FF00:"Watt"
152
          sRRDCmd3 = "rrdtool graph " & sGraphImage & " --lazy --start -" & sTime & " -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t '" & rDevice!name & " " & Main.Caps(rDevice!value3rrddsname) & "' -v '" & rDevice!label3 & "' DEF:" & rDevice!value3rrddsname & "=" & sRRD & ":" & rDevice!value3rrddsname & ":AVERAGE LINE1:" & rDevice!value3rrddsname & "#00FF00:'" & Main.Caps(rDevice!value3rrddsname) & "'"
153
          sRRDCmd3 &= " GPRINT:" & rDevice!value3rrddsname & ":MIN:'Min\\:%1.1lf' GPRINT:" & rDevice!value3rrddsname & ":MAX:'Max\\:%1.1lf' GPRINT:" & rDevice!value3rrddsname & ":AVERAGE:'Avg\\:%1.1lf' GPRINT:" & rDevice!value3rrddsname & ":LAST:'Last\\:%1.1lf'"
154
          IF Main.bRRDToolDebug THEN
155
            Main.WriteDebugLog("[RRDTool] " & sRRDCmd3)
156
          ELSE
157
            sRRDCmd3 &= " >/dev/null" & " 2>&1"
158
          END IF
159
          SHELL sRRDCmd3 WAIT
160
        NEXT
161
      END IF
162
      IF rDevice!value4rrddsname THEN
163
        IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for " & rDevice!value4rrddsname & ".")
164
        sRRDName = LCase(Replace$(rDevice!name, " ", ""))
165
        sRRDName = Replace$(sRRDName, "/", "")
166
        sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
167
        FOR EACH sTime IN aTimeFrame
168
          aTimeStr = Scan(sTime, "*,*")
169
          sGraphImage = Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value4rrddsname & "-" & sTime & ".png"
170
          ' rrdtool graph /home/ron/domotiga/rrd/graphs/powerusage-hour.png --lazy --start -1h -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t "Power Usage - Last Hour" -v "Watt" DEF:watt=/home/ron/domotiga/rrd/powerusage.rrd:watt:AVERAGE LINE2:watt#00FF00:"Watt"
171
          sRRDCmd4 = "rrdtool graph " & sGraphImage & " --lazy --start -" & sTime & " -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t '" & rDevice!name & " " & Main.Caps(rDevice!value4rrddsname) & "' -v '" & rDevice!label4 & "' DEF:" & rDevice!value4rrddsname & "=" & sRRD & ":" & rDevice!value4rrddsname & ":AVERAGE LINE1:" & rDevice!value4rrddsname & "#00FF00:'" & Main.Caps(rDevice!value4rrddsname) & "'"
172
          sRRDCmd4 &= " GPRINT:" & rDevice!value4rrddsname & ":MIN:'Min\\:%1.1lf' GPRINT:" & rDevice!value4rrddsname & ":MAX:'Max\\:%1.1lf' GPRINT:" & rDevice!value4rrddsname & ":AVERAGE:'Avg\\:%1.1lf' GPRINT:" & rDevice!value4rrddsname & ":LAST:'Last\\:%1.1lf'"
173
          IF Main.bRRDToolDebug THEN
174
            Main.WriteDebugLog("[RRDTool] " & sRRDCmd4)
175
          ELSE
176
            sRRDCmd4 &= " >/dev/null" & " 2>&1"
177
          END IF
178
          SHELL sRRDCmd4 WAIT
179
        NEXT
180
      END IF
181
    NEXT
182
  ELSE
183
    IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] No device(s) with graphing enabled found!")
184
  END IF
185
186
END
187
188
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
189
' find devices with graph boolean enabled, return the names of their graphic images
190
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191
PUBLIC SUB Graphs(sGroup AS String) AS String
192
193
  DIM rDevice AS Result
194
  DIM sGraphs, sRRDName AS String
195
196
  TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE groups LIKE &1 AND graph is TRUE AND enabled is TRUE", "%" & sGroup & "%")
197
  IF (rDevice.Count > 0) THEN
198
    FOR EACH rDevice
199
      sRRDName = LCase(Replace$(rDevice!name, " ", ""))
200
      sRRDName = Replace$(sRRDName, "/", "")
201
      IF rDevice!valuerrddsname THEN
202
        IF Main.bRRDToolDebug THEN
203
          Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & " has graphing enabled for '" & rDevice!valuerrddsname & "'.")
204
          Main.WriteDebugLog("[RRDTool] Graphs - " & Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!valuerrddsname)
205
        END IF
206
        sGraphs &= Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!valuerrddsname & "|"
207
      END IF
208
      IF rDevice!value2rrddsname THEN
209
        IF Main.bRRDToolDebug THEN
210
          Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for '" & rDevice!value2rrddsname & "'.")
211
          Main.WriteDebugLog("[RRDTool] Graphs - " & Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value2rrddsname)
212
        END IF
213
        sGraphs &= Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value2rrddsname & "|"
214
      END IF
215
      IF rDevice!value3rrddsname THEN
216
        IF Main.bRRDToolDebug THEN
217
          Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for '" & rDevice!value3rrddsname & "'.")
218
          Main.WriteDebugLog("[RRDTool] Graphs - " & Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value3rrddsname)
219
        END IF
220
        sGraphs &= Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value3rrddsname & "|"
221
      END IF
222
      IF rDevice!value4rrddsname THEN
223
        IF Main.bRRDToolDebug THEN
224
          Main.WriteDebugLog("[RRDTool] Device with address '" & rDevice!address & "' named '" & rDevice!name & "' has graphing enabled for '" & rDevice!value4rrddsname & "'.")
225
          Main.WriteDebugLog("[RRDTool] Graphs - " & Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value4rrddsname)
226
        END IF
227
        sGraphs &= Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice!value4rrddsname & "|"
228
      END IF
229
    NEXT
230
  ELSE
231
    IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] No device(s) with graphing enabled found!")
232
  END IF
233
  ' return string with all graph image names in this group
234
  RETURN sGraphs
235
236
END
237
238
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
' find devices with graph enabled, and update rrd files with device values
240
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
241
PUBLIC FUNCTION UpdateRRDs()
242
243
  DIM rDevice, rResult AS Result
244
  DIM sSql, sRRD, sRRDCmd, sRRDName AS String
245
246
  TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE enabled is TRUE AND graph is TRUE")
247
  IF (rDevice.Count > 0) THEN
248
    FOR EACH rDevice
249
      sRRDName = LCase(Replace$(rDevice!name, " ", ""))
250
      sRRDName = Replace$(sRRDName, "/", "")
251
      sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd"
252
      ' rrdtool update name.rrd N:%1:%2:%3
253
      sRRDCmd = "rrdtool update " & sRRD & " N"
254
      IF rDevice!valuerrddsname THEN sRRDCmd = sRRDCmd & ":" & rDevice!value
255
      IF rDevice!value2rrddsname THEN sRRDCmd = sRRDCmd & ":" & rDevice!value2
256
      IF rDevice!value3rrddsname THEN sRRDCmd = sRRDCmd & ":" & rDevice!value3
257
      IF rDevice!value4rrddsname THEN sRRDCmd = sRRDCmd & ":" & rDevice!value4
258
      IF Main.bRRDToolDebug THEN
259
        Main.WriteDebugLog("[RRDTool] " & sRRDCmd)
260
      ELSE
261
        sRRDCmd &= " >/dev/null" & " 2>&1"
262
      END IF
263
      SHELL sRRDCmd WAIT
264
    NEXT
265
  ELSE
266
    IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] No device(s) with graphing enabled found!")
267
  END IF
268
269
END
270
271
PUBLIC SUB tRRDTool_Timer()
272
273
  UpdateRRDs() ' update rrd values every Polltime seconds
274
275
END