Class: Munin::Node

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

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) ⇒ Node

Initialize a new Node instance

host - Server host port - Server port reconnect - Reconnect if connection was closed (default: true)



17
18
19
# File 'lib/munin-ruby/node.rb', line 17

def initialize(host='127.0.0.1', port=4949, reconnect=true)
  @connection = Munin::Connection.new(host, port, reconnect)
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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/munin-ruby/node.rb', line 68

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



23
24
25
# File 'lib/munin-ruby/node.rb', line 23

def connect
  connection.open
end

#disconnect(reconnect = true) ⇒ Object

Close server connection



29
30
31
# File 'lib/munin-ruby/node.rb', line 29

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/munin-ruby/node.rb', line 100

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



53
54
55
56
57
58
59
60
61
62
# File 'lib/munin-ruby/node.rb', line 53

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



44
45
46
47
48
49
# File 'lib/munin-ruby/node.rb', line 44

def nodes
  cache 'nodes' do 
    connection.send_data("nodes")
    connection.read_line.split
  end
end

#versionObject

Get a node version



35
36
37
38
39
40
# File 'lib/munin-ruby/node.rb', line 35

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