Skip to content

Commit 1aee985

Browse files
author
Patrick Schless
committed
Initial commit
0 parents  commit 1aee985

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

rrd.coffee

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
sys = require('sys')
2+
exec = require('child_process').exec
3+
spawn = require('child_process').spawn
4+
fs = require('fs')
5+
6+
binaryRRDFile = require('./binaryFile.js')
7+
RRDReader = require('./rrdFile.js').RRDFile
8+
RRDRecord = require('./rrdRecord').RRDRecord
9+
10+
class RRD
11+
constructor: (@filename) ->
12+
13+
create: (rrdArgs, options, cb) ->
14+
start = options.start ? new Date
15+
console.log start
16+
cmd = "rrdtool create #{@filename} --start #{_rrdTime(start)} --step 300 #{rrdArgs.join(" ")}"
17+
console.log " - #{cmd}"
18+
exec(cmd, cb)
19+
20+
destroy: (cb) ->
21+
fs.unlink(@filename, cb)
22+
23+
dump: (cb) ->
24+
this.rrdExec("dump", "", cb)
25+
26+
rrdExec: (command, cmd_args, cb) ->
27+
cmd = "rrdtool #{command} #{@filename} #{cmd_args}"
28+
console.log cmd
29+
exec cmd, cb
30+
31+
update: (time, value1, value2, value3, cb) ->
32+
this.rrdExec("update", "#{_rrdTime(time)}:#{value1}:#{value2}:#{value3}", cb)
33+
34+
fetch: (start, end, cb) ->
35+
this.rrdExec "fetch", "AVERAGE --start #{start} --end #{end}", (err, data) ->
36+
lines = data.split("\n")
37+
fieldNames = lines.shift().replace(new RegExp("^ +"), "").split(new RegExp(" +"))
38+
lines.shift()
39+
40+
records = for line in lines
41+
continue if line == ""
42+
continue if line.match(" nan ")
43+
44+
fields = line.split(new RegExp("[: ]+"))
45+
record = new RRDRecord(fields.shift(), fieldNames)
46+
for i in [0..fields.length-1]
47+
record[fieldNames[i]] = fields[i]
48+
record
49+
50+
cb(records)
51+
52+
graph: (graphFilename, lines, options, cb) ->
53+
cmd = "rrdtool graph #{graphFilename} #{(this._rrdGraphLine(line) for line in lines).join(" ")} --start #{options.start}"
54+
console.log cmd
55+
exec cmd, cb
56+
57+
_datasourceInfo = (rrdReader, dsNum) ->
58+
datasource = rrdReader.getDS(dsNum)
59+
60+
values = []
61+
rra = rrdReader.getRRA(dsNum)
62+
for rowNum in [0..rra.row_cnt-1]
63+
do (rowNum) ->
64+
values.push(rra.getElFast(rowNum, dsNum))
65+
66+
result = {}
67+
result[datasource.getName()] = values
68+
return result
69+
70+
_rrdGraphLine: (line) =>
71+
return "DEF:#{line.name}=#{@filename}:#{line.name}:AVERAGE LINE2:#{line.name}#{line.color}"
72+
73+
_rrdTime = (date) ->
74+
return Math.round(date.valueOf() / 1000)
75+
76+
RRD.restore = (filenameXML, filenameRRD, cb) ->
77+
exec "rrdtool restore #{filenameXML} #{filenameRRD}", cb
78+
exports.RRD = RRD

rrdRecord.coffee

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class RRDRecord
2+
constructor: (@timestamp, @fieldNames) ->
3+
4+
exports.RRDRecord = RRDRecord

0 commit comments

Comments
 (0)