-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrrd.coffee
82 lines (65 loc) · 2.13 KB
/
rrd.coffee
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
sys = require('sys')
exec = require('child_process').exec
spawn = require('child_process').spawn
fs = require('fs')
RRDRecord = require('./rrdRecord').RRDRecord
class RRD
constructor: (@filename) ->
create: (rrdArgs, options, cb) ->
start = options.start ? new Date
cmdArgs = ["create", @filename, "--start", _rrdTime(start), "--step", 300].concat rrdArgs
console.log " - rrdtool #{cmdArgs.join(" ")}"
proc = spawn("rrdtool", cmdArgs)
err = ""
proc.stderr.on 'data', (data) ->
err += data
proc.on 'exit', (code) ->
if code == 0
cb undefined, 'ok'
else
cb err, undefined
destroy: (cb) ->
fs.unlink(@filename, cb)
dump: (cb) ->
@rrdSpawn("dump", [], cb)
rrdExec: (command, cmd_args, cb) ->
cmd = "rrdtool #{command} #{@filename} #{cmd_args}"
console.log cmd
exec(cmd, {maxBuffer: 500 * 1024}, cb)
rrdSpawn: (command, args, cb) ->
proc = spawn("rrdtool", [command, @filename].concat(args))
err = ""
out = ""
proc.stderr.on 'data', (data) ->
err += data
proc.stdout.on 'data', (data) ->
out += data
proc.on 'exit', (code) ->
if code == 0
cb null, out
else
cb err, null
update: (time, values, cb) ->
@rrdSpawn("update", ["#{_rrdTime(time)}:#{values.join(':')}"], cb)
fetch: (start, end, cb) ->
this.rrdExec "fetch", "AVERAGE --start #{start} --end #{end}", (err, data) ->
if err
cb(err.message)
return
lines = data.split("\n")
fieldNames = lines.shift().replace(new RegExp("^ +"), "").split(new RegExp(" +"))
lines.shift()
records = for line in lines
continue if line == ""
continue if line.match(" nan ")
fields = line.split(new RegExp("[: ]+"))
record = new RRDRecord(fields.shift(), fieldNames)
for i in [0..fields.length-1]
record[fieldNames[i]] = fields[i]
record
cb(undefined, records)
_rrdTime = (date) ->
return Math.round(date.valueOf() / 1000)
RRD.restore = (filenameXML, filenameRRD, cb) ->
exec "rrdtool restore #{filenameXML} #{filenameRRD}", cb
exports.RRD = RRD