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

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