hq-check-site 0.0.0 → 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.
@@ -1,15 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- script_path = $0
4
- script_path = File.readlink(script_path) while File.symlink?(script_path)
5
- CONFIG = File.expand_path("#{File.dirname(script_path)}/../..")
6
- $LOAD_PATH.unshift "#{CONFIG}/alchemy-hq/ruby"
3
+ require "hq/check-site/script"
7
4
 
8
- require "hq/systools/monitoring/check-site-script"
5
+ include HQ::CheckSite
9
6
 
10
- include HQ::SysTools::Monitoring
11
-
12
- script = CheckSiteScript.new
7
+ script = Script.new
13
8
  script.args = ARGV
14
9
  script.main
15
10
  exit script.status
@@ -0,0 +1,29 @@
1
+ Feature: Check for regex in HTTP response
2
+
3
+ Background:
4
+
5
+ Given a config "regex":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="page">
10
+ <request path="/page"/>
11
+ <response body-regex="y+e+s+"/>
12
+ </step>
13
+ </check-site-script>
14
+ """
15
+
16
+ Scenario: Body contains regex
17
+ Given one server which responds with "-yes-"
18
+ When check-site is run with config "regex"
19
+ Then all servers should receive page requests
20
+ And the message should be "Site OK: 1 hosts found, 0.0s time"
21
+ And the status should be 0
22
+
23
+ Scenario: Body does not contain regex
24
+ Given one server which responds with "-yes-"
25
+ And one server which responds with "-no-"
26
+ When check-site is run with config "regex"
27
+ Then all servers should receive page requests
28
+ And the message should be "Site CRITICAL: 2 hosts found, 1 mismatches, 0.0s time"
29
+ And the status should be 2
@@ -0,0 +1,21 @@
1
+ Feature: Function correctly in edge cases based on config file
2
+
3
+ Background:
4
+
5
+ Given a config "no-path":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}/page">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="page">
10
+ <request/>
11
+ <response/>
12
+ </step>
13
+ </check-site-script>
14
+ """
15
+
16
+ Scenario: No path specified
17
+ Given one server which responds in 0 seconds
18
+ When check-site is run with config "no-path"
19
+ Then all servers should receive page requests
20
+ And the message should be "Site OK: 1 hosts found, 0.0s time"
21
+ And the status should be 0
@@ -0,0 +1,36 @@
1
+ Feature: Handle various types of connection error
2
+
3
+ Background:
4
+
5
+ Given a config "default":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="page">
10
+ <request path="/page"/>
11
+ <response/>
12
+ </step>
13
+ </check-site-script>
14
+ """
15
+
16
+ Given a config "wrong-port":
17
+ """
18
+ <check-site-script base-url="http://hostname:65535">
19
+ <timings warning="2" critical="4" timeout="10"/>
20
+ <step name="page">
21
+ <request path="/path"/>
22
+ <response/>
23
+ </step>
24
+ </check-site-script>
25
+ """
26
+
27
+ Scenario: No servers
28
+ When check-site is run with config "default"
29
+ And the message should be "Site CRITICAL: unable to resolve hostname"
30
+ And the status should be 2
31
+
32
+ Scenario: Connection refused
33
+ Given one server which responds in 0 seconds
34
+ When check-site is run with config "wrong-port"
35
+ Then the message should be "Site CRITICAL: 1 hosts found, 1 uncontactable"
36
+ And the status should be 2
@@ -0,0 +1,42 @@
1
+ Feature: Authenticate via custom HTTP headers
2
+
3
+ Background:
4
+
5
+ Given a config "default":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="page">
10
+ <request path="/page"/>
11
+ <response/>
12
+ </step>
13
+ </check-site-script>
14
+ """
15
+
16
+ Given a config "headers-auth":
17
+ """
18
+ <check-site-script base-url="http://hostname:${port}">
19
+ <timings warning="2" critical="4" timeout="10"/>
20
+ <step name="page">
21
+ <request path="/page">
22
+ <header name="username" value="USER"/>
23
+ <header name="password" value="PASS"/>
24
+ </request>
25
+ <response/>
26
+ </step>
27
+ </check-site-script>
28
+ """
29
+
30
+ Scenario: Form based login success
31
+ Given one server which requires header based login
32
+ When check-site is run with config "headers-auth"
33
+ Then all servers should receive page requests
34
+ And the message should be "Site OK: 1 hosts found, 0.0s time"
35
+ And the status should be 0
36
+
37
+ Scenario: Form based login failure
38
+ Given one server which requires header based login
39
+ When check-site is run with config "default"
40
+ Then all servers should receive page requests
41
+ And the message should be "Site CRITICAL: 1 hosts found, 1 errors (500), 0.0s time"
42
+ And the status should be 2
@@ -0,0 +1,28 @@
1
+ Feature: Supply credentials via HTTP authentication
2
+
3
+ Background:
4
+
5
+ Given a config "http-auth":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="page">
10
+ <request path="/page" username="USER" password="PASS"/>
11
+ <response/>
12
+ </step>
13
+ </check-site-script>
14
+ """
15
+
16
+ Scenario: Username and password are correct
17
+ Given one server which requires username "USER" and password "PASS"
18
+ When check-site is run with config "http-auth"
19
+ Then all servers should receive page requests
20
+ And the message should be "Site OK: 1 hosts found, 0.0s time"
21
+ And the status should be 0
22
+
23
+ Scenario: Username and password are incorrect
24
+ Given one server which requires username "USER" and password "SECRET"
25
+ When check-site is run with config "http-auth"
26
+ Then all servers should receive page requests
27
+ And the message should be "Site CRITICAL: 1 hosts found, 1 errors (401), 0.0s time"
28
+ And the status should be 2
@@ -0,0 +1,28 @@
1
+ Feature: Log in via an HTML form
2
+
3
+ Background:
4
+
5
+ Given a config "form-auth":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="login">
10
+ <request path="/login" method="post">
11
+ <param name="username" value="USER"/>
12
+ <param name="password" value="PASS"/>
13
+ </request>
14
+ <response/>
15
+ </step>
16
+ <step name="page">
17
+ <request path="/page" username="USER" password="PASS"/>
18
+ <response/>
19
+ </step>
20
+ </check-site-script>
21
+ """
22
+
23
+ Scenario: Form based login
24
+ Given one server which requires form based login with "USER" and "PASS"
25
+ When check-site is run with config "form-auth"
26
+ Then all servers should receive page requests
27
+ And the message should be "Site OK: 1 hosts found, 0.0s time"
28
+ And the status should be 0
@@ -11,6 +11,8 @@ $web_config = {
11
11
  :DoNotReverseLookup => true,
12
12
  }
