Class: Munin::Plugin
- Inherits:
-
Object
- Object
- Munin::Plugin
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
-
.declare_field(name, options = {}) ⇒ Object
Declare a data source / field of the plugin.
-
.graph_attributes(title, options = {}) ⇒ Object
Sets the graph attributes of the plugin.
-
.run ⇒ Object
Create and executes the plugin.
-
.with_options(options) {|OptionMerger.new(self, options)| ... } ⇒ Object
An elegant way to share common options.
Instance Method Summary collapse
- #declare_field(name, options = {}) ⇒ Object
-
#initialize(config = {}) ⇒ Plugin
constructor
A new instance of Plugin.
-
#print_config ⇒ Object
Prints plugin configuration by using the munin format.
-
#print_values ⇒ Object
Prints plugin values by using the munin format.
-
#run ⇒ Object
Executes the plugin.
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
#fields ⇒ Object (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, = {}) @@fields << Field.new(name, ) 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, = {}) @@graph_options = { :title => title, :args => '--base 1000' }.merge() end |
.run ⇒ Object
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
72 73 74 |
# File 'lib/munin.rb', line 72 def () yield OptionMerger.new(self, ) end |
Instance Method Details
#declare_field(name, options = {}) ⇒ Object
128 129 130 |
# File 'lib/munin.rb', line 128 def declare_field(name, = {}) @fields << Field.new(name, ) end |
#print_config ⇒ Object
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 |
#print_values ⇒ Object
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 |
#run ⇒ Object
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 |