Class: Munin::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Initialize a new connection to munin-node server

host - Server host (default: 127.0.0.1) port - Server port (default: 4949)



12
13
14
15
16
17
18
# File 'lib/munin-ruby/connection.rb', line 12

def initialize(host='127.0.0.1', port=4949, reconnect=true)
  @host      = host
  @port      = port
  @socket    = nil
  @connected = false
  @reconnect = reconnect
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/munin-ruby/connection.rb', line 5

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/munin-ruby/connection.rb', line 5

def port
  @port
end

Instance Method Details

#close(reconnect = true) ⇒ Object

Close connection



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

def close(reconnect=true)
  if connected?
    @socket.flush
    @socket.shutdown
    @connected = false
    @reconnect = reconnect
  end
end

#connected?Boolean

Returns true if socket is connected

Returns:

  • (Boolean)


22
23
24
# File 'lib/munin-ruby/connection.rb', line 22

def connected?
  @connected == true
end

#openObject

Establish connection to the server



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/munin-ruby/connection.rb', line 28

def open
  begin
    begin
      with_timeout do
        @socket = TCPSocket.new(@host, @port)
        @socket.sync = true
        welcome = @socket.gets
        unless welcome =~ /^# munin node at/
          raise Munin::AccessDenied
        end
        @connected = true
      end
    rescue Timeout::Error
      raise Munin::ConnectionError, "Timed out talking to #{@host}"
    end
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET => ex
    raise Munin::ConnectionError, ex.message
  rescue EOFError
    raise Munin::AccessDenied
  rescue Exception => ex
    raise Munin::ConnectionError, ex.message
  end
end

#read_lineObject

Reads a single line from socket



83
84
85
86
87
88
89
90
91
# File 'lib/munin-ruby/connection.rb', line 83

def read_line
  begin
    with_timeout { @socket.gets.to_s.strip }
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError => ex
    raise Munin::ConnectionError, ex.message
  rescue Timeout::Error
    raise Munin::ConnectionError, "Timed out reading from #{@host}."
  end
end

#read_packetObject

Reads a packet of data until ‘.’ reached



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/munin-ruby/connection.rb', line 95

def read_packet
  begin
    with_timeout do
      lines = []
      while(str = @socket.readline.to_s) do
        break if str.strip == '.'
        lines << str.strip
      end
      parse_error(lines)
      lines
    end
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError => ex
    raise Munin::ConnectionError, ex.message
  rescue Timeout::Error
    raise Munin::ConnectionError, "Timed out reading from #{@host}."
  end
end

#send_data(str) ⇒ Object

Send a string of data followed by a newline symbol



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/munin-ruby/connection.rb', line 65

def send_data(str)
  if !connected?
    if !@socket.nil? && @reconnect == false
      raise Munin::ConnectionError, "Not connected."
    else
      open
    end
  end

  begin
    with_timeout { @socket.puts("#{str.strip}\n") }
  rescue Timeout::Error
    raise Munin::ConnectionError, "Timed out on #{@host} trying to send."
  end
end