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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package nvme
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/agent/module"
    10  	"github.com/netdata/go.d.plugin/pkg/web"
    11  )
    12  
    13  //go:embed "config_schema.json"
    14  var configSchema string
    15  
    16  func init() {
    17  	module.Register("nvme", module.Creator{
    18  		JobConfigSchema: configSchema,
    19  		Defaults: module.Defaults{
    20  			UpdateEvery: 10,
    21  		},
    22  		Create: func() module.Module { return New() },
    23  	})
    24  }
    25  
    26  func New() *NVMe {
    27  	return &NVMe{
    28  		Config: Config{
    29  			BinaryPath: "nvme",
    30  			Timeout:    web.Duration{Duration: time.Second * 2},
    31  		},
    32  		charts:           &module.Charts{},
    33  		devicePaths:      make(map[string]bool),
    34  		listDevicesEvery: time.Minute * 10,
    35  	}
    36  
    37  }
    38  
    39  type Config struct {
    40  	Timeout    web.Duration
    41  	BinaryPath string `yaml:"binary_path"`
    42  }
    43  
    44  type (
    45  	NVMe struct {
    46  		module.Base
    47  		Config `yaml:",inline"`
    48  
    49  		charts *module.Charts
    50  
    51  		exec nvmeCLI
    52  
    53  		devicePaths      map[string]bool
    54  		listDevicesTime  time.Time
    55  		listDevicesEvery time.Duration
    56  		forceListDevices bool
    57  	}
    58  	nvmeCLI interface {
    59  		list() (*nvmeDeviceList, error)
    60  		smartLog(devicePath string) (*nvmeDeviceSmartLog, error)
    61  	}
    62  )
    63  
    64  func (n *NVMe) Init() bool {
    65  	if err := n.validateConfig(); err != nil {
    66  		n.Errorf("config validation: %v", err)
    67  		return false
    68  	}
    69  
    70  	v, err := n.initNVMeCLIExec()
    71  	if err != nil {
    72  		n.Errorf("init nvme-cli exec: %v", err)
    73  		return false
    74  	}
    75  	n.exec = v
    76  
    77  	return true
    78  }
    79  
    80  func (n *NVMe) Check() bool {
    81  	return len(n.Collect()) > 0
    82  }
    83  
    84  func (n *NVMe) Charts() *module.Charts {
    85  	return n.charts
    86  }
    87  
    88  func (n *NVMe) Collect() map[string]int64 {
    89  	mx, err := n.collect()
    90  	if err != nil {
    91  		n.Error(err)
    92  	}
    93  
    94  	if len(mx) == 0 {
    95  		return nil
    96  	}
    97  	return mx
    98  }
    99  
   100  func (n *NVMe) Cleanup() {}