Class: Munin::Plugin

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

Overview

Base class to create Munin plugin

class ActiveRecordSessionPlugin << Munin::Plugin
  graph_attributes "Rails Sessions", 
    :category => 'Application',
    :info => 'This graph shows the rails application session count'

  declare_field :count, :label => 'session count', :min => 0

  def retrieve_values
    count = ...
    { :count => count }
  end
end

ActiveRecordSessionPlugin.run

Constant Summary collapse

@@fields =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Plugin

Returns a new instance of Plugin.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/munin.rb', line 80

def initialize(config = {})
  @config = config.symbolize_keys

  if self.class.respond_to?(:config_from_filename)
    @config.merge!(self.class.config_from_filename.symbolize_keys)
  end

  @fields = @@fields.dup

  after_initialize if respond_to?(:after_initialize)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



148
149
150
151
152
153
154
155
156
# File 'lib/munin.rb', line 148

def method_missing(method, *arguments, &block)
  case method.id2name
    when /^graph_([_a-z]+)$/
      @@graph_options[$1.to_sym]
    when /^graph_([_a-z]+)=$/
      @@graph_options[$1.to_sym] = arguments
    else super
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



78
79
80
# File 'lib/munin.rb', line 78

def fields
  @fields
end

Class Method Details

.declare_field(name, options = {}) ⇒ Object

Declare a data source / field of the plugin

Attributes

  • name: - The field name

  • options: - The field attributes

Examples

# Set a derive field
  declare_field :volume, :label => 'throughput', :type => :derive, :min => 0


56
57
58
# File 'lib/munin.rb', line 56

def declare_field(name, options = {})
  @@fields << Field.new(name, options)
end

.graph_attributes(title, options = {}) ⇒ Object

Sets the graph attributes of the plugin

Attributes

  • title: - The title of the graph, defaults to the plugin’s name (munin global attribute graph_title)

  • options: - Other graph attributes for the plugin (munin global attribute graph_*)

Examples

# Set classic graph attributes
graph_attributes "Fetchmail bytes throughput", 
  :category => 'Mail',
  :info => 'This graph shows the volume of mails retrieved by fetchmail'


42
43
44
# File 'lib/munin.rb', line 42

def graph_attributes(title, options = {})
  @@graph_options = { :title => title, :args => '--base 1000' }.merge(options)
end

.runObject

Create and executes the plugin



114
115
116
# File 'lib/munin.rb', line 114

def self.run
  self.new.run
end

.with_options(options) {|OptionMerger.new(self, options)| ... } ⇒ Object

An elegant way to share common options

Attributes

  • options: - The common options

Examples

# Share field attributes
with :type => :derive, :min => 0 do
  declare_field :input, :label => "download"
  declare_field :output, :label => "upload"
end

Yields:



72
73
74
# File 'lib/munin.rb', line 72

def with_options(options)
  yield OptionMerger.new(self, options)
end

Instance Method Details

#declare_field(name, options = {}) ⇒ Object



128
129
130
# File 'lib/munin.rb', line 128

def declare_field(name, options = {})
  @fields << Field.new(name, options)
end

Prints plugin configuration by using the munin format



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/munin.rb', line 93

def print_config
  output 'host_name', hostname unless hostname.nil?

  GRAPH_ATTRIBUTES.each do |graph_attribute|
    graph_option = @@graph_options[graph_attribute.to_sym]
    output "graph_#{graph_attribute}", graph_option unless graph_option.nil?
  end

  fields.each do |field|
    field.config.each_pair { |key, value| output key, value }
  end
end

Prints plugin values by using the munin format



107
108
109
110
111
# File 'lib/munin.rb', line 107

def print_values
  retrieve_values.each_pair do |name, value|
    output "#{name}.value", value
  end
end

#runObject

Executes the plugin



119
120
121
122
123
124
125
126
# File 'lib/munin.rb', line 119

def run
  case ARGV.first
    when "config"
      print_config
    else
      print_values
  end
end