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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package apache
     4  
     5  import (
     6  	_ "embed"
     7  	"net/http"
     8  	"sync"
     9  	"time"
    10  
    11  	"github.com/netdata/go.d.plugin/agent/module"
    12  	"github.com/netdata/go.d.plugin/pkg/web"
    13  )
    14  
    15  //go:embed "config_schema.json"
    16  var configSchema string
    17  
    18  func init() {
    19  	module.Register("apache", module.Creator{
    20  		Create:          func() module.Module { return New() },
    21  		JobConfigSchema: configSchema,
    22  	})
    23  }
    24  
    25  func New() *Apache {
    26  	return &Apache{
    27  		Config: Config{
    28  			HTTP: web.HTTP{
    29  				Request: web.Request{
    30  					URL: "http://127.0.0.1/server-status?auto",
    31  				},
    32  				Client: web.Client{
    33  					Timeout: web.Duration{Duration: time.Second * 2},
    34  				},
    35  			},
    36  		},
    37  		charts: &module.Charts{},
    38  		once:   &sync.Once{},
    39  	}
    40  }
    41  
    42  type Config struct {
    43  	web.HTTP `yaml:",inline"`
    44  }
    45  
    46  type Apache struct {
    47  	module.Base
    48  
    49  	Config `yaml:",inline"`
    50  
    51  	charts *module.Charts
    52  
    53  	httpClient *http.Client
    54  	once       *sync.Once
    55  }
    56  
    57  func (a *Apache) Init() bool {
    58  	if err := a.verifyConfig(); err != nil {
    59  		a.Errorf("config validation: %v", err)
    60  		return false
    61  	}
    62  
    63  	httpClient, err := a.initHTTPClient()
    64  	if err != nil {
    65  		a.Errorf("init HTTP client: %v", err)
    66  		return false
    67  	}
    68  	a.httpClient = httpClient
    69  
    70  	a.Debugf("using URL %s", a.URL)
    71  	a.Debugf("using timeout: %s", a.Timeout.Duration)
    72  	return true
    73  }
    74  
    75  func (a *Apache) Check() bool {
    76  	return len(a.Collect()) > 0
    77  }
    78  
    79  func (a *Apache) Charts() *module.Charts {
    80  	return a.charts
    81  }
    82  
    83  func (a *Apache) Collect() map[string]int64 {
    84  	mx, err := a.collect()
    85  	if err != nil {
    86  		a.Error(err)
    87  	}
    88  
    89  	if len(mx) == 0 {
    90  		return nil
    91  	}
    92  	return mx
    93  }
    94  
    95  func (a *Apache) Cleanup() {
    96  	if a.httpClient != nil {
    97  		a.httpClient.CloseIdleConnections()
    98  	}
    99  }