| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package x509check |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
| 12 | cfssllog "github.com/cloudflare/cfssl/log" |
| 13 | |
| 14 | "github.com/netdata/netdata/go/plugins/pkg/confopt" |
| 15 | "github.com/netdata/netdata/go/plugins/pkg/tlscfg" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 17 | ) |
| 18 | |
| 19 | //go:embed "config_schema.json" |
| 20 | var configSchema string |
| 21 | |
| 22 | func init() { |
| 23 | cfssllog.Level = cfssllog.LevelFatal |
| 24 | collectorapi.Register("x509check", collectorapi.Creator{ |
| 25 | JobConfigSchema: configSchema, |
| 26 | Defaults: collectorapi.Defaults{ |
| 27 | UpdateEvery: 60, |
| 28 | }, |
| 29 | Create: func() collectorapi.CollectorV1 { return New() }, |
| 30 | Config: func() any { return &Config{} }, |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | func New() *Collector { |
| 35 | return &Collector{ |
| 36 | Config: Config{ |
| 37 | Timeout: confopt.Duration(time.Second * 2), |
| 38 | CheckFullChain: false, |
| 39 | }, |
| 40 | |
| 41 | charts: &collectorapi.Charts{}, |
| 42 | seenCerts: make(map[string]bool), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | type Config struct { |
| 47 | Vnode string `yaml:"vnode,omitempty" json:"vnode"` |
| 48 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"` |
| 49 | Source string `yaml:"source" json:"source"` |
| 50 | Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"` |
| 51 | CheckFullChain bool `yaml:"check_full_chain" json:"check_full_chain"` |
| 52 | CheckRevocation bool `yaml:"check_revocation_status" json:"check_revocation_status"` |
| 53 | tlscfg.TLSConfig `yaml:",inline" json:""` |
| 54 | } |
| 55 | |
| 56 | type Collector struct { |
| 57 | collectorapi.Base |
| 58 | Config `yaml:",inline" json:""` |
| 59 | |
| 60 | charts *collectorapi.Charts |
| 61 | |
| 62 | prov provider |
| 63 | |
| 64 | seenCerts map[string]bool |
| 65 | } |
| 66 | |
| 67 | func (c *Collector) Configuration() any { |
| 68 | return c.Config |
| 69 | } |
| 70 | |
| 71 | func (c *Collector) Init(context.Context) error { |
| 72 | if err := c.validateConfig(); err != nil { |
| 73 | return fmt.Errorf("config validation: %v", err) |
| 74 | } |
| 75 | |
| 76 | prov, err := c.initProvider() |
| 77 | if err != nil { |
| 78 | return fmt.Errorf("certificate provider init: %v", err) |
| 79 | } |
| 80 | c.prov = prov |
| 81 | |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | func (c *Collector) Check(context.Context) error { |
| 86 | mx, err := c.collect() |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | if len(mx) == 0 { |
| 91 | return errors.New("no metrics collected") |
| 92 | } |
| 93 | return nil |
| 94 | } |
| 95 | |
| 96 | func (c *Collector) Charts() *collectorapi.Charts { |
| 97 | return c.charts |
| 98 | } |
| 99 | |
| 100 | func (c *Collector) Collect(context.Context) map[string]int64 { |
| 101 | mx, err := c.collect() |
| 102 | if err != nil { |
| 103 | c.Error(err) |
| 104 | } |
| 105 | |
| 106 | if len(mx) == 0 { |
| 107 | return nil |
| 108 | } |
| 109 | return mx |
| 110 | } |
| 111 | |
| 112 | func (c *Collector) Cleanup(context.Context) {} |