hq-sms-global-balance-check 0.0.1 → 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd135a8ba3ea2cfef393062bdd82c055abffbfae
4
+ data.tar.gz: 07f2c99c82e1c70944b0f8c68beaa17e0307e6de
5
+ SHA512:
6
+ metadata.gz: 9e174f85e98b9ec5c78302eae4945c364932632b5c7361f0463edc82ddc31398738bd69247333b73d763d1a3d075bf25d97b9c601a3691169b2a1aa448221760
7
+ data.tar.gz: f200d1538c878e018bcb320e0e6213de614b50d83a4bda4aa909189eb1351c32846b4229e3a7808f9d8847db9ed62865c73a394661e8048fb61dfed1f15765a3
@@ -10,11 +10,7 @@ Feature: Basic functionality
10
10
  </hq-sms-global-balance-check-config>
11
11
  """
12
12
 
13
- Scenario: Balance ok
14
-
15
- Given that the balance is 750
16
-
17
- When I run the script with args:
13
+ Given a file "default.args":
18
14
  """
19
15
  --config default.config
20
16
  --account account
@@ -22,6 +18,12 @@ Feature: Basic functionality
22
18
  --critical 100
23
19
  """
24
20
 
21
+ Scenario: Balance ok
22
+
23
+ Given the balance is 750
24
+
25
+ When I run the script with "default.args"
26
+
25
27
  Then the status should be 0
26
28
  And the output should be:
27
29
  """
@@ -30,15 +32,9 @@ Feature: Basic functionality
30
32
 
31
33
  Scenario: Balance warning
32
34
 
33
- Given that the balance is 250
35
+ Given the balance is 250
34
36
 
35
- When I run the script with args:
36
- """
37
- --config default.config
38
- --account account
39
- --warning 500
40
- --critical 100
41
- """
37
+ When I run the script with "default.args"
42
38
 
43
39
  Then the status should be 1
44
40
  And the output should be:
@@ -48,15 +44,9 @@ Feature: Basic functionality
48
44
 
49
45
  Scenario: Balance critical
50
46
 
51
- Given that the balance is 50
47
+ Given the balance is 50
52
48
 
53
- When I run the script with args:
54
- """
55
- --config default.config
56
- --account account
57
- --warning 500
58
- --critical 100
59
- """
49
+ When I run the script with "default.args"
60
50
 
61
51
  Then the status should be 2
62
52
  And the output should be:
@@ -0,0 +1,71 @@
1
+ Feature: Cached results
2
+
3
+ If the service does not respond for some reason a cached result can be used
4
+ instead. Alerts will be generated if the time since the last check exceeds a
5
+ specified amount.
6
+
7
+ Background:
8
+
9
+ Given a file "default.config":
10
+ """
11
+ <hq-sms-global-balance-check-config>
12
+ <server url="${server-url}"/>
13
+ <cache dir="cache"/>
14
+ <account name="account" username="USER" password="PASS"/>
15
+ </hq-sms-global-balance-check-config>
16
+ """
17
+
18
+ And a file "default.args":
19
+ """
20
+ --config default.config
21
+ --account account
22
+ --warning 500
23
+ --critical 100
24
+ --cache-warning 10
25
+ --cache-critical 20
26
+ """
27
+
28
+ And a directory "cache"
29
+
30
+ And the balance is 750
31
+ And the time is 100
32
+ And I run the script with "default.args"
33
+
34
+ Scenario: Cache ok
35
+
36
+ Given the time is 105
37
+ And the server is offline
38
+
39
+ When I run the script with "default.args"
40
+
41
+ Then the status should be 0
42
+ And the output should be:
43
+ """
44
+ SMS Global account OK: 750 credits, last check 5 seconds ago
45
+ """
46
+
47
+ Scenario: Cache warning
48
+
49
+ Given the time is 115
50
+ And the server is offline
51
+
52
+ When I run the script with "default.args"
53
+
54
+ Then the status should be 1
55
+ And the output should be:
56
+ """
57
+ SMS Global account WARNING: 750 credits, last check 15 seconds ago (warning is 10)
58
+ """
59
+
60
+ Scenario: Cache critical
61
+
62
+ Given the time is 125
63
+ And the server is offline
64
+
65
+ When I run the script with "default.args"
66
+
67
+ Then the status should be 2
68
+ And the output should be:
69
+ """
70
+ SMS Global account CRITICAL: 750 credits, last check 25 seconds ago (critical is 20)
71
+ """
@@ -0,0 +1,2 @@
1
+ require "hq/cucumber/temp-dir"
2
+ require "hq/cucumber/time"
@@ -2,33 +2,24 @@ require "shellwords"
2
2
 
