munin-ruby 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,3 @@
1
- require 'socket'
2
-
3
1
  module Munin
4
2
  class Connection
5
3
  include Munin::Parser
@@ -29,13 +27,19 @@ module Munin
29
27
  #
30
28
  def open
31
29
  begin
32
- @socket = TCPSocket.new(@host, @port)
33
- @socket.sync = true
34
- welcome = @socket.gets
35
- unless welcome =~ /^# munin node at/
36
- raise Munin::AccessDenied
30
+ begin
31
+ with_timeout do
32
+ @socket = TCPSocket.new(@host, @port)
33
+ @socket.sync = true
34
+ welcome = @socket.gets
35
+ unless welcome =~ /^# munin node at/
36
+ raise Munin::AccessDenied
37
+ end
38
+ @connected = true
39
+ end
40
+ rescue Timeout::Error
41
+ raise Munin::ConnectionError, "Timed out talking to #{@host}"
37
42
  end
38
- @connected = true
39
43
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET => ex
40
44
  raise Munin::ConnectionError, ex.message
41
45
  rescue EOFError
@@ -66,16 +70,23 @@ module Munin
66
70
  open
67
71
  end
68
72
  end
69
- @socket.puts("#{str.strip}\n")
73
+
74
+ begin
75
+ with_timeout { @socket.puts("#{str.strip}\n") }
76
+ rescue Timeout::Error
77
+ raise Munin::ConnectionError, "Timed out on #{@host} trying to send."
78
+ end
70
79
  end
71
80
 
72
81
  # Reads a single line from socket
73
82
  #
74
83
  def read_line
75
84
  begin
76
- @socket.gets.to_s.strip
85
+ with_timeout { @socket.gets.to_s.strip }
77
86
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError => ex
78
87
  raise Munin::ConnectionError, ex.message
88
+ rescue Timeout::Error
89
+ raise Munin::ConnectionError, "Timed out reading from #{@host}."
79
90
  end
80
91
  end
81
92
 
@@ -83,16 +94,33 @@ module Munin
83
94
  #
84
95
  def read_packet
85
96
  begin
86
- lines = []
87
- while(str = @socket.readline.to_s) do
88
- break if str.strip == '.'
89
- lines << str.strip
97
+ with_timeout do
98
+ lines = []
99
+ while(str = @socket.readline.to_s) do
100
+ break if str.strip == '.'
101
+ lines << str.strip
102
+ end
103
+ parse_error(lines)
104
+ lines
90
105
  end
91
- parse_error(lines)
92
- lines
93
106
  rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError => ex
94
107
  raise Munin::ConnectionError, ex.message
108
+ rescue Timeout::Error
109
+ raise Munin::ConnectionError, "Timed out reading from #{@host}."
110
+ end
111
+ end
112
+
113
+ private
114
+
115
+ # Execute operation with timeout
116
+ # @param [Block] block Block to execute
117
+ def with_timeout(time=Munin::TIMEOUT_TIME)
118
+ raise ArgumentError, "Block required" if !block_given?
119
+ if Munin::TIMEOUT_CLASS.respond_to?(:timeout_after)
120
+ Munin::TIMEOUT_CLASS.timeout_after(time) { yield }
121
+ else
122
+ Munin::TIMEOUT_CLASS.timeout(time) { yield }
95
123
  end
96
124
  end
97
125
  end
98
- end
126
+ end
@@ -1,5 +1,5 @@
1
1
  module Munin
2
2
  unless defined?(::Munin::VERSION)
3
- VERSION = '0.2.2'
3
+ VERSION = '0.2.3'
4
4
  end
5
5
  end
data/lib/munin-ruby.rb CHANGED
@@ -1,6 +1,25 @@
1
+ require 'rubygems'
2
+ require 'socket'
3
+
4
+ begin
5
+ require 'system_timer'
6
+ rescue LoadError
7
+ require 'timeout'
8
+ end
9
+
1
10
  require 'munin-ruby/version'
2
11
  require 'munin-ruby/errors'
3
12
  require 'munin-ruby/parser'
4
13
  require 'munin-ruby/cache'
5
14
  require 'munin-ruby/connection'
6
- require 'munin-ruby/node'
15
+ require 'munin-ruby/node'
16
+
17
+ module Munin
18
+ TIMEOUT_TIME = 10
19
+
20
+ if defined?(SystemTimer)
21
+ TIMEOUT_CLASS = SystemTimer
22
+ else
23
+ TIMEOUT_CLASS = Timeout
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: munin-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-15 00:00:00.000000000Z
12
+ date: 2012-07-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2165744280 !ruby/object:Gem::Requirement
16
+ requirement: &2152614540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.6'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2165744280
24
+ version_requirements: *2152614540
25
25
  description: Munin Node client
26
26
  email: dan.sosedoff@gmail.com
27
27
  executables: []