hq-tools 0.8.0 → 0.8.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39e4f42426204ba82333f948c629cc1120585b98
4
+ data.tar.gz: 6e14dc6e6cec5624073961da9f491bbce307d2ba
5
+ SHA512:
6
+ metadata.gz: aef7b55689689b60bad8c5e2104d92527860c4e7f7e06258dd3c624a0a8c6aef08100ed4789a4a34889e15efa0efc60e81b1b5ef1b116d395d79cc55bf15ecdb
7
+ data.tar.gz: d184e99334dc58ed664b49260b2d7784ee2bebe7f6c4415dcc889e12ba0aa4073d06f5191b1fb7682f6c2d03808baa145207585069ab4248de25b195f1bced69
@@ -0,0 +1,44 @@
1
+ module HQ
2
+ module Tools
3
+
4
+ # This class provides a base for creating objects which can be either run as
5
+ # standalone scripts or invoked directly from another ruby script, for testing
6
+ # or other purposes.
7
+
8
+ class BaseScript
9
+
10
+ # Should be set to the args the script was invoked with before calling
11
+ # #main.
12
+
13
+ attr_accessor :args
14
+
15
+ # Will contain the script's exit status after calling #main
16
+
17
+ attr_reader :status
18
+
19
+ # The IO object to treat as stdout.
20
+
21
+ attr_accessor :stdout
22
+
23
+ # The IO object to treat as stderr.
24
+
25
+ attr_accessor :stderr
26
+
27
+ # Creates a new base script object with default field values
28
+
29
+ def initialize
30
+ @args = []
31
+ @status = 0
32
+ @stdout = $stdout
33
+ @stderr = $stderr
34
+ end
35
+
36
+ # Does nothing. This should be overriden by derived classes.
37
+
38
+ def main
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -1,11 +1,22 @@
1
+ require "hq/tools/base-script"
2
+
1
3
  module HQ
2
4
  module Tools
3
- class CheckScript
4
5
 
5
- attr_accessor :args
6
- attr_accessor :status
7
- attr_accessor :stdout
8
- attr_accessor :stderr
6
+ # This is an abstract base class for creating icinga/nagios plugins.
7
+ #
8
+ # Create a derived class and override the #process_args, #prepare and
9
+ # #perform_checks methods where you will do your work.
10
+ #
11
+ # During the #perform_checks method, you should make calls to the methods
12
+ # #message, #warning, #critical and #unknown, with short messages to be added to
13
+ # the final output.
14
+ #
15
+ # The exit status and full output will be put together for you.
16
+
17
+ class CheckScript < BaseScript
18
+
19
+ # Creates a new check script.
9
20
 
10
21
  def initialize
11
22
  @name = "Unnamed"
@@ -19,6 +30,10 @@ class CheckScript
19
30
  @stderr = $stderr
20
31
  end
21
32
 
33
+ # Handle a single invocation of the script. This calls #process_args,
34
+ # #prepare, #perform_checks and #perform_output. It also gracefully handles
35
+ # any exceptions thrown during #prepare and #perform_checks.
36
+
22
37
  def main
23
38
 
24
39
  process_args
@@ -44,10 +59,31 @@ class CheckScript
44
59
 
45
60
  end
46
61
 
47
- def prepare
62
+ private
63
+
64
+ # Process command line arguments. This does nothing and is here as a hook
65
+ # for derived classes to override.
66
+
67
+ def process_args #:doc:
68
+ end
69
+
70
+ # Prepare to perform checks. This does nothing and is here as a hook for
71
+ # derived classes to override.
72
+
73
+ def prepare #:doc:
74
+ end
75
+
76
+ # Perform the actual checks. This does nothing does nothing and is here as a
77
+ # hook for derived classes to override.
78
+
79
+ def perform_checks #:doc:
48
80
  end
49
81
 
50
- def perform_output
82
+ # Perform the output. This is called after checks are performed and both
83
+ # outputs the name, status and messages on stdout, and sets the appropriate
84
+ # return status for the script.
85
+
86
+ def perform_output #:doc:
51
87
 
