Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
What's new
6
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gitlab-com
migration
Commits
3c496512
Verified
Commit
3c496512
authored
Jul 30, 2018
by
Nick Thomas
Browse files
Reduce execution time from 90 seconds to 2 seconds
parent
0a9b686e
Changes
2
Hide whitespace changes
Inline
Side-by-side
bin/hostinfo
View file @
3c496512
...
...
@@ -4,4 +4,4 @@ set -euo pipefail
IFS
=
$'
\n\t
'
echo
-e
"Date:
$(
date
'+%F %T'
)
\n
"
exec
ruby
"
$(
dirname
$0
)
/hostinfo.rb"
"
$@
"
| column
-t
-s
$'
\t
'
ruby
"
$(
dirname
$0
)
/hostinfo.rb"
"
$@
"
| column
-t
-s
$'
\t
'
bin/hostinfo.rb
View file @
3c496512
#!/usr/bin/env ruby
-w
#!/usr/bin/env ruby
def
resolv
(
hostname
,
ptr:
false
)
result
=
`dig
#{
'-x'
if
ptr
}
#{
hostname
}
+short | tail -1`
.
strip
result
==
""
?
nil
:
result
end
Thread
.
abort_on_exception
=
true
def
whois
(
thing
)
result
=
`whois
#{
thing
}
| grep OrgName | sed -E 's/^.*: +//'`
.
strip
result
==
""
?
nil
:
result
end
require
'net/https'
def
network_owner
(
hostname
)
ip
=
resolv
(
hostname
)
if
ip
whois
(
ip
)
||
"N/A"
else
"DOES_NOT_RESOLVE"
class
Check
# These are provided or computed
attr_reader
:hostname
,
:https_url
,
:ssh_port
# These are looked up in parallel
attr_reader
:ip
,
:https_response
,
:ssh_port_open
,
:network_owner
,
:rev_name
def
initialize
(
hostname
)
@hostname
=
hostname
@https_url
=
"https://
#{
hostname
}
"
@ssh_port
=
hostname
.
start_with?
(
'altssh'
)
?
443
:
22
end
end
def
rev_name
(
hostname
)
ip
=
resolv
(
hostname
)
def
execute
parallel_lookup!
if
ip
rev_ip
=
resolv
(
ip
,
ptr:
true
)
rev_ip
||
ip
else
"DOES_NOT_RESOLVE"
[
hostname
,
network_owner
,
rev_name
,
https_status
,
ssh_port_open
,
redirect
].
join
(
"
\t
"
)
end
end
def
ssh_port_open
(
hostname
,
port
)
result
=
`ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -oConnectTimeout=5 -p "
#{
port
}
" "git@
#{
hostname
}
" 2>/dev/null`
.
strip
def
https_status
return
"Invalid"
unless
https_response
if
result
=~
/Welcome to GitLab/
"Yes"
else
"No"
https_response
.
code
end
end
def
ssh_port
(
hostname
)
if
hostname
.
start_with?
(
'altssh'
)
443
else
22
def
redirect
return
"-"
unless
https_response
&&
https_response
.
key?
(
'Location'
)
https_response
[
'Location'
][
0
..
39
]
end
end
def
http_status
(
url
)
status
=
`(curl --insecure --head --connect-timeout 5 --max-time 5 --silent
#{
url
}
| head -1 | cut -d
\\
-f2) || echo "Error"`
.
strip
if
status
==
""
"Invalid"
else
status
private
def
parallel_lookup!
# Checking whois and reverse IP lookup depends on the IP being resolved,
# so set it off separately
ip_thread
=
Thread
.
new
{
@ip
=
resolv
(
hostname
)
}
threads
=
[
Thread
.
new
{
@https_response
=
curl
(
https_url
)
},
Thread
.
new
{
@ssh_port_open
=
check_ssh
}
]
# Once this join completes, it's safe to reference the IP
ip_thread
.
join
threads
.
concat
([
Thread
.
new
{
@network_owner
=
check_network_owner
},
Thread
.
new
{
@rev_name
=
check_rev_name
}
])
threads
.
map
(
&
:join
)
end
end
def
popen
(
cmd
)
cmd
.
push
(
err: :close
)
result
=
IO
.
popen
(
cmd
)
{
|
io
|
io
.
read
.
strip
}
if
$?
.
success?
result
else
nil
end
end
def
check_ssh
result
=
popen
(
%W[
ssh -o UserKnownHostsFile=/dev/null
\
-o StrictHostKeyChecking=no
\
-oConnectTimeout=5
\
-p
#{
ssh_port
}
\
-q -T
\
git@
#{
hostname
}
]
)
if
result
=~
/Welcome to GitLab/
"Yes"
else
"No"
end
end
def
check_network_owner
return
"DOES_NOT_RESOLVE"
unless
ip
whois
(
ip
)
||
"N/A"
end
def
check_rev_name
return
"DOES_NOT_RESOLVE"
unless
ip
resolv
(
ip
,
ptr:
true
)
||
ip
end
def
resolv
(
hostname
,
ptr:
false
)
cmd
=
[
'dig'
,
'+short'
]
cmd
<<
'-x'
if
ptr
cmd
<<
hostname
result
=
popen
(
cmd
)
return
nil
unless
result
result
.
split
(
"
\n
"
)[
-
1
]
end
def
whois
(
thing
)
result
=
popen
(
%W[whois
#{
thing
}
]
)
return
nil
unless
result
orgname
=
result
.
lines
.
find
{
|
l
|
l
=~
/OrgName:/
}
orgname
&
.
split
(
":"
,
2
)[
1
].
strip
end
def
curl
(
url
)
result
=
popen
(
%W[curl --insecure --head --connect-timeout 5 --max-time 5 --silent
#{
url
}
]
)
return
nil
unless
result
lines
=
result
.
lines
.
map
(
&
:chomp
)
return
nil
unless
lines
.
size
>
1
def
redirect
(
url
)
result
=
`curl --insecure --head --connect-timeout 5 --max-time 5 --silent "
#{
url
}
" | grep Location | sed -E 's/^.*: +//' |cut -c 1-40`
if
$?
.
success?
result
.
strip
else
"-"
version
,
code
,
message
=
lines
[
0
].
split
(
" "
)
kls
=
Net
::
HTTPResponse
::
CODE_TO_OBJ
[
code
]
||
Net
::
HTTPResponse
out
=
kls
.
new
(
version
,
code
,
message
)
lines
[
1
..-
1
].
map
do
|
line
|
key
,
value
=
line
.
split
(
": "
)
out
.
add_field
(
key
,
value
)
end
out
end
end
class
Checks
class
<<
self
def
header
[
"HOST"
,
"NETWORK"
,
"REV"
,
"HTTPS"
,
"SSH"
,
"REDIRECT"
].
join
(
"
\t
"
)
end
end
printf
(
"%s
\t
%s
\t
%s
\t
%s
\t
%s
\t
%s
\t\n
"
,
"HOST"
,
"NETWORK"
,
"REV"
,
"HTTPS"
,
"SSH"
,
"REDIRECT"
)
ARGV
.
each
do
|
hostname
|
printf
(
"%s
\t
%s
\t
%s
\t
%s
\t
%s
\t
%s
\n
"
,
hostname
,
network_owner
(
hostname
),
rev_name
(
hostname
),
http_status
(
"https://
#{
hostname
}
"
),
ssh_port_open
(
hostname
,
ssh_port
(
hostname
)),
redirect
(
"https://
#{
hostname
}
"
)
)
attr_reader
:checks
def
initialize
(
hostnames
)
@checks
=
hostnames
.
map
{
|
hostname
|
Check
.
new
(
hostname
)
}
end
def
execute
threads
=
checks
.
map
{
|
check
|
Thread
.
new
{
check
.
execute
}
}
threads
.
map
(
&
:value
)
end
end
results
=
Checks
.
new
(
ARGV
).
execute
STDOUT
.
puts
Checks
.
header
results
.
each
{
|
result
|
STDOUT
.
puts
result
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment