config.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sshcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver"
  4. import (
  5. "errors"
  6. "net"
  7. "strings"
  8. "go.opentelemetry.io/collector/receiver/scraperhelper"
  9. "go.uber.org/multierr"
  10. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/configssh"
  11. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sshcheckreceiver/internal/metadata"
  12. )
  13. // Predefined error responses for configuration validation failures
  14. var (
  15. errMissingEndpoint = errors.New(`"endpoint" not specified in config`)
  16. errInvalidEndpoint = errors.New(`"endpoint" is invalid`)
  17. errMissingUsername = errors.New(`"username" not specified in config`)
  18. errMissingPasswordAndKeyFile = errors.New(`either "password" or "key_file" is required`)
  19. errConfigNotSSHCheck = errors.New("config was not a SSH check receiver config")
  20. errWindowsUnsupported = errors.New(metadata.Type + " is unsupported on Windows.")
  21. )
  22. type Config struct {
  23. scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
  24. configssh.SSHClientSettings `mapstructure:",squash"`
  25. CheckSFTP bool `mapstructure:"check_sftp"`
  26. MetricsBuilderConfig metadata.MetricsBuilderConfig `mapstructure:",squash"`
  27. }
  28. // SFTPEnabled tells whether SFTP metrics are Enabled in MetricsSettings.
  29. func (c Config) SFTPEnabled() bool {
  30. return (c.CheckSFTP || c.MetricsBuilderConfig.Metrics.SshcheckSftpDuration.Enabled || c.MetricsBuilderConfig.Metrics.SshcheckSftpStatus.Enabled)
  31. }
  32. func (c Config) Validate() (err error) {
  33. if c.SSHClientSettings.Endpoint == "" {
  34. err = multierr.Append(err, errMissingEndpoint)
  35. } else if strings.Contains(c.SSHClientSettings.Endpoint, " ") {
  36. err = multierr.Append(err, errInvalidEndpoint)
  37. } else if _, _, splitErr := net.SplitHostPort(c.SSHClientSettings.Endpoint); splitErr != nil {
  38. err = multierr.Append(splitErr, errInvalidEndpoint)
  39. }
  40. if c.SSHClientSettings.Username == "" {
  41. err = multierr.Append(err, errMissingUsername)
  42. }
  43. if c.SSHClientSettings.Password == "" && c.KeyFile == "" {
  44. err = multierr.Append(err, errMissingPasswordAndKeyFile)
  45. }
  46. return
  47. }