Skip to content

Add rrd info #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
"type" : "git"
, "url" : "https://github.com/plainlystated/coffeescript-rrd"
}
, "devDependencies" : {
"coffee-script": "~> 1.1.2"
, "vows": "~> 0.5.3"
}
}
5 changes: 5 additions & 0 deletions rrd.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ spawn = require('child_process').spawn
fs = require('fs')

RRDRecord = require('./rrdRecord').RRDRecord
RRDInfo = require('./rrdInfo').RRDInfo

class RRD
constructor: (@filename) ->
Expand Down Expand Up @@ -74,6 +75,10 @@ class RRD

cb(undefined, records)

info: (cb) ->
@rrdSpawn "info", [], (err, results) ->
return cb(null, new RRDInfo(results))

_rrdTime = (date) ->
return Math.round(date.valueOf() / 1000)

Expand Down
38 changes: 31 additions & 7 deletions rrd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions rrdInfo.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class RRDInfo
constructor: (raw_output) ->
this.parse_line(line) for line in raw_output.split('\n')

ds_names: () ->
keys = []
keys.push(k) for k of this['ds']
return keys

parse_line: (line) ->
line = line.replace(/["\s]/g, '')
return if line == ''

[key, value] = line.split('=')

key_tree = if key.match /[\.\[]/
key.replace(/\[/g, '.').replace(/[\]\s]/g, '').split('.')
else
[key]

_add_value(key_tree, value, this)

_add_value = (key_tree, value, obj) ->
return obj[key_tree[0]] = value if key_tree.length == 1
new_key = key_tree.shift()
obj[new_key] ?= new Object

return _add_value(key_tree, value, obj[new_key])

exports.RRDInfo = RRDInfo
48 changes: 48 additions & 0 deletions rrdInfo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/info-test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
vows = require('vows')
assert = require('assert')
RRDInfo = require('../rrdInfo').RRDInfo
valid_contents = require('fs').readFileSync('./valid.info').toString()

vows.describe('RRDInfo').addBatch(
'from a valid rrd file':
topic: new RRDInfo(valid_contents)

'should have the filename set': (topic) ->
assert.equal(topic.filename, 'valid.rrd')

'should parse multi level lines': (topic) ->
assert.equal(topic.ds.state.last_ds, 0)

'should list the ds_names': (topic) ->
assert.include(topic.ds_names(), 'temperature')
assert.include(topic.ds_names(), 'target_temp')
assert.include(topic.ds_names(), 'state')

).export(module, {error: false})
12 changes: 12 additions & 0 deletions test/rrd-test.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vows = require('vows')
assert = require('assert')
RRD = require('../rrd').RRD
RRDInfo = require('../rrdInfo').RRDInfo
exec = require('child_process').exec

vows.describe('RRD').addBatch(
Expand Down Expand Up @@ -99,6 +100,17 @@ vows.describe('RRD').addBatch(
# Need to figure out how to test this without messing up subsequent test runs
# 'returns no error': (err) ->
# assert.equal(err, undefined)
'when infoing':
topic: (rrd) ->
rrd.info(@callback)
return

'returns no error': (err, data) ->
assert.equal(err, null)

'returns an RRDInfo object': (err, data) ->
assert.instanceOf(data, RRDInfo)

'when restoring':
'a valid file':
topic: () ->
Expand Down
73 changes: 73 additions & 0 deletions test/valid.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
filename = "valid.rrd"
rrd_version = "0003"
step = 300
last_update = 1310750319
header_size = 2320
ds[temperature].index = 0
ds[temperature].type = "GAUGE"
ds[temperature].minimal_heartbeat = 600
ds[temperature].min = NaN
ds[temperature].max = NaN
ds[temperature].last_ds = "131"
ds[temperature].value = 2.8608000000e+04
ds[temperature].unknown_sec = 0
ds[target_temp].index = 1
ds[target_temp].type = "GAUGE"
ds[target_temp].minimal_heartbeat = 600
ds[target_temp].min = NaN
ds[target_temp].max = NaN
ds[target_temp].last_ds = "68"
ds[target_temp].value = 1.4892000000e+04
ds[target_temp].unknown_sec = 0
ds[state].index = 2
ds[state].type = "GAUGE"
ds[state].minimal_heartbeat = 600
ds[state].min = NaN
ds[state].max = NaN
ds[state].last_ds = "0"
ds[state].value = 0.0000000000e+00
ds[state].unknown_sec = 0
rra[0].cf = "AVERAGE"
rra[0].rows = 300
rra[0].cur_row = 182
rra[0].pdp_per_row = 1
rra[0].xff = 5.0000000000e-01
rra[0].cdp_prep[0].value = NaN
rra[0].cdp_prep[0].unknown_datapoints = 0
rra[0].cdp_prep[1].value = NaN
rra[0].cdp_prep[1].unknown_datapoints = 0
rra[0].cdp_prep[2].value = NaN
rra[0].cdp_prep[2].unknown_datapoints = 0
rra[1].cf = "AVERAGE"
rra[1].rows = 300
rra[1].cur_row = 38
rra[1].pdp_per_row = 7
rra[1].xff = 5.0000000000e-01
rra[1].cdp_prep[0].value = 6.3484000000e+02
rra[1].cdp_prep[0].unknown_datapoints = 0
rra[1].cdp_prep[1].value = 3.4000000000e+02
rra[1].cdp_prep[1].unknown_datapoints = 0
rra[1].cdp_prep[2].value = 0.0000000000e+00
rra[1].cdp_prep[2].unknown_datapoints = 0
rra[2].cf = "AVERAGE"
rra[2].rows = 300
rra[2].cur_row = 177
rra[2].pdp_per_row = 31
rra[2].xff = 5.0000000000e-01
rra[2].cdp_prep[0].value = 3.2700500000e+03
rra[2].cdp_prep[0].unknown_datapoints = 0
rra[2].cdp_prep[1].value = 1.8360000000e+03
rra[2].cdp_prep[1].unknown_datapoints = 0
rra[2].cdp_prep[2].value = 0.0000000000e+00
rra[2].cdp_prep[2].unknown_datapoints = 0
rra[3].cf = "AVERAGE"
rra[3].rows = 1500
rra[3].cur_row = 417
rra[3].pdp_per_row = 352
rra[3].xff = 5.0000000000e-01
rra[3].cdp_prep[0].value = 1.4339880000e+04
rra[3].cdp_prep[0].unknown_datapoints = 0
rra[3].cdp_prep[1].value = 9.7240000000e+03
rra[3].cdp_prep[1].unknown_datapoints = 0
rra[3].cdp_prep[2].value = 0.0000000000e+00
rra[3].cdp_prep[2].unknown_datapoints = 0