3
3
  require "hq/sms-global/balance-check/script"
4
4
 
5
- Given /^a file "(.*?)":$/ do
6
- |file_name, file_contents|
7
-
8
- File.open file_name, "w" do
9
- |file_io|
10
-
11
- file_contents =
12
- file_contents.gsub "${server-url}", $web_url
13
-
14
- file_io.write file_contents
5
+ Given /^the balance is (\d+)$/ do
6
+ |balance_str|
15
7
 
16
- end
8
+ $balance = balance_str.to_f
17
9
 
18
10
  end
19
11
 
20
- Given /^that the balance is (\d+)$/ do
21
- |balance_str|
12
+ Given /^the server is offline$/ do
22
13
 
23
- $balance = balance_str.to_f
14
+ $status = :offline
24
15
 
25
16
  end
26
17
 
27
- When /^I run the script with args:$/ do
28
- |args_str|
18
+ When /^I run the script with "(.+)"$/ do
19
+ |args_file|
29
20
 
30
21
  @script = HQ::SmsGlobal::BalanceCheck::Script.new
31
- @script.args = Shellwords.split args_str
22
+ @script.args = Shellwords.split File.read(args_file)
32
23
 
33
24
  @script.stdout = StringIO.new
34
25
  @script.stderr = StringIO.new
@@ -28,13 +28,28 @@ end
28
28
  $web_server.mount_proc "/balance-api.php" do
29
29
  |request, response|
30
30
 
31
- request.query["user"].should == "USER"
32
- request.query["password"].should == "PASS"
31
+ case $status
33
32
 
34
- response.body = "BALANCE: #{$balance}; USER: USER"
33
+ when :online
34
+
35
+ request.query["user"].should == "USER"
36
+ request.query["password"].should == "PASS"
37
+
38
+ response.body = "BALANCE: #{$balance}; USER: USER"
39
+
40
+ when :offline
41
+
42
+ response.status = 500
43
+ response.message = "Internal server error"
44
+
45
+ response.body = "Sorry, an error occurred"
46
+
47
+ end
35
48
 
36
49
  end
37
50
 
38
51
  Before do
39
52
  $balance = 0.0
53
+ $status = :online
54
+ $placeholders["${server-url}"] = $web_url
40
55
  end
@@ -33,6 +33,12 @@ class Script < Tools::CheckScript
33
33
  :convert => :to_f,
34
34
  :required => true },
35
35
 
36
+ { :name => :cache_warning,
37
+ :convert => :to_i },
38
+
39
+ { :name => :cache_critical,
40
+ :convert => :to_i },
41
+
36
42
  ]
37
43
 
38
44
  raise "Extra args" unless @args.empty?
@@ -58,6 +64,9 @@ class Script < Tools::CheckScript
58
64
  @server_elem =
59
65
  @config_elem.find_first "server"
60
66
 
67
+ @cache_elem =
68
+ @config_elem.find_first "cache"
69
+
61
70
  @account_elem =
62
71
  @config_elem.find_first "account [
63
72
  @name = #{esc_xp @opts[:account]}
@@ -67,6 +76,81 @@ class Script < Tools::CheckScript
67
76
 
68
77
  def perform_checks
