Class: Puppet::Util::Metric

Inherits:
Object show all
Includes:
Network::FormatSupport
Defined in:
lib/puppet/util/metric.rb

Overview

A class for handling metrics. This is currently ridiculously hackish.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Network::FormatSupport

included, #mime, #render, #support_format?, #to_msgpack

Constructor Details

#initialize(name, label = nil) ⇒ Metric

Returns a new instance of Metric.



146
147
148
149
150
151
152
# File 'lib/puppet/util/metric.rb', line 146

def initialize(name,label = nil)
  @name = name.to_s

  @label = label || self.class.labelize(name)

  @values = []
end

Instance Attribute Details

#basedirObject



46
47
48
49
50
51
52
# File 'lib/puppet/util/metric.rb', line 46

def basedir
  if defined?(@basedir)
    @basedir
  else
    Puppet[:rrddir]
  end
end

#labelObject



9
10
11
# File 'lib/puppet/util/metric.rb', line 9

def label
  @label
end

#nameObject



9
10
11
# File 'lib/puppet/util/metric.rb', line 9

def name
  @name
end

#typeObject



9
10
11
# File 'lib/puppet/util/metric.rb', line 9

def type
  @type
end

#valueObject



9
10
11
# File 'lib/puppet/util/metric.rb', line 9

def value
  @value
end

#valuesObject



197
198
199
# File 'lib/puppet/util/metric.rb', line 197

def values
  @values.sort { |a, b| a[1] <=> b[1] }
end

Class Method Details

.from_data_hash(data) ⇒ Object



14
15
16
17
18
# File 'lib/puppet/util/metric.rb', line 14

def self.from_data_hash(data)
  metric = new(data['name'], data['label'])
  metric.values = data['values']
  metric
end

.from_pson(data) ⇒ Object



20
21
22
23
# File 'lib/puppet/util/metric.rb', line 20

def self.from_pson(data)
  Puppet.deprecation_warning("from_pson is being removed in favour of from_data_hash.")
  self.from_data_hash(data)
end

.labelize(name) ⇒ Object

Convert a name into a label.



202
203
204
# File 'lib/puppet/util/metric.rb', line 202

def self.labelize(name)
  name.to_s.capitalize.gsub("_", " ")
end

Instance Method Details

#[](name) ⇒ Object

Return a specific value



38
39
40
41
42
43
44
# File 'lib/puppet/util/metric.rb', line 38

def [](name)
  if value = @values.find { |v| v[0] == name }
    return value[2]
  else
    return 0
  end
end

#create(start = nil) ⇒ 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
# File 'lib/puppet/util/metric.rb', line 54

def create(start = nil)
  Puppet.settings.use(:main, :metrics)

  start ||= Time.now.to_i - 5

  args = []

  if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
    @rrd = RRDtool.new(self.path)
  end

  values.each { |value|
    # the 7200 is the heartbeat -- this means that any data that isn't
    # more frequently than every two hours gets thrown away
    args.push "DS:#{value[0]}:GAUGE:7200:U:U"
  }
  args.push "RRA:AVERAGE:0.5:1:300"

  begin
    if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
      @rrd.create( Puppet[:rrdinterval], start, args)
    else
      RRD.create( self.path, '-s', Puppet[:rrdinterval].to_s, '-b', start.to_i.to_s, *args)
    end
  rescue => detail
    raise detail, "Could not create RRD file #{path}: #{detail}", detail.backtrace
  end
end

#dumpObject



83
84
85
86
87
88
89
# File 'lib/puppet/util/metric.rb', line 83

def dump
  if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
    puts @rrd.info
  else
    puts RRD.info(self.path)
  end
end

#graph(range = nil) ⇒ 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
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/puppet/util/metric.rb', line 91

def graph(range = nil)
  unless Puppet.features.rrd? || Puppet.features.rrd_legacy?
    Puppet.warning "RRD library is missing; cannot graph metrics"
    return
  end

  unit = 60 * 60 * 24
  colorstack = %w{#00ff00 #ff0000 #0000ff #ffff00 #ff99ff #ff9966 #66ffff #990000 #099000 #000990 #f00990 #0f0f0f #555555 #333333 #ffffff}

  {:daily => unit, :weekly => unit * 7, :monthly => unit * 30, :yearly => unit * 365}.each do |name, time|
    file = self.path.sub(/\.rrd$/, "-#{name}.png")
    args = [file]

    args.push("--title",self.label)
    args.push("--imgformat","PNG")
    args.push("--interlace")
    defs = []
    lines = []
    #p @values.collect { |s,l| s }
    values.zip(colorstack).each { |value,color|
      next if value.nil?
      # this actually uses the data label
      defs.push("DEF:#{value[0]}=#{self.path}:#{value[0]}:AVERAGE")
      lines.push("LINE2:#{value[0]}#{color}:#{value[1]}")
    }
    args << defs
    args << lines
    args.flatten!
    if range
      if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
        args.push("--start",range[0],"--end",range[1])
      else
        args.push("--start",range[0].to_i.to_s,"--end",range[1].to_i.to_s)
      end
    else
      if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
        args.push("--start", Time.now.to_i - time, "--end", Time.now.to_i)
      else
        args.push("--start", (Time.now.to_i - time).to_s, "--end", Time.now.to_i.to_s)
      end
    end

    begin
      #Puppet.warning "args = #{args}"
      if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
        RRDtool.graph( args )
      else
        RRD.graph( *args )
      end
    rescue => detail
      Puppet.err "Failed to graph #{self.name}: #{detail}"
    end
  end
end

#newvalue(name, value, label = nil) ⇒ Object

Raises:

  • (ArgumentError)


158
159
160
161
162
# File 'lib/puppet/util/metric.rb', line 158

def newvalue(name,value,label = nil)
  raise ArgumentError.new("metric name #{name.inspect} is not a string") unless name.is_a? String
  label ||= self.class.labelize(name)
  @values.push [name,label,value]
end

#pathObject



154
155
156
# File 'lib/puppet/util/metric.rb', line 154

def path
  File.join(self.basedir, @name + ".rrd")
end

#store(time) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/puppet/util/metric.rb', line 164

def store(time)
  unless Puppet.features.rrd? || Puppet.features.rrd_legacy?
    Puppet.warning "RRD library is missing; cannot store metrics"
    return
  end
  self.create(time - 5) unless Puppet::FileSystem.exist?(self.path)

  if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
    @rrd ||= RRDtool.new(self.path)
  end

  # XXX this is not terribly error-resistant
  args = [time]
  temps = []
  values.each { |value|
    #Puppet.warning "value[0]: #{value[0]}; value[1]: #{value[1]}; value[2]: #{value[2]}; "
    args.push value[2]
    temps.push value[0]
  }
  arg = args.join(":")
  template = temps.join(":")
  begin
    if Puppet.features.rrd_legacy? && ! Puppet.features.rrd?
      @rrd.update( template, [ arg ] )
    else
      RRD.update( self.path, '-t', template, arg )
    end
    #system("rrdtool updatev #{self.path} '#{arg}'")
  rescue => detail
    raise Puppet::Error, "Failed to update #{self.name}: #{detail}", detail.backtrace
  end
end

#to_data_hashObject



25
26
27
28
29
30
31
# File 'lib/puppet/util/metric.rb', line 25

def to_data_hash
  {
    'name' => @name,
    'label' => @label,
    'values' => @values
  }
end

#to_pson(*args) ⇒ Object



33
34
35
# File 'lib/puppet/util/metric.rb', line 33

def to_pson(*args)
  to_data_hash.to_pson(*args)
end