Class: Visage::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/visage-app/profile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Profile

Returns a new instance of Profile.



42
43
44
45
46
47
48
49
# File 'lib/visage-app/profile.rb', line 42

def initialize(opts={})
  @options = opts
  @options[:url] = @options[:profile_name] ? @options[:profile_name].downcase.gsub(/[^\w]+/, "+") : nil
  @errors = {}
  @options[:hosts]       = @options[:hosts].values       if @options[:hosts].class       == Hash
  @options[:metrics]     = @options[:metrics].values     if @options[:metrics].class     == Hash
  @options[:percentiles] = @options[:percentiles].values if @options[:percentiles].class == Hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Hashed based access to @options.



52
53
54
# File 'lib/visage-app/profile.rb', line 52

def method_missing(method)
  @options[method]
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def errors
  @errors
end

#hostsObject (readonly)

Returns the value of attribute hosts.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def hosts
  @hosts
end

#metricsObject (readonly)

Returns the value of attribute metrics.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def metrics
  @metrics
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def options
  @options
end

#percentilesObject (readonly)

Returns the value of attribute percentiles.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def percentiles
  @percentiles
end

#selected_hostsObject (readonly)

Returns the value of attribute selected_hosts.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def selected_hosts
  @selected_hosts
end

#selected_metricsObject (readonly)

Returns the value of attribute selected_metrics.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def selected_metrics
  @selected_metrics
end

#selected_percentilesObject (readonly)

Returns the value of attribute selected_percentiles.



11
12
13
# File 'lib/visage-app/profile.rb', line 11

def selected_percentiles
  @selected_percentiles
end

Class Method Details

.all(opts = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/visage-app/profile.rb', line 34

def self.all(opts={})
  sort = opts[:sort]
  profiles = self.load
  profiles = ((sort == "name") or not sort) ? profiles.sort_by {|k,v| v[:profile_name]}.map {|i| i.last } : profiles.values
  # FIXME - to sort by creation time we need to save creation time on each profile
  profiles.map { |prof| self.new(prof) }
end

.get(id) ⇒ Object



28
29
30
31
32
# File 'lib/visage-app/profile.rb', line 28

def self.get(id)
  url = id.downcase.gsub(/[^\w]+/, "+")
  profiles = self.load
  profiles[url] ? self.new(profiles[url]) : nil
end

.loadObject



24
25
26
# File 'lib/visage-app/profile.rb', line 24

def self.load
  Visage::Config::File.load('profiles.yaml', :create => true, :ignore_bundled => true) || {}
end

.old_format?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
# File 'lib/visage-app/profile.rb', line 15

def self.old_format?
  profiles = Visage::Config::File.load('profiles.yaml', :create => true, :ignore_bundled => true) || {}
  profiles.each_pair do |name, attrs|
    return true if attrs[:hosts] =~ /\*/ || attrs[:metrics] =~ /\*/
  end

  false
end

Instance Method Details

#graphsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/visage-app/profile.rb', line 84

def graphs
  graphs      = []
  hosts       = @options[:hosts]
  metrics     = @options[:metrics]
  percentiles = @options[:percentiles]

  hosts.each do |host|
    attrs = {}
    globs = Visage::Collectd::RRDs.metrics(:host => host, :metrics => metrics)
    globs.each do |n|
      parts    = n.split('/')
      plugin   = parts[0]
      instance = parts[1]
      attrs[plugin] ||= []
      attrs[plugin] << instance
    end

    attrs.each_pair do |plugin, instances|
      graphs << Visage::Graph.new(:host => host,
                                  :plugin => plugin,
                                  :instances => instances,
                                  :percentiles => percentiles)
    end
  end

  graphs
end

#private_idObject



112
113
114
# File 'lib/visage-app/profile.rb', line 112

def private_id
  Digest::MD5.hexdigest("#{@options[:url]}\n")
end

#saveObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/visage-app/profile.rb', line 56

def save
  if valid?
    # Construct record.
    attrs = { :hosts        => @options[:hosts],
              :metrics      => @options[:metrics],
              :percentiles  => @options[:percentiles],
              :profile_name => @options[:profile_name],
              :url          => @options[:profile_name].downcase.gsub(/[^\w]+/, "+") }

    # Save it.
    profiles = self.class.load
    profiles[attrs[:url]] = attrs

    Visage::Config::File.open('profiles.yaml') do |file|
      file.truncate(0)
      file << profiles.to_yaml
    end

    true
  else
    false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/visage-app/profile.rb', line 80

def valid?
  valid_profile_name?
end