icingabeat/beater/icingabeat.go

71 lines
1.3 KiB
Go
Raw Permalink Normal View History

package beater
import (
"fmt"
2021-09-22 16:58:34 +02:00
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"
"github.com/icinga/icingabeat/config"
)
// Icingabeat type
type Icingabeat struct {
2016-12-21 14:49:18 +01:00
done chan struct{}
config config.Config
2017-10-17 10:23:16 +02:00
client beat.Client
}
var target_key = "icinga."
// New beater
func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
config := config.DefaultConfig
if err := cfg.Unpack(&config); err != nil {
return nil, fmt.Errorf("Error reading config file: %v", err)
}
bt := &Icingabeat{
2016-12-21 14:49:18 +01:00
done: make(chan struct{}),
config: config,
}
return bt, nil
}
// Run Icingabeat
func (bt *Icingabeat) Run(b *beat.Beat) error {
2016-12-22 13:40:14 +01:00
logp.Info("icingabeat is running! Hit CTRL-C to stop it.")
2017-10-17 10:23:16 +02:00
var err error
bt.client, err = b.Publisher.Connect()
if err != nil {
return err
}
2016-12-21 14:49:18 +01:00
if len(bt.config.Eventstream.Types) > 0 {
var eventstream *Eventstream
eventstream = NewEventstream(bt, bt.config)
go eventstream.Run()
}
if bt.config.Statuspoller.Interval > 0 {
var statuspoller *Statuspoller
statuspoller = NewStatuspoller(bt, bt.config)
go statuspoller.Run()
}
2016-12-22 13:40:14 +01:00
for {
2016-12-21 14:49:18 +01:00
select {
case <-bt.done:
return nil
}
}
}
// Stop Icingabeat
func (bt *Icingabeat) Stop() {
bt.client.Close()
2016-12-21 14:49:18 +01:00
close(bt.done)
}