52
88
  str = StringIO.new
53
89
 
@@ -87,26 +123,41 @@ class CheckScript
87
123
 
88
124
  end
89
125
 
90
- def message string
126
+ # Add a short message to the output, without affecting the status.
127
+
128
+ def message string #:doc:
91
129
  @messages << string
92
130
  end
93
131
 
94
- def critical string
132
+ # Add a short message to the output, and set a critical status. This status
133
+ # overrides all others.
134
+
135
+ def critical string #:doc:
95
136
  @messages << string
96
137
  @critical = true
97
138
  end
98
139
 
99
- def warning string
140
+ # Add a short message to the output, and set a warning status. This status
141
+ # is overridden by critical.
142
+
143
+ def warning string #:doc:
100
144
  @messages << string
101
145
  @warning = true
102
146
  end
103
147
 
104
- def unknown string
148
+ # Add a short message to the output, and set an unknown status. This status
149
+ # is unknown by both warning and critical.
150
+
151
+ def unknown string #:doc:
105
152
  @messages << string
106
153
  @unknown = true
107
154
  end
108
155
 
109
- def performance name, value, options = {}
156
+ # Add a performance metric. Recognised options are :units, :warning,
157
+ # :critical, :minimum and :maximum. See the icinga spec at
158
+ # http://docs.icinga.org/latest/en/perfdata.html#formatperfdata
159
+
160
+ def performance name, value, options = {} #:doc:
110
161
  parts = []
111
162
  parts[0] = "%s%s" % [ value, options[:units] ]
112
163
  parts[1] = options[:warning].to_s if options[:warning]
@@ -0,0 +1,40 @@
1
+ require "hq/tools/base-script"
2
+
3
+ module HQ
4
+ module Tools
5
+
6
+ describe BaseScript do
7
+
8
+ context ".args" do
9
+ it "defaults to an empty array" do
10
+ subject.args.should == []
11
+ end
12
+ end
13
+
14
+ context ".status" do
15
+ it "defaults to 0" do
16
+ subject.status.should == 0
17
+ end
18
+ end
19
+
20
+ context ".stdout" do
21
+ it "defaults to $stdout" do
22
+ subject.stdout.should == $stdout
23
+ end
24
+ end
25
+
26
+ context ".stderr" do
27
+ it "defaults to $stderr" do
28
+ subject.stderr.should == $stderr
29
+ end
30
+ end
31
+
32
+ context "#main" do
33
+ it "does nothing" do
34
+ end
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -15,10 +15,10 @@ describe CheckScript do
15
15
 
16
16
  it "critical" do
17
17
  subject.stub(:perform_checks) do
18
- subject.critical "critical"
19
- subject.warning "warning"
20
- subject.unknown "unknown"
21
- subject.message "ok"
18
+ subject.send :critical, "critical"
19
+ subject.send :warning, "warning"
20
+ subject.send :unknown, "unknown"
21
+ subject.send :message, "ok"
22
22
  end
23
23
  subject.main
24
24
  subject.stdout.string.should ==
@@ -28,9 +28,9 @@ describe CheckScript do
28
28
 
29
29
  it "warning" do
30
30
  subject.stub(:perform_checks) do
31
- subject.warning "warning"
32
- subject.unknown "unknown"
33
- subject.message "ok"
31
+ subject.send :warning, "warning"
32
+ subject.send :unknown, "unknown"
33
+ subject.send :message, "ok"
34
34
  end
35
35
  subject.main
36
36
  subject.stdout.string.should ==
@@ -40,8 +40,8 @@ describe CheckScript do
40
40
 
41
41
  it "unknown" do
42
42
  subject.stub(:perform_checks) do
43
- subject.unknown "unknown"
44
- subject.message "ok"
43
+ subject.send :unknown, "unknown"
44
+ subject.send :message, "ok"
45
45
  end
46
46
  subject.main
47
47
  subject.stdout.string.should ==