13
13
 
14
+ $servers = {}
15
+
14
16
  $web_server =
15
17
  WEBrick::HTTPServer.new \
16
18
  $web_config
@@ -63,11 +65,13 @@ $web_server.mount_proc "/page" do
63
65
  server[:request_count] += 1
64
66
 
65
67
  if server[:auth_method] == :http
68
+
66
69
  WEBrick::HTTPAuth.basic_auth request, response, "Realm" do
67
70
  |user, pass|
68
71
  user == server[:auth_username] &&
69
72
  pass == server[:auth_password]
70
73
  end
74
+
71
75
  end
72
76
 
73
77
  if server[:auth_method] == :form
@@ -86,6 +90,13 @@ $web_server.mount_proc "/page" do
86
90
 
87
91
  end
88
92
 
93
+ if server[:auth_method] == :headers
94
+
95
+ raise "username" unless request["username"] == "USER"
96
+ raise "password" unless request["password"] == "PASS"
97
+
98
+ end
99
+
89
100
  response.status = server[:response_code]
90
101
  response.body = server[:response_body]
91
102
 
@@ -69,6 +69,23 @@ Given /^one server which requires form based login with "([^"]*)" and "([^"]*)"$
69
69
 
70
70
  end
71
71
 
72
+ Given /^one server which requires header based login$/ do
73
+
74
+ server = {
75
+ address: "127.0.1.#{$servers.size}",
76
+ request_count: 0,
77
+ response_code: "200",
78
+ response_time: 0,
79
+ response_body: "",
80
+ auth_method: :headers,
81
+ auth_username: "USER",
82
+ auth_password: "PASS",
83
+ }
84
+
85
+ $servers[server[:address]] = server
86
+
87
+ end
88
+
72
89
  When /^check\-site is run with config "([^"]*)"$/ do
