Class: Munin::Node

Inherits:
Object
  • Object
show all
Includes:
Cache, Parser
Defined in:
lib/munin-ruby/node.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{:timeout => Munin::TIMEOUT_TIME}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Cache

#cache, #flush_cache

Methods included from Parser

#parse_config, #parse_config_args, #parse_error, #parse_fetch, #parse_version, #process_data

Constructor Details

#initialize(host = '127.0.0.1', port = 4949, reconnect = true, options = {}) ⇒ Node

Initialize a new Node instance

host - Server host port - Server port reconnect - Reconnect if connection was closed (default: true) options - A hash containing different options

:timeout => A timeout in seconds to be used in the connection


19
20
21
22
# File 'lib/munin-ruby/node.rb', line 19

def initialize(host='127.0.0.1', port=4949, reconnect=true, options = {})
  @options    = DEFAULT_OPTIONS.merge(options)
  @connection = Munin::Connection.new(host, port, reconnect, @options)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/munin-ruby/node.rb', line 8

def connection
  @connection
end

Instance Method Details

#config(services, raw = false) ⇒ Object

Get a configuration information for service

services - Name of the service, or list of service names



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/munin-ruby/node.rb', line 75

def config(services, raw=false)
  unless [String, Array].include?(services.class)
    raise ArgumentError, "Service(s) argument required"
  end
  
  results       = {}
  names         = [services].flatten.uniq
  
  if names.empty?
    raise ArgumentError, "Service(s) argument required"
  end
  
  key = 'config_' + Digest::MD5.hexdigest(names.to_s) + "_#{raw}"
  
  cache(key) do
    names.each do |service|
      begin
        connection.send_data("config #{service}")
        lines = connection.read_packet 
        results[service] = raw ? lines.join("\n") : parse_config(lines)
      rescue UnknownService, BadExit
        # TODO
      end
    end
    results
  end
end

#connectObject

Open service connection



26
27
28
# File 'lib/munin-ruby/node.rb', line 26

def connect
  connection.open
end

#disconnect(reconnect = true) ⇒ Object

Close server connection



32
33
34
# File 'lib/munin-ruby/node.rb', line 32

def disconnect(reconnect=true)
  connection.close(reconnect)
end

#fetch(services) ⇒ Object

Get all service metrics values

services - Name of the service, or list of service names



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/munin-ruby/node.rb', line 107

def fetch(services)
  unless [String, Array].include?(services.class)
    raise ArgumentError, "Service(s) argument required"
  end
  
  results = {}
  names   = [services].flatten
  
  if names.empty?
    raise ArgumentError, "Service(s) argument required"
  end
  
  names.each do |service|
    begin
      connection.send_data("fetch #{service}")
      lines = connection.read_packet
      results[service] =  parse_fetch(lines)
    rescue UnknownService, BadExit
      # TODO
    end
  end
  results
end

#list(node = "") ⇒ Object

Get a list of all available metrics



60
61
62
63
64
65
66
67
68
69
# File 'lib/munin-ruby/node.rb', line 60

def list(node = "")
  cache "list_#{node.empty? ? 'default' : node}" do
    connection.send_data("list #{node}")
    if ( line = connection.read_line ) != "."
      line.split
    else
      connection.read_line.split
    end
  end
end

#nodesObject

Get a list of all available nodes



47
48
49
50
51
52
53
54
55
56
# File 'lib/munin-ruby/node.rb', line 47

def nodes
  nodes = []
  cache 'nodes' do
    connection.send_data("nodes")
    while ( ( line = connection.read_line ) != "." )
      nodes << line
    end
    nodes
  end
end

#versionObject

Get a node version



38
39
40
41
42
43
# File 'lib/munin-ruby/node.rb', line 38

def version
  cache 'version' do
    connection.send_data("version")
    parse_version(connection.read_line)
  end
end