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.
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
#fields ⇒ Object (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, = {}) @@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'
41 42 43 |
# File 'lib/munin.rb', line 41 def graph_attributes(title, = {}) @@graph_options = { :title => title, :args => '--base 1000' }.merge() end |
.run ⇒ Object
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
71 72 73 |
# File 'lib/munin.rb', line 71 def () yield OptionMerger.new(self, ) end |
Instance Method Details
#declare_field(name, options = {}) ⇒ Object
127 128 129 |
# File 'lib/munin.rb', line 127 def declare_field(name, = {}) @fields << Field.new(name, ) end |
#print_config ⇒ Object
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 |
#print_values ⇒ Object
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 |
#run ⇒ Object
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 |