github.com/netdata/go.d.plugin@v0.58.1/modules/scaleio/scaleio.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package scaleio
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/modules/scaleio/client"
    10  	"github.com/netdata/go.d.plugin/pkg/web"
    11  
    12  	"github.com/netdata/go.d.plugin/agent/module"
    13  )
    14  
    15  //go:embed "config_schema.json"
    16  var configSchema string
    17  
    18  func init() {
    19  	module.Register("scaleio", module.Creator{
    20  		JobConfigSchema: configSchema,
    21  		Create:          func() module.Module { return New() },
    22  	})
    23  }
    24  
    25  // New creates ScaleIO with default values.
    26  func New() *ScaleIO {
    27  	config := Config{
    28  		HTTP: web.HTTP{
    29  			Request: web.Request{
    30  				URL: "https://127.0.0.1",
    31  			},
    32  			Client: web.Client{
    33  				Timeout: web.Duration{Duration: time.Second},
    34  			},
    35  		},
    36  	}
    37  	return &ScaleIO{
    38  		Config:  config,
    39  		charts:  systemCharts.Copy(),
    40  		charted: make(map[string]bool),
    41  	}
    42  }
    43  
    44  type (
    45  	// Config is the ScaleIO module configuration.
    46  	Config struct {
    47  		web.HTTP `yaml:",inline"`
    48  	}
    49  	// ScaleIO ScaleIO module.
    50  	ScaleIO struct {
    51  		module.Base
    52  		Config `yaml:",inline"`
    53  		client *client.Client
    54  		charts *module.Charts
    55  
    56  		discovered instances
    57  		charted    map[string]bool
    58  
    59  		lastDiscoveryOK bool
    60  		runs            int
    61  	}
    62  	instances struct {
    63  		sdc  map[string]client.Sdc
    64  		pool map[string]client.StoragePool
    65  	}
    66  )
    67  
    68  // Init makes initialization.
    69  func (s *ScaleIO) Init() bool {
    70  	if s.Username == "" || s.Password == "" {
    71  		s.Error("username and password aren't set")
    72  		return false
    73  	}
    74  
    75  	c, err := client.New(s.Client, s.Request)
    76  	if err != nil {
    77  		s.Errorf("error on creating ScaleIO client: %v", err)
    78  		return false
    79  	}
    80  	s.client = c
    81  
    82  	s.Debugf("using URL %s", s.URL)
    83  	s.Debugf("using timeout: %s", s.Timeout.Duration)
    84  	return true
    85  }
    86  
    87  // Check makes check.
    88  func (s *ScaleIO) Check() bool {
    89  	if err := s.client.Login(); err != nil {
    90  		s.Error(err)
    91  		return false
    92  	}
    93  	return len(s.Collect()) > 0
    94  }
    95  
    96  // Charts returns Charts.
    97  func (s *ScaleIO) Charts() *module.Charts {
    98  	return s.charts
    99  }
   100  
   101  // Collect collects metrics.
   102  func (s *ScaleIO) Collect() map[string]int64 {
   103  	mx, err := s.collect()
   104  	if err != nil {
   105  		s.Error(err)
   106  		return nil
   107  	}
   108  
   109  	if len(mx) == 0 {
   110  		return nil
   111  	}
   112  	return mx
   113  }
   114  
   115  // Cleanup makes cleanup.
   116  func (s *ScaleIO) Cleanup() {
   117  	if s.client == nil {
   118  		return
   119  	}
   120  	_ = s.client.Logout()
   121  }