| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nagios |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | _ "embed" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/framework/vnodes" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/pathvalidate" |
| 14 | "github.com/netdata/netdata/go/plugins/plugin/scripts.d/pkg/timeperiod" |
| 15 | ) |
| 16 | |
| 17 | //go:embed config_schema.json |
| 18 | var configSchema string |
| 19 | |
| 20 | //go:embed charts.yaml |
| 21 | var nagiosChartTemplateV2 string |
| 22 | |
| 23 | func init() { |
| 24 | collectorapi.Register("nagios", collectorapi.Creator{ |
| 25 | JobConfigSchema: configSchema, |
| 26 | Defaults: collectorapi.Defaults{ |
| 27 | UpdateEvery: defaultCollectorUpdateEvery, |
| 28 | }, |
| 29 | CreateV2: func() collectorapi.CollectorV2 { return New() }, |
| 30 | Config: func() any { return &Config{} }, |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | // Config is the public v2 config surface. |
| 35 | type Config struct { |
| 36 | UpdateEvery int `yaml:"update_every,omitempty" json:"update_every,omitempty"` |
| 37 | AutoDetectEvery int `yaml:"autodetection_retry,omitempty" json:"autodetection_retry,omitempty"` |
| 38 | JobConfig `yaml:",inline" json:",inline"` |
| 39 | TimePeriods []timeperiod.Config `yaml:"time_periods,omitempty" json:"time_periods,omitempty"` |
| 40 | Notes string `yaml:"notes,omitempty" json:"notes,omitempty"` |
| 41 | DirectorySource string `yaml:"__directory_source__,omitempty" json:"-"` |
| 42 | } |
| 43 | |
| 44 | // Collector is the v2 Nagios collector. |
| 45 | type Collector struct { |
| 46 | collectorapi.Base |
| 47 | Config `yaml:",inline" json:",inline"` |
| 48 | |
| 49 | store metrix.CollectorStore |
| 50 | router *perfdataRouter |
| 51 | runner checkRunner |
| 52 | validatePlugin func(string) (string, error) |
| 53 | now func() time.Time |
| 54 | vnode vnodes.VirtualNode |
| 55 | |
| 56 | job compiledJob |
| 57 | state collectState |
| 58 | |
| 59 | cadenceWarning string |
| 60 | } |
| 61 | |
| 62 | func New() *Collector { |
| 63 | return &Collector{ |
| 64 | Config: Config{ |
| 65 | UpdateEvery: defaultCollectorUpdateEvery, |
| 66 | JobConfig: defaultedJobConfig(JobConfig{}), |
| 67 | }, |
| 68 | store: metrix.NewCollectorStore(), |
| 69 | router: newPerfdataRouter(defaultPerfdataMetricKeyBudget), |
| 70 | runner: systemCheckRunner{}, |
| 71 | validatePlugin: pathvalidate.ValidateBinaryPath, |
| 72 | now: time.Now, |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (c *Collector) Configuration() any { return c.Config } |
| 77 | |
| 78 | func (c *Collector) VirtualNode() *vnodes.VirtualNode { return &c.vnode } |
| 79 | |
| 80 | func (c *Collector) Init(context.Context) error { return c.initCollector() } |
| 81 | |
| 82 | func (c *Collector) Check(context.Context) error { return c.checkCollector() } |
| 83 | |
| 84 | func (c *Collector) Collect(ctx context.Context) error { return c.collect(ctx) } |
| 85 | |
| 86 | func (c *Collector) Cleanup(context.Context) {} |
| 87 | |
| 88 | func (c *Collector) MetricStore() metrix.CollectorStore { return c.store } |
| 89 | |
| 90 | func (c *Collector) ChartTemplateYAML() string { return nagiosChartTemplateV2 } |