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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package freeradius
     4  
     5  import (
     6  	_ "embed"
     7  	"errors"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/modules/freeradius/api"
    11  	"github.com/netdata/go.d.plugin/pkg/web"
    12  
    13  	"github.com/netdata/go.d.plugin/agent/module"
    14  )
    15  
    16  //go:embed "config_schema.json"
    17  var configSchema string
    18  
    19  func init() {
    20  	module.Register("freeradius", module.Creator{
    21  		JobConfigSchema: configSchema,
    22  		Create:          func() module.Module { return New() },
    23  	})
    24  }
    25  
    26  func New() *FreeRADIUS {
    27  	cfg := Config{
    28  		Address: "127.0.0.1",
    29  		Port:    18121,
    30  		Secret:  "adminsecret",
    31  		Timeout: web.Duration{Duration: time.Second},
    32  	}
    33  	return &FreeRADIUS{
    34  		Config: cfg,
    35  	}
    36  }
    37  
    38  type (
    39  	client interface {
    40  		Status() (*api.Status, error)
    41  	}
    42  	Config struct {
    43  		Address string
    44  		Port    int
    45  		Secret  string
    46  		Timeout web.Duration
    47  	}
    48  	FreeRADIUS struct {
    49  		module.Base
    50  		Config `yaml:",inline"`
    51  		client
    52  	}
    53  )
    54  
    55  func (f FreeRADIUS) validateConfig() error {
    56  	if f.Address == "" {
    57  		return errors.New("address not set")
    58  	}
    59  	if f.Port == 0 {
    60  		return errors.New("port not set")
    61  	}
    62  	if f.Secret == "" {
    63  		return errors.New("secret not set")
    64  	}
    65  	return nil
    66  }
    67  
    68  func (f *FreeRADIUS) initClient() {
    69  	f.client = api.New(api.Config{
    70  		Address: f.Address,
    71  		Port:    f.Port,
    72  		Secret:  f.Secret,
    73  		Timeout: f.Timeout.Duration,
    74  	})
    75  }
    76  
    77  func (f *FreeRADIUS) Init() bool {
    78  	err := f.validateConfig()
    79  	if err != nil {
    80  		f.Errorf("error on validating config: %v", err)
    81  		return false
    82  	}
    83  
    84  	f.initClient()
    85  	return true
    86  }
    87  
    88  func (f FreeRADIUS) Check() bool {
    89  	return len(f.Collect()) > 0
    90  }
    91  
    92  func (FreeRADIUS) Charts() *Charts {
    93  	return charts.Copy()
    94  }
    95  
    96  func (f *FreeRADIUS) Collect() map[string]int64 {
    97  	mx, err := f.collect()
    98  	if err != nil {
    99  		f.Error(err)
   100  	}
   101  
   102  	if len(mx) == 0 {
   103  		return nil
   104  	}
   105  	return mx
   106  }
   107  
   108  func (FreeRADIUS) Cleanup() {}