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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package x509check
     4  
     5  import (
     6  	_ "embed"
     7  	"time"
     8  
     9  	"github.com/netdata/go.d.plugin/pkg/tlscfg"
    10  	"github.com/netdata/go.d.plugin/pkg/web"
    11  
    12  	cfssllog "github.com/cloudflare/cfssl/log"
    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  	cfssllog.Level = cfssllog.LevelFatal
    21  	module.Register("x509check", module.Creator{
    22  		JobConfigSchema: configSchema,
    23  		Defaults: module.Defaults{
    24  			UpdateEvery: 60,
    25  		},
    26  		Create: func() module.Module { return New() },
    27  	})
    28  }
    29  
    30  func New() *X509Check {
    31  	return &X509Check{
    32  		Config: Config{
    33  			Timeout:           web.Duration{Duration: time.Second * 2},
    34  			DaysUntilWarn:     14,
    35  			DaysUntilCritical: 7,
    36  		},
    37  	}
    38  }
    39  
    40  type Config struct {
    41  	Source            string
    42  	Timeout           web.Duration
    43  	tlscfg.TLSConfig  `yaml:",inline"`
    44  	DaysUntilWarn     int64 `yaml:"days_until_expiration_warning"`
    45  	DaysUntilCritical int64 `yaml:"days_until_expiration_critical"`
    46  	CheckRevocation   bool  `yaml:"check_revocation_status"`
    47  }
    48  
    49  type X509Check struct {
    50  	module.Base
    51  	Config `yaml:",inline"`
    52  	charts *module.Charts
    53  	prov   provider
    54  }
    55  
    56  func (x *X509Check) Init() bool {
    57  	if err := x.validateConfig(); err != nil {
    58  		x.Errorf("config validation: %v", err)
    59  		return false
    60  	}
    61  
    62  	prov, err := x.initProvider()
    63  	if err != nil {
    64  		x.Errorf("certificate provider init: %v", err)
    65  		return false
    66  	}
    67  	x.prov = prov
    68  
    69  	x.charts = x.initCharts()
    70  
    71  	return true
    72  }
    73  
    74  func (x *X509Check) Check() bool {
    75  	return len(x.Collect()) > 0
    76  }
    77  
    78  func (x *X509Check) Charts() *module.Charts {
    79  	return x.charts
    80  }
    81  
    82  func (x *X509Check) Collect() map[string]int64 {
    83  	mx, err := x.collect()
    84  	if err != nil {
    85  		x.Error(err)
    86  	}
    87  
    88  	if len(mx) == 0 {
    89  		return nil
    90  	}
    91  	return mx
    92  }
    93  
    94  func (x *X509Check) Cleanup() {}