@@ -51,7 +51,7 @@ describe CheckScript do
51
51
 
52
52
  it "ok" do
53
53
  subject.stub(:perform_checks) do
54
- subject.message "ok"
54
+ subject.send :message, "ok"
55
55
  end
56
56
  subject.main
57
57
  subject.stdout.string.should ==
@@ -65,7 +65,7 @@ describe CheckScript do
65
65
 
66
66
  it "none" do
67
67
  subject.stub(:perform_checks) do
68
- subject.message "hello"
68
+ subject.send :message, "hello"
69
69
  end
70
70
  subject.main
71
71
  subject.stdout.string.should ==
@@ -74,8 +74,8 @@ describe CheckScript do
74
74
 
75
75
  it "single metric" do
76
76
  subject.stub(:perform_checks) do
77
- subject.message "hello"
78
- subject.performance "metric1", 1
77
+ subject.send :message, "hello"
78
+ subject.send :performance, "metric1", 1
79
79
  end
80
80
  subject.main
81
81
  subject.stdout.string.should ==
@@ -84,8 +84,8 @@ describe CheckScript do
84
84
 
85
85
  it "apostrophe" do
86
86
  subject.stub(:perform_checks) do
87
- subject.message "hello"
88
- subject.performance "apos'trophe", 1
87
+ subject.send :message, "hello"
88
+ subject.send :performance, "apos'trophe", 1
89
89
  end
90
90
  subject.main
91
91
  subject.stdout.string.should ==
@@ -94,8 +94,8 @@ describe CheckScript do
94
94
 
95
95
  it "whitespace" do
96
96
  subject.stub(:perform_checks) do
97
- subject.message "hello"
98
- subject.performance "white space", 1
97
+ subject.send :message, "hello"
98
+ subject.send :performance, "white space", 1
99
99
  end
100
100
  subject.main
101
101
  subject.stdout.string.should ==
@@ -104,8 +104,8 @@ describe CheckScript do
104
104
 
105
105
  it "optional fields" do
106
106
  subject.stub(:perform_checks) do
107
- subject.message "hello"
108
- subject.performance \
107
+ subject.send :message, "hello"
108
+ subject.send :performance, \
109
109
  "metric1",
110
110
  1,
111
111
  :units => "blops",
@@ -121,9 +121,9 @@ describe CheckScript do
121
121
 
122
122
  it "two metrics" do
123
123
  subject.stub(:perform_checks) do
124
- subject.message "hello"
125
- subject.performance "metric1", 1
126
- subject.performance "metric2", 5
124
+ subject.send :message, "hello"
125
+ subject.send :performance, "metric1", 1
126
+ subject.send :performance, "metric2", 5
127
127
  end
128
128
  subject.main
129
129
  subject.stdout.string.should ==
metadata CHANGED
@@ -1,112 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
5
- prerelease:
4
+ version: 0.8.1
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-07 00:00:00.000000000 Z
11
+ date: 2013-05-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: cucumber
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
- version: 1.2.1
19
+ version: 1.3.1
22
20
  type: :development
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
- version: 1.2.1
26
+ version: 1.3.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: hq-dev
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.17
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.17
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: libxml-ruby
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ! '>='
45
+ - - '>='
36
46
  - !ruby/object:Gem::Version
37
47
  version: 2.6.0
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ! '>='
52
+ - - '>='
44
53
  - !ruby/object:Gem::Version
45
54
  version: 2.6.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: json
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.7.7
46
69
  - !ruby/object:Gem::Dependency
47
70
  name: rake
48
71
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
72
  requirements:
51
- - - ! '>='
73
+ - - '>='
52
74
  - !ruby/object:Gem::Version
53
- version: 10.0.3
75
+ version: 10.0.4
54
76
  type: :development
55
77
  prerelease: false
56
78
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
79
  requirements:
59
- - - ! '>='
80
+ - - '>='
60
81
  - !ruby/object:Gem::Version
