Module: RRDm
Constant Summary collapse
- @@rrdtool_path =
(Dir.glob('/usr/local/rrdtool-*/bin/rrdtool').sort.reverse + ['/usr/local/bin/rrdtool', '/usr/bin/rrdtool']).find {|path| File.executable? path.untaint }
Instance Attribute Summary collapse
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #close ⇒ Object
- #cmd(*args) ⇒ Object
- #read(out = nil) ⇒ Object
- #running? ⇒ Boolean
- #start(rrdtool_path = nil) ⇒ Object
Instance Attribute Details
#version ⇒ Object (readonly)
Returns the value of attribute version.
13 14 15 |
# File 'lib/gri/rrd.rb', line 13 def version @version end |
Instance Method Details
#close ⇒ Object
73 74 75 76 77 78 |
# File 'lib/gri/rrd.rb', line 73 def close @sequence = nil @in.close @out.close @err.close end |
#cmd(*args) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/gri/rrd.rb', line 66 def cmd(*args) @sequence = 'C' cmd = args.join(' ') @in.print cmd, "\n" @last_cmd = cmd end |
#read(out = nil) ⇒ Object
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 |
# File 'lib/gri/rrd.rb', line 37 def read out=nil unless @sequence == 'C' raise RuntimeError, 'RRDm.read can only be called after RRDm.cmd' end @sequence == 'R' lines = [] errline = nil while line = @out.gets if line =~ /^ERROR/ errline = line.gsub(/\n/, "\\n") raise RuntimeError, errline elsif line =~ /^OK u:([\d\.]+) s:([\d\.]+) r:([\d\.]+)/ sys, user, real = $1.to_f, $2.to_f, $3.to_f if errline raise RuntimeError, errline else return [lines.join(''), sys, user, real] end end if out out.write line else lines.push line end end raise RuntimeError, 'unexpected EOF' end |
#running? ⇒ Boolean
33 34 35 |
# File 'lib/gri/rrd.rb', line 33 def running? @sequence end |
#start(rrdtool_path = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gri/rrd.rb', line 15 def start rrdtool_path=nil rrdtool_path ||= @@rrdtool_path raise RuntimeError, 'rrdtool is already running' if @sequence unless rrdtool_path and File.exist?(rrdtool_path) raise Exception, "rrdtool #{rrdtool_path} not found" end @sequence = 'S' @last_cmd = nil @in, @out, @err = Open3.popen3("#{rrdtool_path} -") @in.sync = true @out.sync = true cmd 'version' line, = read @version, = line.scan(/^RRDtool\s+(\S+)/)[0] end |