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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  //go:build linux
     4  // +build linux
     5  
     6  package logind
     7  
     8  import (
     9  	_ "embed"
    10  	"time"
    11  
    12  	"github.com/netdata/go.d.plugin/agent/module"
    13  	"github.com/netdata/go.d.plugin/pkg/web"
    14  )
    15  
    16  //go:embed "config_schema.json"
    17  var configSchema string
    18  
    19  func init() {
    20  	module.Register("logind", module.Creator{
    21  		JobConfigSchema: configSchema,
    22  		Defaults: module.Defaults{
    23  			Priority: 59999, // copied from the python collector
    24  		},
    25  		Create: func() module.Module { return New() },
    26  	})
    27  }
    28  
    29  func New() *Logind {
    30  	return &Logind{
    31  		Config: Config{
    32  			Timeout: web.Duration{Duration: time.Second * 2},
    33  		},
    34  		newLogindConn: func(cfg Config) (logindConnection, error) {
    35  			return newLogindConnection(cfg.Timeout.Duration)
    36  		},
    37  		charts: charts.Copy(),
    38  	}
    39  }
    40  
    41  type Config struct {
    42  	Timeout web.Duration `yaml:"timeout"`
    43  }
    44  
    45  type Logind struct {
    46  	module.Base
    47  	Config `yaml:",inline"`
    48  
    49  	newLogindConn func(config Config) (logindConnection, error)
    50  	conn          logindConnection
    51  	charts        *module.Charts
    52  }
    53  
    54  func (l *Logind) Init() bool {
    55  	return true
    56  }
    57  
    58  func (l *Logind) Check() bool {
    59  	return len(l.Collect()) > 0
    60  }
    61  
    62  func (l *Logind) Charts() *module.Charts {
    63  	return l.charts
    64  }
    65  
    66  func (l *Logind) Collect() map[string]int64 {
    67  	mx, err := l.collect()
    68  	if err != nil {
    69  		l.Error(err)
    70  	}
    71  
    72  	if len(mx) == 0 {
    73  		return nil
    74  	}
    75  	return mx
    76  }
    77  
    78  func (l *Logind) Cleanup() {
    79  	if l.conn != nil {
    80  		l.conn.Close()
    81  	}
    82  }