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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package geth
     4  
     5  import (
     6  	_ "embed"
     7  	"errors"
     8  	"time"
     9  
    10  	"github.com/netdata/go.d.plugin/pkg/prometheus"
    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("geth", module.Creator{
    21  		JobConfigSchema: configSchema,
    22  		Create:          func() module.Module { return New() },
    23  	})
    24  }
    25  
    26  func New() *Geth {
    27  	config := Config{
    28  		HTTP: web.HTTP{
    29  			Request: web.Request{
    30  				URL: "http://127.0.0.1:6060/debug/metrics/prometheus",
    31  			},
    32  			Client: web.Client{
    33  				Timeout: web.Duration{Duration: time.Second},
    34  			},
    35  		},
    36  	}
    37  
    38  	return &Geth{
    39  		Config: config,
    40  		charts: charts.Copy(),
    41  	}
    42  }
    43  
    44  type (
    45  	Config struct {
    46  		web.HTTP `yaml:",inline"`
    47  	}
    48  
    49  	Geth struct {
    50  		module.Base
    51  		Config `yaml:",inline"`
    52  
    53  		prom   prometheus.Prometheus
    54  		charts *Charts
    55  	}
    56  )
    57  
    58  func (g Geth) validateConfig() error {
    59  	if g.URL == "" {
    60  		return errors.New("URL is not set")
    61  	}
    62  	return nil
    63  }
    64  
    65  func (g *Geth) initClient() error {
    66  	client, err := web.NewHTTPClient(g.Client)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	g.prom = prometheus.New(client, g.Request)
    72  	return nil
    73  }
    74  
    75  func (g *Geth) Init() bool {
    76  	if err := g.validateConfig(); err != nil {
    77  		g.Errorf("error on validating config: %g", err)
    78  		return false
    79  	}
    80  	if err := g.initClient(); err != nil {
    81  		g.Errorf("error on initializing client: %g", err)
    82  		return false
    83  	}
    84  	return true
    85  }
    86  
    87  func (g *Geth) Check() bool {
    88  	return len(g.Collect()) > 0
    89  }
    90  
    91  func (g *Geth) Charts() *Charts {
    92  	return g.charts
    93  }
    94  
    95  func (g *Geth) Collect() map[string]int64 {
    96  	mx, err := g.collect()
    97  	if err != nil {
    98  		g.Error(err)
    99  	}
   100  
   101  	if len(mx) == 0 {
   102  		return nil
   103  	}
   104  	return mx
   105  }
   106  
   107  func (Geth) Cleanup() {}