-
Couldn't load subscription status.
- Fork 1
Collect data from the Icinga 2 API #109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ run: | |
| linters: | ||
| enable-all: true | ||
| disable: | ||
| - goimports | ||
| - cyclop | ||
| - depguard | ||
| - exhaustivestruct | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
| package icinga2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "github.com/NETWAYS/support-collector/internal/collection" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "path/filepath" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| type UserAuth struct { | ||
| Username string | ||
| Password string | ||
| } | ||
|
|
||
| // APICred saves the user and password. Provided as arguments | ||
| var APICred UserAuth | ||
|
|
||
| // APIEndpoints saves the FQDN or ip address for the endpoints, that will be collected. Provided as arguments. | ||
| var APIEndpoints []string | ||
|
|
||
| // InitAPICollection starts to collect data from the Icinga 2 API for given endpoints | ||
| func InitAPICollection(c *collection.Collection) error { | ||
| // return if no endpoints are provided | ||
| if len(APIEndpoints) == 0 { | ||
| return fmt.Errorf("0 API endpoints provided. No data will be collected from remote targets") | ||
| } | ||
|
|
||
| c.Log.Info("Start collection of Icinga 2 API endpoints") | ||
|
|
||
| // return if username or password is not provided | ||
| if APICred.Username == "" || APICred.Password == "" { | ||
| return fmt.Errorf("API Endpoints provided but username and/or password are missing") | ||
| } | ||
|
|
||
| for _, endpoint := range APIEndpoints { | ||
| // check if endpoint is reachable | ||
| err := endpointIsReachable(endpoint) | ||
| if err != nil { | ||
| c.Log.Warn(err) | ||
| continue | ||
| } | ||
|
|
||
| c.Log.Debugf("Endpoint '%s' is reachable", endpoint) | ||
|
|
||
| // collect /v1/status from endpoint | ||
| err = collectStatus(endpoint, c) | ||
| if err != nil { | ||
| c.Log.Warn(err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // endpointIsReachable checks if the given endpoint is reachable within 5 sec | ||
| func endpointIsReachable(endpoint string) error { | ||
| timeout := 5 * time.Second | ||
|
|
||
| // try to dial tcp connection within 5 seconds | ||
| conn, err := net.DialTimeout("tcp", endpoint, timeout) | ||
| if err != nil { | ||
| return fmt.Errorf("cant connect to endpoint '%s' within 5 seconds: %w", endpoint, err) | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // collectStatus requests $endpoint$/v1/status with APICred and saves the json result to file | ||
| func collectStatus(endpoint string, c *collection.Collection) error { | ||
| c.Log.Debugf("request data from endpoint '%s/v1/status'", endpoint) | ||
|
|
||
| // allow insecure connections because of Icinga 2 certificates and add proxy if one is defined in the environments | ||
| tr := &http.Transport{ | ||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, //nolint:gosec | ||
| Proxy: http.ProxyFromEnvironment, | ||
| } | ||
| client := &http.Client{Transport: tr} | ||
|
|
||
| // build context for request | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| // build request | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("https://%s/v1/status", endpoint), nil) | ||
| if err != nil { | ||
| return fmt.Errorf("cant build new request for '%s': %w", endpoint, err) | ||
| } | ||
|
|
||
| // set authentication for request | ||
| req.SetBasicAuth(APICred.Username, APICred.Password) | ||
|
|
||
| // make request | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return fmt.Errorf("cant requests status from '%s': %w", endpoint, err) | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return fmt.Errorf("cant read from response: %w", err) | ||
| } | ||
|
|
||
| // if response code is not '200 OK' throw error and return | ||
| if resp.Status != "200 OK" { | ||
| return fmt.Errorf("request failed with status code %s: %s", resp.Status, string(body)) | ||
| } | ||
|
|
||
| // add body to file | ||
| c.AddFileJSON(filepath.Join(ModuleName, fmt.Sprintf("api-v1_status_%s.json", extractHostname(endpoint))), string(body)) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // extractsHostname takes the endpoint and extract the hostname of it | ||
| func extractHostname(endpoint string) string { | ||
| splits := strings.Split(endpoint, ":") | ||
|
|
||
| return splits[0] | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.