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.
root / trunk / DomotiGa / RRDTool.module @ 348
History | View | Annotate | Download (9.7 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-2010 Ron Klinkien |
| 13 | |
| 14 | ' Read file called COPYING for license details. |
| 15 | |
| 16 | PUBLIC tRRDTool AS Timer |
| 17 | |
| 18 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | ' check for/create missing rrd files and start timer |
| 20 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 21 | PUBLIC SUB Run() |
| 22 | |
| 23 | ' check for missing rrd databases |
| 24 | CreateRRDs() |
| 25 | |
| 26 | ' start poll timer for RRDTool |
| 27 | tRRDTool = NEW Timer AS "tRRDTool" |
| 28 | tRRDTool.Delay = Main.iRRDToolPollTime * 1000 ' multiply for seconds |
| 29 | tRRDTool.Start |
| 30 | |
| 31 | END |
| 32 | |
| 33 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 34 | ' gets called at each timer event |
| 35 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 36 | PUBLIC SUB tRRDTool_Timer() |
| 37 | |
| 38 | UpdateRRDs() ' update rrd values every Polltime seconds |
| 39 | |
| 40 | END |
| 41 | |
| 42 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 43 | ' delete rrd file belonging to device |
| 44 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 | PUBLIC SUB DeleteRRD(sDeviceName AS String) |
| 46 | |
| 47 | DIM sRRDName, sRRD, sCmd AS String |
| 48 | |
| 49 | sRRDName = LCase(Replace$(sDeviceName, " ", "")) |
| 50 | sRRDName = Replace$(sRRDName, "/", "") |
| 51 | sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd" |
| 52 | sCmd = "rm -f " & sRRD |
| 53 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] Delete rrd database ") & sRRD & (" with ") & sCmd & ".")
|
| 54 | SHELL sCmd WAIT |
| 55 | |
| 56 | END |
| 57 | |
| 58 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 59 | ' delete all rrd files |
| 60 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 61 | PUBLIC SUB DeleteAllRRDs() |
| 62 | |
| 63 | DIM sCmd AS String |
| 64 | |
| 65 | sCmd = "rm -f " & Main.sBaseDir &/ "rrd/*.rrd" |
| 66 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] Delete rrd databases with ") & sCmd & ".")
|
| 67 | SHELL sCmd WAIT |
| 68 | |
| 69 | END |
| 70 | |
| 71 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | ' delete all graphic images |
| 73 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 74 | PUBLIC SUB DeleteAllGraphs() |
| 75 | |
| 76 | DIM sCmd AS String |
| 77 | |
| 78 | sCmd = "rm -f " & Main.sBaseDir &/ "rrd/graphs/*.png" |
| 79 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] Delete rrd graphs with ") & sCmd & ".")
|
| 80 | SHELL sCmd WAIT |
| 81 | |
| 82 | END |
| 83 | |
| 84 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 85 | ' find all devices with graph boolean enabled and |
| 86 | ' check for existance of rrd file, create one if missing |
| 87 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 88 | PUBLIC SUB CreateRRDs() |
| 89 | |
| 90 | DIM rDevice AS Result |
| 91 | DIM sRRD, sRRDCmd, sRRDName AS String |
| 92 | |
| 93 | IF NOT Main.bRRDToolEnabled THEN RETURN |
| 94 | |
| 95 | TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE graph is TRUE AND enabled is TRUE")
|
| 96 | IF (rDevice.Count > 0) THEN |
| 97 | FOR EACH rDevice |
| 98 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] Device with address '") & rDevice!address & ("' named '") & rDevice!name & ("' has graphing enabled."))
|
| 99 | sRRDName = LCase(Replace$(rDevice!name, " ", "")) |
| 100 | sRRDName = Replace$(sRRDName, "/", "") |
| 101 | sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd" |
| 102 | IF NOT Exist(sRRD) THEN |
| 103 | sRRDCmd = "rrdtool create " & sRRD & " -s " & Main.iRRDToolPollTime & " " |
| 104 | IF rDevice!valuerrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!valuerrddsname & ":" & rDevice!valuerrdtype & ":600:U:U " |
| 105 | IF rDevice!value2rrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!value2rrddsname & ":" & rDevice!value2rrdtype & ":600:U:U " |
| 106 | IF rDevice!value3rrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!value3rrddsname & ":" & rDevice!value3rrdtype & ":600:U:U " |
| 107 | IF rDevice!value4rrddsname THEN sRRDCmd = sRRDCmd & "DS:" & rDevice!value4rrddsname & ":" & rDevice!value4rrdtype & ":600:U:U " |
| 108 | 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" |
| 109 | sRRDCmd &= " 2>&1 >/dev/null" |
| 110 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] " & sRRDCmd)
|
| 111 | SHELL sRRDCmd WAIT |
| 112 | END IF |
| 113 | NEXT |
| 114 | ELSE |
| 115 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] No device(s) with graphing enabled found!"))
|
| 116 | END IF |
| 117 | |
| 118 | END |
| 119 | |
| 120 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 121 | ' find devices with graph boolean enabled and create graphs |
| 122 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 123 | PUBLIC SUB CreateGraphs(sGroup AS String, sTime AS String) |
| 124 | |
| 125 | DIM rDevice AS Result |
| 126 | DIM sRRD, sRRDCmd, sGraphImage, sRRDName, sValueRRDsName AS String |
| 127 | DIM aValueRRDsName AS String[] = ["valuerrddsname", "value2rrddsname", "value3rrddsname", "value4rrddsname"] |
| 128 | |
| 129 | IF NOT Main.bRRDToolEnabled THEN RETURN |
| 130 | |
| 131 | TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE groups LIKE &1 AND graph is TRUE AND enabled is TRUE", "%" & sGroup & "%")
|
| 132 | IF (rDevice.Count > 0) THEN |
| 133 | FOR EACH rDevice |
| 134 | sRRDName = LCase(Replace$(rDevice!name, " ", "")) |
| 135 | sRRDName = Replace$(sRRDName, "/", "") |
| 136 | FOR EACH sValueRRDsName IN aValueRRDsName |
| 137 | IF rDevice[sValueRRDsName] THEN |
| 138 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] Device with address '") & rDevice!address & ("' named '") & rDevice!name & ("' has graphing enabled for ") & rDevice[sValueRRDsName] & ".")
|
| 139 | sRRD = Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd" |
| 140 | sGraphImage = Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice[sValueRRDsName] & "-" & sTime & ".png" |
| 141 | sRRDCmd = "rrdtool graph " & sGraphImage & " --lazy --start -" & sTime & " -c SHADEA#FFFFFF -c SHADEB#FFFFFF -c BACK#FFFFFF -t '" & rDevice!name & " " & Main.Caps(rDevice[sValueRRDsName]) & "' -v '" & rDevice!label & "' DEF:" & rDevice[sValueRRDsName] & "=" & sRRD & ":" & rDevice[sValueRRDsName] & ":AVERAGE LINE1:" & rDevice[sValueRRDsName] & "#00FF00:'" & Main.Caps(rDevice[sValueRRDsName]) & "'" |
| 142 | sRRDCmd &= " GPRINT:" & rDevice[sValueRRDsName] & ":MIN:'Min\\:%1.1lf' GPRINT:" & rDevice[sValueRRDsName] & ":MAX:'Max\\:%1.1lf' GPRINT:" & rDevice[sValueRRDsName] & ":AVERAGE:'Avg\\:%1.1lf' GPRINT:" & rDevice[sValueRRDsName] & ":LAST:'Last\\:%1.1lf'" |
| 143 | sRRDCmd &= " 2>&1 >/dev/null" |
| 144 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] " & sRRDCmd)
|
| 145 | SHELL sRRDCmd WAIT |
| 146 | END IF |
| 147 | NEXT |
| 148 | NEXT |
| 149 | ELSE |
| 150 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] No device(s) with graphing enabled found!"))
|
| 151 | END IF |
| 152 | |
| 153 | END |
| 154 | |
| 155 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 156 | ' find devices with graph boolean enabled, return the names of their graphic images |
| 157 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 158 | PUBLIC FUNCTION Graphs(sGroup AS String) AS String |
| 159 | |
| 160 | DIM rDevice AS Result |
| 161 | DIM sGraphs, sRRDName, sValueRRDsName AS String |
| 162 | DIM aValueRRDsName AS String[] = ["valuerrddsname", "value2rrddsname", "value3rrddsname", "value4rrddsname"] |
| 163 | |
| 164 | TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE groups LIKE &1 AND graph is TRUE AND enabled is TRUE", "%" & sGroup & "%")
|
| 165 | IF (rDevice.Count > 0) THEN |
| 166 | FOR EACH rDevice |
| 167 | sRRDName = LCase(Replace$(rDevice!name, " ", "")) |
| 168 | sRRDName = Replace$(sRRDName, "/", "") |
| 169 | FOR EACH sValueRRDsName IN aValueRRDsName |
| 170 | IF rDevice[sValueRRDsName] THEN |
| 171 | sGraphs &= Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice[sValueRRDsName] & "|" |
| 172 | IF NOT Main.bRRDToolDebug THEN CONTINUE |
| 173 | Main.WriteDebugLog(("[RRDTool] Device with address '") & rDevice!address & ("' named '") & rDevice!name & (" has graphing enabled for '") & rDevice[sValueRRDsName] & "'.")
|
| 174 | Main.WriteDebugLog(("[RRDTool] Graphs - ") & Main.sBaseDir &/ "rrd/graphs/" &/ sRRDName & "-" & rDevice[sValueRRDsName])
|
| 175 | END IF |
| 176 | NEXT |
| 177 | NEXT |
| 178 | ELSE |
| 179 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] No device(s) with graphing enabled found!"))
|
| 180 | END IF |
| 181 | ' return string with all graph image names in this group |
| 182 | RETURN sGraphs |
| 183 | |
| 184 | END |
| 185 | |
| 186 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 187 | ' find devices with graph enabled, and update rrd files with device values |
| 188 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 189 | PUBLIC SUB UpdateRRDs() |
| 190 | |
| 191 | DIM rDevice AS Result |
| 192 | DIM sRRDCmd, sRRDName AS String |
| 193 | |
| 194 | TRY rDevice = Main.hDB.Exec("SELECT * FROM devices WHERE enabled is TRUE AND graph is TRUE")
|
| 195 | IF (rDevice.Count > 0) THEN |
| 196 | FOR EACH rDevice |
| 197 | sRRDName = LCase(Replace$(rDevice!name, " ", "")) |
| 198 | sRRDName = Replace$(sRRDName, "/", "") |
| 199 | sRRDCmd = "rrdtool update " & Main.sBaseDir &/ "rrd" &/ sRRDName & ".rrd" & " N" |
| 200 | IF rDevice!valuerrddsname THEN |
| 201 | IF rDevice!valuerrdtype = "COUNTER" THEN |
| 202 | sRRDCmd &= ":" & Format(rDevice!value, "####") |
| 203 | ELSE |
| 204 | sRRDCmd &= ":" & rDevice!value |
| 205 | END IF |
| 206 | END IF |
| 207 | IF rDevice!value2rrddsname THEN |
| 208 | IF rDevice!value2rrdtype = "COUNTER" THEN |
| 209 | sRRDCmd &= ":" & Format(rDevice!value2, "####") |
| 210 | ELSE |
| 211 | sRRDCmd &= ":" & rDevice!value2 |
| 212 | END IF |
| 213 | END IF |
| 214 | IF rDevice!value3rrddsname THEN |
| 215 | IF rDevice!value3rrdtype = "COUNTER" THEN |
| 216 | sRRDCmd &= ":" & Format(rDevice!value3, "####") |
| 217 | ELSE |
| 218 | sRRDCmd &= ":" & rDevice!value3 |
| 219 | END IF |
| 220 | END IF |
| 221 | IF rDevice!value4rrddsname THEN |
| 222 | IF rDevice!value4rrdtype = "COUNTER" THEN |
| 223 | sRRDCmd &= ":" & Format(rDevice!value4, "####") |
| 224 | ELSE |
| 225 | sRRDCmd &= ":" & rDevice!value4 |
| 226 | END IF |
| 227 | END IF |
| 228 | sRRDCmd &= " 2>&1 >/dev/null" |
| 229 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog("[RRDTool] " & sRRDCmd)
|
| 230 | SHELL sRRDCmd WAIT |
| 231 | NEXT |
| 232 | ELSE |
| 233 | IF Main.bRRDToolDebug THEN Main.WriteDebugLog(("[RRDTool] No device(s) with graphing enabled found!"))
|
| 234 | END IF |
| 235 | |
| 236 | END |
