hq-sms-global-balance-check 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd135a8ba3ea2cfef393062bdd82c055abffbfae
4
- data.tar.gz: 07f2c99c82e1c70944b0f8c68beaa17e0307e6de
3
+ metadata.gz: ac2884b718655062111084b5f841ce23c62bbfc0
4
+ data.tar.gz: 67f8bb1a7a52b4ec07ba2faeca8d327249815f80
5
5
  SHA512:
6
- metadata.gz: 9e174f85e98b9ec5c78302eae4945c364932632b5c7361f0463edc82ddc31398738bd69247333b73d763d1a3d075bf25d97b9c601a3691169b2a1aa448221760
7
- data.tar.gz: f200d1538c878e018bcb320e0e6213de614b50d83a4bda4aa909189eb1351c32846b4229e3a7808f9d8847db9ed62865c73a394661e8048fb61dfed1f15765a3
6
+ metadata.gz: 4c560444e4484a7bb35974f706b9a2f9bb0d7d9e05b60d593e40ed8ba2727c5c588c1f0ae774f7eb347982f0b6c4f35abf9f53edca3e1adbddcd51deac3ee639
7
+ data.tar.gz: 4ab8340f21425d5cb7eca5c68bc7cda601e5a32c7c09c2cc493439818c9212a8eba3cc6d4d24d159151d892691a0c69c13e5630a509b7ec76e8823a25dfe2a9f
@@ -16,6 +16,16 @@ Feature: Basic functionality
16
16
  --account account
17
17
  --warning 500
18
18
  --critical 100
19
+ --timeout 10
20
+ """
21
+
22
+ Given a file "timeout.args":
23
+ """
24
+ --config default.config
25
+ --account account
26
+ --warning 500
27
+ --critical 100
28
+ --timeout 0
19
29
  """
20
30
 
21
31
  Scenario: Balance ok
@@ -53,3 +63,15 @@ Feature: Basic functionality
53
63
  """
54
64
  SMS Global account CRITICAL: 50 credits (critical is 100)
55
65
  """
66
+
67
+ Scenario: Read timeout
68
+
69
+ Given the server is slow to respond
70
+
71
+ When I run the script with "timeout.args"
72
+
73
+ Then the status should be 3
74
+ And the output should be:
75
+ """
76
+ SMS Global account UNKNOWN: server timed out
77
+ """
@@ -21,6 +21,18 @@ Feature: Cached results
21
21
  --account account
22
22
  --warning 500
23
23
  --critical 100
24
+ --timeout 10
25
+ --cache-warning 10
26
+ --cache-critical 20
27
+ """
28
+
29
+ And a file "timeout.args":
30
+ """
31
+ --config default.config
32
+ --account account
33
+ --warning 500
34
+ --critical 100
35
+ --timeout 0
24
36
  --cache-warning 10
25
37
  --cache-critical 20
26
38
  """
@@ -41,7 +53,7 @@ Feature: Cached results
41
53
  Then the status should be 0
42
54
  And the output should be:
43
55
  """
44
- SMS Global account OK: 750 credits, last check 5 seconds ago
56
+ SMS Global account OK: server status 500, 750 credits, last check 5 seconds ago
45
57
  """
46
58
 
47
59
  Scenario: Cache warning
@@ -54,7 +66,7 @@ Feature: Cached results
54
66
  Then the status should be 1
55
67
  And the output should be:
56
68
  """
57
- SMS Global account WARNING: 750 credits, last check 15 seconds ago (warning is 10)
69
+ SMS Global account WARNING: server status 500, 750 credits, last check 15 seconds ago (warning is 10)
58
70
  """
59
71
 
60
72
  Scenario: Cache critical
@@ -67,5 +79,18 @@ Feature: Cached results
67
79
  Then the status should be 2
68
80
  And the output should be:
69
81
  """
70
- SMS Global account CRITICAL: 750 credits, last check 25 seconds ago (critical is 20)
82
+ SMS Global account CRITICAL: server status 500, 750 credits, last check 25 seconds ago (critical is 20)
83
+ """
84
+
85
+ Scenario: Cache warning with timeout
86
+
87
+ Given the time is 115
88
+ And the server is slow to respond
89
+
90
+ When I run the script with "timeout.args"
91
+
92
+ Then the status should be 1
93
+ And the output should be:
94
+ """
95
+ SMS Global account WARNING: server timed out, 750 credits, last check 15 seconds ago (warning is 10)
71
96
  """
