Class: Errand
- Inherits:
-
Object
- Object
- Errand
- Defined in:
- lib/errand.rb,
lib/errand/version.rb
Overview
Errand: Wraps the RRD Ruby library provided by your distribution’s package manager, exposing a more Ruby-like and accessible interface.
Constant Summary collapse
- VERSION =
"0.8.1"
Instance Method Summary collapse
- #create(opts = {}) ⇒ Object
-
#data_sources ⇒ Object
ordered array of data sources as defined in rrd.
- #dump(opts = {}) ⇒ Object
- #fetch(opts = {}) ⇒ Object
- #first ⇒ Object
- #info ⇒ Object
-
#initialize(opts = {}) ⇒ Errand
constructor
A new instance of Errand.
- #last ⇒ Object
- #update(opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Errand
Returns a new instance of Errand.
11 12 13 14 15 |
# File 'lib/errand.rb', line 11 def initialize(opts={}) raise ArgumentError unless opts[:filename] @filename = opts[:filename] @backend = ::RRD::Wrapper end |
Instance Method Details
#create(opts = {}) ⇒ Object
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 83 84 85 86 87 88 89 |
# File 'lib/errand.rb', line 54 def create(opts={}) step = (opts[:step] || 300).to_s start = (opts[:start] || Time.now.to_i - 10).to_s = ["--step", step, "--start", start] sources = opts[:sources].map { |source| name = source[:name] type = source[:type].to_s.upcase heartbeat = source[:heartbeat] min = source[:min] max = source[:max] ds = ["DS", name, type] ds += [heartbeat, min, max] if heartbeat && min && max ds.join(':') } archives = opts[:archives].map { |archive| function = archive[:function].to_s.upcase xff = archive[:xff] steps = archive[:steps] rows = archive[:rows] rra = ["RRA", function] rra += [xff, steps, rows] if xff && steps && rows rra.join(':') } args = + sources + archives @backend.create(@filename, *args) # * "flattens" the array true end |
#data_sources ⇒ Object
ordered array of data sources as defined in rrd
140 141 142 |
# File 'lib/errand.rb', line 140 def data_sources self.info.keys.grep(/^ds\[/).map { |ds| ds[3..-1].split(']').first}.uniq end |
#dump(opts = {}) ⇒ Object
17 18 19 20 |
# File 'lib/errand.rb', line 17 def dump(opts={}) output = opts[:filename] || "" @backend.dump(@filename, output) end |
#fetch(opts = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/errand.rb', line 30 def fetch(opts={}) start = (opts[:start] || Time.now.to_i - 3600).to_s finish = (opts[:finish] || Time.now.to_i).to_s function = opts[:function] ? opts[:function].to_s.upcase : "AVERAGE" args = [ @filename, function, "--start", start, "--end", finish ] data = @backend.fetch(*args) labels = data.shift points = {} labels.each_with_index do |label, index| points[label] = [] data.each do |tuple| value = tuple[index] points[label] << value end end points.delete('time') {:start => start, :finish => finish, :data => points} end |
#first ⇒ Object
22 23 24 |
# File 'lib/errand.rb', line 22 def first @backend.first(@filename) end |
#info ⇒ Object
135 136 137 |
# File 'lib/errand.rb', line 135 def info @backend.info(@filename) end |
#last ⇒ Object
26 27 28 |
# File 'lib/errand.rb', line 26 def last @backend.last(@filename) end |
#update(opts = {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/errand.rb', line 91 def update(opts={}) time_specified = opts[:sources].find { |source| source[:time] } times = opts[:sources].map {|s| s[:time]}.sort.uniq if time_specified sources = opts[:sources].map { |source| source[:name] }.join(':') case when time_specified && times.size == 1 values = "#{times.first}:" + opts[:sources].map { |s| s[:value] }.join(':') args = ["--template", sources, values] @backend.update(@filename, *args) when time_specified && times.size > 1 times.each do |t| points = opts[:sources].find_all { |source| source[:time] == t } sources = points.map { |p| p[:name] }.join(':') values = "#{t}:" + points.map { |p| p[:value] }.join(':') args = ["--template", sources, values] @backend.update(@filename, *args) end when !time_specified values = "N:" + opts[:sources].map { |source| source[:value] }.join(':') args = ["--template", sources, values] @backend.update(@filename, *args) end true end |