diff --git a/modules/icinga2/api.go b/modules/icinga2/api.go deleted file mode 100644 index 7a4d2ba..0000000 --- a/modules/icinga2/api.go +++ /dev/null @@ -1,128 +0,0 @@ -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 //nolint:mnd - - // 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) //nolint:mnd - 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))), 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] -}