Class: Munin::Connection

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

Constant Summary collapse

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

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, options = {}) ⇒ Connection

Initialize a new connection to munin-node server

host - Server host (default: 127.0.0.1) port - Server port (default: 4949) options - A hash containing different options

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

16
17
18
19
20
21
22
23
# File 'lib/munin-ruby/connection.rb', line 16

def initialize(host='127.0.0.1', port=4949, reconnect=true, options = {})
  @host      = host
  @port      = port
  @socket    = nil
  @connected = false
  @reconnect = reconnect
  @options    = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host


7
8
9
# File 'lib/munin-ruby/connection.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port


7
8
9
# File 'lib/munin-ruby/connection.rb', line 7

def port
  @port
end

Instance Method Details

#close(reconnect = true) ⇒ Object

Close connection


59
60
61
62
63
64
65
66
# File 'lib/munin-ruby/connection.rb', line 59

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)

27
28
29
# File 'lib/munin-ruby/connection.rb', line 27

def connected?
  @connected == true
end

#openObject

Establish connection to the server


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/munin-ruby/connection.rb', line 33

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


88
89
90
91
92
93
94
95
96
# File 'lib/munin-ruby/connection.rb', line 88

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


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/munin-ruby/connection.rb', line 100

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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/munin-ruby/connection.rb', line 70

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