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.



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

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)



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

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.



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

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


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

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'


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

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

.runObject

Create and executes the plugin



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

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:



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

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

Instance Method Details

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



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

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

Prints plugin configuration by using the munin format



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

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



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

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

#runObject

Executes the plugin



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

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