61
- version: 10.0.3
82
+ version: 10.0.4
62
83
  - !ruby/object:Gem::Dependency
63
84
  name: rspec
64
85
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
86
  requirements:
67
- - - ! '>='
87
+ - - '>='
68
88
  - !ruby/object:Gem::Version
69
- version: 2.12.0
89
+ version: 2.13.0
70
90
  type: :development
71
91
  prerelease: false
72
92
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
93
  requirements:
75
- - - ! '>='
94
+ - - '>='
76
95
  - !ruby/object:Gem::Version
77
- version: 2.12.0
96
+ version: 2.13.0
78
97
  - !ruby/object:Gem::Dependency
79
98
  name: rspec_junit_formatter
80
99
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
100
  requirements:
83
- - - ! '>='
101
+ - - '>='
84
102
  - !ruby/object:Gem::Version
85
- version: '0'
103
+ version: 0.1.6
86
104
  type: :development
87
105
  prerelease: false
88
106
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
107
  requirements:
91
- - - ! '>='
108
+ - - '>='
92
109
  - !ruby/object:Gem::Version
93
- version: '0'
110
+ version: 0.1.6
94
111
  - !ruby/object:Gem::Dependency
95
112
  name: simplecov
96
113
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
114
  requirements:
99
- - - ! '>='
115
+ - - '>='
100
116
  - !ruby/object:Gem::Version
101
- version: '0'
117
+ version: 0.7.1
102
118
  type: :development
103
119
  prerelease: false
104
120
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
121
  requirements:
107
- - - ! '>='
122
+ - - '>='
108
123
  - !ruby/object:Gem::Version
109
- version: '0'
124
+ version: 0.7.1
110
125
  description: HQ common library
111
126
  email:
112
127
  - james@phsys.co.uk
@@ -119,41 +134,43 @@ files:
119
134
  - lib/hq/tools/lock.rb
120
135
  - lib/hq/tools/escape.rb
121
136
  - lib/hq/tools/thread-pool.rb
137
+ - lib/hq/tools/base-script.rb
122
138
  - lib/hq/tools/check-script.rb
123
139
  - lib/hq/tools/libxml-ruby.rb
124
140
  - lib/hq/tools/future.rb
125
141
  - spec/hq/tools/libxml-ruby-spec.rb
126
142
  - spec/hq/tools/lock-spec.rb
127
143
  - spec/hq/tools/getopt-spec.rb
144
+ - spec/hq/tools/base-script-spec.rb
128
145
  - spec/hq/tools/cron-spec.rb
129
146
  - spec/hq/tools/check-script-spec.rb
130
147
  homepage: https://github.com/jamespharaoh/hq-tools
131
148
  licenses: []
149
+ metadata: {}
132
150
  post_install_message:
133
151
  rdoc_options: []
134
152
  require_paths:
135
153
  - lib
136
154
  required_ruby_version: !ruby/object:Gem::Requirement
137
- none: false
138
155
  requirements:
139
- - - ! '>='
156
+ - - '>='
140
157
  - !ruby/object:Gem::Version
141
158
  version: '0'
142
159
  required_rubygems_version: !ruby/object:Gem::Requirement
143
- none: false
144
160
  requirements:
145
- - - ! '>='
161
+ - - '>='
146
162
  - !ruby/object:Gem::Version
147
163
  version: 1.3.6
148
164
  requirements: []
149
165
  rubyforge_project: hq-tools
150
- rubygems_version: 1.8.23
166
+ rubygems_version: 2.0.3
151
167
  signing_key:
152
- specification_version: 3
168
+ specification_version: 4
153
169
  summary: HQ tools
154
170
  test_files:
155
171
  - spec/hq/tools/libxml-ruby-spec.rb
156
172
  - spec/hq/tools/lock-spec.rb
157
173
  - spec/hq/tools/getopt-spec.rb
174
+ - spec/hq/tools/base-script-spec.rb
158
175
  - spec/hq/tools/cron-spec.rb
159
176
  - spec/hq/tools/check-script-spec.rb