73
90
  |config_name|
74
91
 
@@ -0,0 +1,38 @@
1
+ Feature: Handle timeout correctly
2
+
3
+ Background:
4
+
5
+ Given a config "default":
6
+ """
7
+ <check-site-script base-url="http://hostname:${port}">
8
+ <timings warning="2" critical="4" timeout="10"/>
9
+ <step name="page">
10
+ <request path="/page"/>
11
+ <response/>
12
+ </step>
13
+ </check-site-script>
14
+ """
15
+
16
+ Given a config "timeout":
17
+ """
18
+ <check-site-script base-url="http://hostname:${port}">
19
+ <timings warning="2" critical="4" timeout="0"/>
20
+ <step name="page">
21
+ <request path="/page"/>
22
+ <response/>
23
+ </step>
24
+ </check-site-script>
25
+ """
26
+
27
+ Scenario: Timeout does not expire
28
+ Given one server which responds in 0 seconds
29
+ When check-site is run with config "default"
30
+ Then all servers should receive page requests
31
+ And the message should be "Site OK: 1 hosts found, 0.0s time"
32
+ And the status should be 0
33
+
34
+ Scenario: Timeout expires
35
+ Given one server which responds in 0 seconds
36
+ When check-site is run with config "timeout"
37
+ And the message should be "Site CRITICAL: 1 hosts found, 1 uncontactable"
38
+ And the status should be 2
@@ -12,12 +12,6 @@ module HQ
12
12
  module CheckSite
13
13
  class Script < Tools::CheckScript
14
14
 
15
- # custom http class allows us to connect to a different address
16
-
17
- class CustomHTTP < Net::HTTP
18
- attr_accessor :conn_address
19
- end
20
-
21
15
  def initialize
22
16
  super
23
17
  @name = "Site"
@@ -122,8 +116,13 @@ class Script < Tools::CheckScript
122
116
 
123
117
  # open http connection
124
118
 
125
- http = CustomHTTP.new @base_url.host, @base_url.port
126
- http.conn_address = address
119
+ http =
120
+ Net::HTTP.new \
121
+ @base_url.host,
122
+ @base_url.port,
123
+ address,
124
+ @base_url.port
125
+
127
126
  http.open_timeout = @timeout_time
128
127
  http.read_timeout = @timeout_time
129
128
  http.use_ssl = @base_url.scheme == "https"
@@ -176,7 +175,7 @@ class Script < Tools::CheckScript
176
175
  raise "error"
177
176
  end
178
177
 
179
- # set headers
178
+ # set standard headers
180
179
 
181
180
  req["host"] = @base_url.host
182
181
  req["user-agent"] = "hq check-site"
@@ -189,6 +188,13 @@ class Script < Tools::CheckScript
189
188
  }.join ", "
190
189
  end
191
190
 
191
+ # set custom headers
192
+
193
+ request_elem.find("header").each do
194
+ |header_elem|
195
+ req[header_elem["name"]] = header_elem["value"]
196
+ end
197
+
192
198
  # set http auth
193
199
 
