-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connectivity diagnostic with VC/ESXi from appliance #3210
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3ced0ad
Check VC/ESXi availability from the appliance/
vburenin b60cdb3
Merge branch 'master' into basic-diagnostics
vburenin 00c9d5d
Better diagnostic text.
vburenin ab29cbc
Merge branch 'master' into basic-diagnostics
vburenin e835e9b
Fixed "make check" complains.
vburenin 25c5f70
Merge branch 'basic-diagnostics' of github.com:vburenin/vic into basi…
vburenin 7588c42
Added mention of appliance in the error text.
vburenin 6146421
removed accidentally added file.
vburenin 43b5321
Merge branch 'master' into basic-diagnostics
vburenin f059080
Updated logs and comments to refer vSphere instead of ESXi/vCenter.
vburenin 9ecc720
Merge branch 'basic-diagnostics' of github.com:vburenin/vic into basi…
vburenin 7c1c3db
Removed ping test due to high complexity of implementation. Will be d…
vburenin 0fc34a8
Merge branch 'master' into basic-diagnostics
vburenin 80c8cca
Merge branch 'master' into basic-diagnostics
vburenin 6245945
Merge branch 'master' into basic-diagnostics
vburenin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright 2016 VMware, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package diag | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/vmware/vic/pkg/trace" | ||
) | ||
|
||
// StatusCodeFatalThreshold defines a threshold after which all codes can be treated as fatal. | ||
const StatusCodeFatalThreshold = 64 | ||
|
||
const ( | ||
// VCStatusOK vSphere API is available. | ||
VCStatusOK = 0 | ||
// VCStatusInvalidURL Provided vSphere API URL is wrong. | ||
VCStatusInvalidURL = 64 | ||
// VCStatusErrorQuery Error happened trying to query vSphere API | ||
VCStatusErrorQuery = 65 | ||
// VCStatusErrorResponse Received response doesn't contain expected data. | ||
VCStatusErrorResponse = 66 | ||
// VCStatusIncorrectResponse Received in case if returned data from server is different from expected. | ||
VCStatusIncorrectResponse = 67 | ||
// VCStatusNotXML Received response is not XML | ||
VCStatusNotXML = 68 | ||
// VCStatusUnknownHost is returned in case if DNS failed to resolve name. | ||
VCStatusUnknownHost = 69 | ||
// VCStatusHostIsNotReachable | ||
VCStatusHostIsNotReachable = 70 | ||
) | ||
|
||
// UserReadableVCAPITestDescription convert API test code into user readable text | ||
func UserReadableVCAPITestDescription(code int) string { | ||
switch code { | ||
case VCStatusOK: | ||
return "vSphere API target responds as expected" | ||
case VCStatusInvalidURL: | ||
return "vSphere API target url is invalid" | ||
case VCStatusErrorQuery: | ||
return "vSphere API target failed to respond to the query" | ||
case VCStatusIncorrectResponse: | ||
return "vSphere API target returns unexpected response" | ||
case VCStatusErrorResponse: | ||
return "vSphere API target returns error" | ||
case VCStatusNotXML: | ||
return "vSphere API target returns non XML response" | ||
case VCStatusUnknownHost: | ||
return "vSphere API target can not be resolved from VCH" | ||
case VCStatusHostIsNotReachable: | ||
return "vSphere API target is out of reach. Wrong routing table?" | ||
default: | ||
return "vSphere API target test returned unknown code" | ||
} | ||
} | ||
|
||
// CheckAPIAvailability accesses vSphere API to ensure it is a correct end point that is up and running. | ||
func CheckAPIAvailability(targetURL string) int { | ||
op := trace.NewOperation(context.Background(), "api test") | ||
errorCode := VCStatusErrorQuery | ||
|
||
u, err := url.Parse(targetURL) | ||
if err != nil { | ||
return VCStatusInvalidURL | ||
} | ||
|
||
u.Path = "/sdk/vimService.wsdl" | ||
apiURL := u.String() | ||
|
||
op.Debugf("Checking access to: %s", apiURL) | ||
|
||
for attempts := 5; errorCode != VCStatusOK && attempts > 0; attempts-- { | ||
|
||
c := http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
InsecureSkipVerify: true, | ||
}, | ||
}, | ||
// Is 20 seconds enough to receive any response from vSphere target server? | ||
Timeout: time.Second * 20, | ||
} | ||
errorCode = queryAPI(op, c.Get, apiURL) | ||
} | ||
return errorCode | ||
} | ||
|
||
func queryAPI(op trace.Operation, getter func(string) (*http.Response, error), apiURL string) int { | ||
resp, err := getter(apiURL) | ||
if err != nil { | ||
errTxt := err.Error() | ||
op.Errorf("Query error: %s", err) | ||
if strings.Contains(errTxt, "no such host") { | ||
return VCStatusUnknownHost | ||
} | ||
if strings.Contains(errTxt, "no route to host") { | ||
return VCStatusHostIsNotReachable | ||
} | ||
if strings.Contains(errTxt, "host is down") { | ||
return VCStatusHostIsNotReachable | ||
} | ||
return VCStatusErrorQuery | ||
} | ||
|
||
data := make([]byte, 65636) | ||
n, err := io.ReadFull(resp.Body, data) | ||
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { | ||
op.Errorf("Query error: %s", err) | ||
return VCStatusErrorResponse | ||
} | ||
if n >= len(data) { | ||
io.Copy(ioutil.Discard, resp.Body) | ||
} | ||
resp.Body.Close() | ||
|
||
contentType := strings.ToLower(resp.Header.Get("Content-Type")) | ||
if !strings.Contains(contentType, "text/xml") { | ||
op.Errorf("Unexpected content type %s, should be text/xml", contentType) | ||
op.Errorf("Response from the server: %s", string(data)) | ||
return VCStatusNotXML | ||
} | ||
// we just want to make sure that response contains something familiar that we could | ||
// use as vSphere API marker. | ||
if !bytes.Contains(data, []byte("urn:vim25Service")) { | ||
op.Errorf("Server response doesn't contain 'urn:vim25Service': %s", string(data)) | ||
return VCStatusIncorrectResponse | ||
} | ||
return VCStatusOK | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to actually check for errno's here instead of comparing error strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. I wish it was the case.