69
78
 
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
89
+
90
+ read_cache
91
+
92
+ used_cache = true
93
+
94
+ end
95
+
96
+ # report balance
97
+
98
+ if @actual_balance < @opts[:critical]
99
+
100
+ critical "%s credits (critical is %s)" % [
101
+ @actual_balance.to_i,
102
+ @opts[:critical].to_i,
103
+ ]
104
+
105
+ elsif @actual_balance < @opts[:warning]
106
+
107
+ warning "%s credits (warning is %s)" % [
108
+ @actual_balance.to_i,
109
+ @opts[:warning].to_i,
110
+ ]
111
+
112
+ else
113
+
114
+ message "%s credits" % [
115
+ @actual_balance.to_i,
116
+ ]
117
+
118
+ end
119
+
120
+ # report cache usage
121
+
122
+ if used_cache
123
+
124
+ if @opts[:cache_critical] \
125
+ && @cache_age >= @opts[:cache_critical]
126
+
127
+ critical "last check %s seconds ago (critical is %s)" % [
128
+ @cache_age,
129
+ @opts[:cache_critical]
130
+ ]
131
+
132
+ elsif @opts[:cache_warning] \
133
+ && @cache_age >= @opts[:cache_warning]
134
+
135
+ warning "last check %s seconds ago (warning is %s)" % [
136
+ @cache_age,
137
+ @opts[:cache_warning]
138
+ ]
139
+
140
+ else
141
+
142
+ message "last check #{@cache_age} seconds ago"
143
+
144
+ end
145
+
146
+ end
147
+
148
+ write_cache
149
+
150
+ end
151
+
152
+ def get_balance_from_server
153
+
70
154
  url =
71
155
  URI.parse "#{@server_elem["url"]}/balance-api.php"
72
156
 
@@ -79,7 +163,6 @@ class Script < Tools::CheckScript
79
163
  Net::HTTP.start url.host, url.port do
80
164
  |http|
81
165
 
82
-
83
166
  request = Net::HTTP::Get.new "#{url.path}?#{url.query}"
84
167
 
85
168
  response = http.request request
@@ -90,32 +173,54 @@ class Script < Tools::CheckScript
90
173
  raise "Error 2" \
91
174
  unless response.body =~ /^BALANCE: ([^;]+); USER: (.+)$/
92
175
 
93
- actual_balance = $1.to_f
176
+ @actual_balance =
177
+ $1.to_f
178
+
179
+ end
94
180
 
95
- if actual_balance < @opts[:critical]
181
+ end
96
182
 
97
- critical "%s credits (critical is %s)" % [
98
- actual_balance.to_i,
99
- @opts[:critical].to_i,
100
- ]
183
+ def read_cache
101
184
 
102
- elsif actual_balance < @opts[:warning]
185
+ cache_path = "%s/%s.yaml" % [
186
+ @cache_elem["dir"],
187
+ @opts[:account],
188
+ ]
103
189
 
104
- warning "%s credits (warning is %s)" % [
105
- actual_balance.to_i,
106
- @opts[:warning].to_i,
107
- ]
190
+ cache_data =
191
+ YAML.load_file cache_path
108
192
 
109
- else
193
+ @actual_balance =
194
+ cache_data["balance"]
110
195
 
111
- message "%s credits" % [
112
- actual_balance.to_i,
113
- ]
196
+ @cache_age =
197
+ (Time.now - cache_data["timestamp"]).to_i
114
198
 
115
- end
199
+ end
200
+
201
+ def write_cache
202
+
203
+ return unless @cache_elem
204
+
205
+ cache_data = {
206
+ "balance" => @actual_balance,
207
+ "timestamp" => Time.now,
208
+ }
209
+
210
+ cache_path = "%s/%s.yaml" % [
211
+ @cache_elem["dir"],
212
+ @opts[:account],
213
+ ]
214
+
215
+ File.open "#{cache_path}.new", "w" do
216
+ |cache_io|
217
+
218
+ cache_io.write YAML.dump cache_data
116
219
 
117
220
  end
118
221
 
222
+ FileUtils.mv "#{cache_path}.new", cache_path
223
+
119
224
  end
120
225
 
121
226
  end
metadata CHANGED
@@ -1,158 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-sms-global-balance-check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - James Pharaoh
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-10 00:00:00.000000000 Z
11
+ date: 2013-05-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: hq-tools
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 0.8.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.8.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: libxml-ruby
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: 2.6.0
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: 2.6.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: cucumber
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: 1.3.1
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: 1.3.1
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: hq-dev
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
- version: 0.0.2
61
+ version: 0.0.11
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
- version: 0.0.2
68
+ version: 0.0.11
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: json
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - '>='
84
74
  - !ruby/object:Gem::Version
85
75
  version: 1.7.7
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - '>='
92
81
  - !ruby/object:Gem::Version
93
82
  version: 1.7.7
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: rake
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - '>='
100
88
  - !ruby/object:Gem::Version
101
89
  version: 10.0.4
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - '>='
108
95
  - !ruby/object:Gem::Version
109
96
  version: 10.0.4
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: rspec
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - '>='
116
102
  - !ruby/object:Gem::Version
117
103
  version: 2.13.0
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - '>='
124
109
  - !ruby/object:Gem::Version
125
110
  version: 2.13.0
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: rspec_junit_formatter
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ! '>='
115
+ - - '>='
132
116
  - !ruby/object:Gem::Version
133
117
  version: 0.1.6
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ! '>='
122
+ - - '>='
140
123
  - !ruby/object:Gem::Version
141
124
  version: 0.1.6
142
125
  - !ruby/object:Gem::Dependency
143
126
  name: simplecov
144
127
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
128
  requirements:
147
- - - ! '>='
129
+ - - '>='
148
130
  - !ruby/object:Gem::Version
149
131
  version: 0.7.1
150
132
  type: :development
151
133
  prerelease: false
152
134
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
135
  requirements:
155
- - - ! '>='
136
+ - - '>='
156
137
  - !ruby/object:Gem::Version
157
138
  version: 0.7.1
158
139
  description: HQ nagios/icinga plugin to check balance of SMS global account
@@ -164,39 +145,38 @@ extensions: []
164
145
  extra_rdoc_files: []
165
146
  files:
166
147
  - lib/hq/sms-global/balance-check/script.rb
148
+ - features/cache.feature
167
149
  - features/basic.feature
168
- - features/support/temp-dir.rb
169
150
  - features/support/steps.rb
170
151
  - features/support/env.rb
171
152
  - features/support/web-server.rb
172
153
  - bin/hq-sms-global-balance-check
173
154
  homepage: https://github.com/jamespharaoh/hq-sms-global-balance-check
174
155
  licenses: []
156
+ metadata: {}
175
157
  post_install_message:
176
158
  rdoc_options: []
177
159
  require_paths:
178
160
  - lib
179
161
  required_ruby_version: !ruby/object:Gem::Requirement
180
- none: false
181
162
  requirements:
182
- - - ! '>='
163
+ - - '>='
183
164
  - !ruby/object:Gem::Version
184
165
  version: '0'
185
166
  required_rubygems_version: !ruby/object:Gem::Requirement
186
- none: false
187
167
  requirements:
188
- - - ! '>='
168
+ - - '>='
189
169
  - !ruby/object:Gem::Version
190
170
  version: 1.3.6
191
171
  requirements: []
192
172
  rubyforge_project: hq-sms-global-balance-check
193
- rubygems_version: 1.8.23
173
+ rubygems_version: 2.0.3
194
174
  signing_key:
195
- specification_version: 3
175
+ specification_version: 4
196
176
  summary: HQ SMS Global balance check
197
177
  test_files:
178
+ - features/cache.feature
198
179
  - features/basic.feature
199
- - features/support/temp-dir.rb
200
180
  - features/support/steps.rb
201
181
  - features/support/env.rb
202
182
  - features/support/web-server.rb
@@ -1,16 +0,0 @@
1
- require "tmpdir"
2
-
3
- Before do
4
-
5
- @old_dir = Dir.pwd
6
- @temp_dir = Dir.mktmpdir
7
- Dir.chdir @temp_dir
8
-
9
- end
10
-
11
- After do
12
-
13
- FileUtils.remove_entry_secure @temp_dir
14
- Dir.chdir @old_dir
15
-
16
- end