194
200
  if request_elem["username"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-check-site
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 2.6.0
46
- - !ruby/object:Gem::Dependency
47
- name: capybara
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: 2.0.2
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: 2.0.2
62
46
  - !ruby/object:Gem::Dependency
63
47
  name: cucumber
64
48
  requirement: !ruby/object:Gem::Requirement
@@ -148,7 +132,13 @@ extensions: []
148
132
  extra_rdoc_files: []
149
133
  files:
150
134
  - lib/hq/check-site/script.rb
151
- - features/check-site.feature
135
+ - features/headers-auth.feature
136
+ - features/config-edge-cases.feature
137
+ - features/http-auth.feature
138
+ - features/timeout.feature
139
+ - features/body-regex.feature
140
+ - features/errors.feature
141
+ - features/login-form.feature
152
142
  - features/support/steps.rb
153
143
  - features/support/env.rb
154
144
  - bin/hq-check-site
@@ -177,6 +167,12 @@ signing_key:
177
167
  specification_version: 3
178
168
  summary: HQ check site script
179
169
  test_files:
180
- - features/check-site.feature
170
+ - features/headers-auth.feature
171
+ - features/config-edge-cases.feature
172
+ - features/http-auth.feature
173
+ - features/timeout.feature
174
+ - features/body-regex.feature
175
+ - features/errors.feature
176
+ - features/login-form.feature
181
177
  - features/support/steps.rb
182
178
  - features/support/env.rb
@@ -1,178 +0,0 @@
1
- Feature: Check site script
2
-
3
- Background:
4
-
5
- Given a config "default":
6
- """
7
- <check-site-script base-url="http://hostname:${port}">
8
- <timings warning="2" critical="4" timeout="10"/>
9
- <step name="page">
10
- <request path="/page"/>
11
- <response/>
12
- </step>
13
- </check-site-script>
14
- """
15
-
16
- Given a config "regex":
17
- """
18
- <check-site-script base-url="http://hostname:${port}">
19
- <timings warning="2" critical="4" timeout="10"/>
20
- <step name="page">
21
- <request path="/page"/>
22
- <response body-regex="y+e+s+"/>
23
- </step>
24
- </check-site-script>
25
- """
26
-
27
- Given a config "timeout":
28
- """
29
- <check-site-script base-url="http://hostname:${port}">
30
- <timings warning="2" critical="4" timeout="0"/>
31
- <step name="page">
32
- <request path="/page"/>
33
- <response/>
34
- </step>
35
- </check-site-script>
36
- """
37
-
38
- Given a config "http-auth":
39
- """
40
- <check-site-script base-url="http://hostname:${port}">
41
- <timings warning="2" critical="4" timeout="10"/>
42
- <step name="page">
43
- <request path="/page" username="USER" password="PASS"/>
44
- <response/>
45
- </step>
46
- </check-site-script>
47
- """
48
-
49
- Given a config "form-auth":
50
- """
51
- <check-site-script base-url="http://hostname:${port}">
52
- <timings warning="2" critical="4" timeout="10"/>
53
- <step name="login">
54
- <request path="/login" method="post">
55
- <param name="username" value="USER"/>
56
- <param name="password" value="PASS"/>
57
- </request>
58
- <response/>
59
- </step>
60
- <step name="page">
61
- <request path="/page" username="USER" password="PASS"/>
62
- <response/>
63
- </step>
64
- </check-site-script>
65
- """
66
-
67
- Given a config "no-path":
68
- """
69
- <check-site-script base-url="http://hostname:${port}/page">
70
- <timings warning="2" critical="4" timeout="10"/>
71
- <step name="page">
72
- <request/>
73
- <response/>
74
- </step>
75
- </check-site-script>
76
- """
77
-
78
- Given a config "wrong-port":
79
- """
80
- <check-site-script base-url="http://hostname:65535">
81
- <timings warning="2" critical="4" timeout="10"/>
82
- <step name="page">
83
- <request path="/path"/>
84
- <response/>
85
- </step>
86
- </check-site-script>
87
- """
88
-
89
- Scenario: Site responds in ok time
90
- Given one server which responds in 1 second
91
- When check-site is run with config "default"
92
- Then all servers should receive page requests
93
- And the message should be "Site OK: 1 hosts found, 1.0s time"
94
- And the status should be 0
95
-
96
- Scenario: Site responds in warning time
97
- Given one server which responds in 1 second
98
- And one server which responds in 3 seconds
99
- When check-site is run with config "default"
100
- Then all servers should receive page requests
101
- And the message should be "Site WARNING: 2 hosts found, 3.0s time (warning is 2.0)"
102
- And the status should be 1
103
-
104
- Scenario: Site responds in critical time
105
- Given one server which responds in 1 second
106
- And one server which responds in 3 seconds
107
- And one server which responds in 5 seconds
108
- When check-site is run with config "default"
109
- Then all servers should receive page requests
110
- And the message should be "Site CRITICAL: 3 hosts found, 5.0s time (critical is 4.0)"
111
- And the status should be 2
112
-
113
- Scenario: Body contains regex
114
- Given one server which responds with "-yes-"
115
- When check-site is run with config "regex"
116
- Then all servers should receive page requests
117
- And the message should be "Site OK: 1 hosts found, 0.0s time"
118
- And the status should be 0
119
-
120
- Scenario: Body does not contain regex
121
- Given one server which responds with "-yes-"
122
- And one server which responds with "-no-"
123
- When check-site is run with config "regex"
124
- Then all servers should receive page requests
125
- And the message should be "Site CRITICAL: 2 hosts found, 1 mismatches, 0.0s time"
126
- And the status should be 2
127
-
128
- Scenario: Timeout does not expire
129
- Given one server which responds in 0 seconds
130
- When check-site is run with config "default"
131
- Then all servers should receive page requests
132
- And the message should be "Site OK: 1 hosts found, 0.0s time"
133
- And the status should be 0
134
-
135
- Scenario: Timeout expires
136
- Given one server which responds in 0 seconds
137
- When check-site is run with config "timeout"
138
- And the message should be "Site CRITICAL: 1 hosts found, 1 uncontactable"
139
- And the status should be 2
140
-
141
- Scenario: Username and password are correct
142
- Given one server which requires username "USER" and password "PASS"
143
- When check-site is run with config "http-auth"
144
- Then all servers should receive page requests
145
- And the message should be "Site OK: 1 hosts found, 0.0s time"
146
- And the status should be 0
147
-
148
- Scenario: Username and password are incorrect
149
- Given one server which requires username "USER" and password "SECRET"
150
- When check-site is run with config "http-auth"
151
- Then all servers should receive page requests
152
- And the message should be "Site CRITICAL: 1 hosts found, 1 errors (401), 0.0s time"
153
- And the status should be 2
154
-
155
- Scenario: No servers
156
- When check-site is run with config "default"
157
- And the message should be "Site CRITICAL: unable to resolve hostname"
158
- And the status should be 2
159
-
160
- Scenario: Form based login
161
- Given one server which requires form based login with "USER" and "PASS"
162
- When check-site is run with config "form-auth"
163
- Then all servers should receive page requests
164
- And the message should be "Site OK: 1 hosts found, 0.0s time"
165
- And the status should be 0
166
-
167
- Scenario: No path specified
168
- Given one server which responds in 0 seconds
169
- When check-site is run with config "no-path"
170
- Then all servers should receive page requests
171
- And the message should be "Site OK: 1 hosts found, 0.0s time"
172
- And the status should be 0
173
-
174
- Scenario: Connection refused
175
- Given one server which responds in 0 seconds
176
- When check-site is run with config "wrong-port"
177
- Then the message should be "Site CRITICAL: 1 hosts found, 1 uncontactable"
178
- And the status should be 2