Class: Solanum

Inherits:
Object
  • Object
show all
Defined in:
lib/solanum.rb

Overview

Class which wraps up an active Solanum monitoring system into an object.

Author

Greg Look

Defined Under Namespace

Classes: Config, Matcher, Source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scripts) ⇒ Solanum

Loads the given monitoring scripts and initializes the sources and service definitions.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/solanum.rb', line 13

def initialize(scripts)
  @sources = []
  @services = []
  @metrics = {}

  scripts.each do |path|
    begin
      config = Solanum::Config.new(path)
      @sources.concat(config.sources)
      @services.concat(config.services)
    rescue => e
      STDERR.puts "Error loading monitor script #{path}: #{e}"
    end
  end

  @sources.freeze
  @services.freeze
end

Instance Attribute Details

#metricsObject (readonly)

Returns the value of attribute metrics.



5
6
7
# File 'lib/solanum.rb', line 5

def metrics
  @metrics
end

#servicesObject (readonly)

Returns the value of attribute services.



5
6
7
# File 'lib/solanum.rb', line 5

def services
  @services
end

#sourcesObject (readonly)

Returns the value of attribute sources.



5
6
7
# File 'lib/solanum.rb', line 5

def sources
  @sources
end

Instance Method Details

#build_events(defaults = {}) ⇒ Object

Builds full events from a set of service prototypes, old metrics, and new metrics.



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
# File 'lib/solanum.rb', line 51

def build_events(defaults={})
  @metrics.keys.sort.map do |service|
    value = @metrics[service]
    prototype = @services.select{|m| m[0] === service }.map{|m| m[1] }.reduce({}, &:merge)

    state = prototype[:state] ? prototype[:state].call(value) : :ok
    tags = ((prototype[:tags] || []) + (defaults[:tags] || [])).uniq
    ttl = prototype[:ttl] || defaults[:ttl]

    if prototype[:diff]
      last = @old_metrics[service]
      if last && last <= value
        value = value - last
      else
        value = nil
      end
    end

    if value
      defaults.merge({
        service: service,
        metric: value,
        state: state.to_s,
        tags: tags,
        ttl: ttl
      })
    end
  end.compact
end

#collect!Object

Collects metrics from the given sources, in order. Updates the internal merged map of metric data.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/solanum.rb', line 35

def collect!
  @old_metrics = @metrics
  @metrics = @sources.reduce({}) do |metrics, source|
    begin
      new_metrics = source.collect(metrics) || {}
      metrics.merge(new_metrics)
    rescue => e
      STDERR.puts "Error collecting metrics from #{source}: #{e}"
      metrics
    end
  end
end