@@ -15,6 +15,12 @@ Given /^the server is offline$/ do
15
15
 
16
16
  end
17
17
 
18
+ Given /^the server is slow to respond$/ do
19
+
20
+ $status = :slow
21
+
22
+ end
23
+
18
24
  When /^I run the script with "(.+)"$/ do
19
25
  |args_file|
20
26
 
@@ -37,3 +43,7 @@ Then /^the output should be:$/ do
37
43
  |expected_output|
38
44
  @script.stdout.string.strip.should == expected_output
39
45
  end
46
+
47
+ Then /^show stderr$/ do
48
+ puts @script.stderr.string
49
+ end
@@ -44,6 +44,10 @@ $web_server.mount_proc "/balance-api.php" do
44
44
 
45
45
  response.body = "Sorry, an error occurred"
46
46
 
47
+ when :slow
48
+
49
+ sleep 2
50
+
47
51
  end
48
52
 
49
53
  end
@@ -33,6 +33,10 @@ class Script < Tools::CheckScript
33
33
  :convert => :to_f,
34
34
  :required => true },
35
35
 
36
+ { :name => :timeout,
37
+ :convert => :to_f,
38
+ :required => true },
39
+
36
40
  { :name => :cache_warning,
37
41
  :convert => :to_i },
38
42
 
@@ -76,21 +80,16 @@ class Script < Tools::CheckScript
76
80
 
77
81
  def perform_checks
78
82
 
79
- used_cache = false
80
-
81
- begin
82
-
83
- get_balance_from_server
84
-
85
- rescue => exception
86
-
87
- raise exception \
88
- unless @cache_elem
83
+ get_balance_from_server
89
84
 
85
+ if @actual_balance
86
+ used_cache = false
87
+ elsif @cache_elem
90
88
  read_cache
91
-
89
+ @unknown = false
92
90
  used_cache = true
93
-
91
+ else
92
+ return
94
93
  end
95
94
 
96
95
  # report balance
@@ -160,22 +159,42 @@ class Script < Tools::CheckScript
160
159
  "password" => @account_elem["password"],
161
160
  })
162
161
 
163
- Net::HTTP.start url.host, url.port do
164
- |http|
162
+ http =
163
+ Net::HTTP.new url.host, url.port
164
+
165
+ http.open_timeout = @opts[:timeout]
166
+ http.read_timeout = @opts[:timeout]
167
+
168
+ http.start
169
+
170
+ begin
165
171
 
166
172
  request = Net::HTTP::Get.new "#{url.path}?#{url.query}"
167
173
 
168
174
  response = http.request request
169
175
 
170
- raise "Error 1" \
171
- unless response.code == "200"
176
+ unless response.code == "200"
177
+ unknown "server status #{response.code}"
178
+ return
179
+ end
172
180
 
173
- raise "Error 2" \
174
- unless response.body =~ /^BALANCE: ([^;]+); USER: (.+)$/
181
+ unless response.body =~ /^BALANCE: ([^;]+); USER: (.+)$/
182
+ unknown "invalid server response"
183
+ return
184
+ end
175
185
 
176
186
  @actual_balance =
177
187
  $1.to_f
178
188
 
189
+ rescue Net::ReadTimeout
190
+
191
+ unknown "server timed out"
192
+ return
193
+
194
+ ensure
195
+
196
+ http.finish
197
+
179
198
  end
180
199
 
181
200
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-sms-global-balance-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Pharaoh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-14 00:00:00.000000000 Z
11
+ date: 2013-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hq-tools
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.8.0
19
+ version: 0.8.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.8.0
26
+ version: 0.8.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: libxml-ruby
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 0.0.11
61
+ version: 0.0.18
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
- version: 0.0.11
68
+ version: 0.0.18
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: json
71
71
  requirement: !ruby/object:Gem::Requirement