2016-12-02 11:51:44 +01:00
|
|
|
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"
|
2016-12-02 11:51:44 +01:00
|
|
|
|
|
|
|
"github.com/icinga/icingabeat/config"
|
|
|
|
)
|
|
|
|
|
2016-12-08 17:19:25 +01:00
|
|
|
// Icingabeat type
|
2016-12-02 11:51:44 +01:00
|
|
|
type Icingabeat struct {
|
2016-12-21 14:49:18 +01:00
|
|
|
done chan struct{}
|
2016-12-02 11:51:44 +01:00
|
|
|
config config.Config
|
2017-10-17 10:23:16 +02:00
|
|
|
client beat.Client
|
2016-12-08 17:19:25 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 14:36:18 +01:00
|
|
|
var target_key = "icinga."
|
|
|
|
|
2016-12-08 17:19:25 +01:00
|
|
|
// New beater
|
2016-12-02 11:51:44 +01:00
|
|
|
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{}),
|
2016-12-02 11:51:44 +01:00
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
return bt, nil
|
|
|
|
}
|
|
|
|
|
2016-12-08 17:19:25 +01:00
|
|
|
// Run Icingabeat
|
2016-12-02 11:51:44 +01:00
|
|
|
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
|
|
|
|
2017-01-03 10:05:23 +01:00
|
|
|
if len(bt.config.Eventstream.Types) > 0 {
|
2017-01-03 16:36:39 +01:00
|
|
|
var eventstream *Eventstream
|
2017-01-03 10:05:23 +01:00
|
|
|
eventstream = NewEventstream(bt, bt.config)
|
|
|
|
go eventstream.Run()
|
|
|
|
}
|
2016-12-02 11:51:44 +01:00
|
|
|
|
2017-01-03 16:36:39 +01:00
|
|
|
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
|
2016-12-02 11:51:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 17:19:25 +01:00
|
|
|
// Stop Icingabeat
|
2016-12-02 11:51:44 +01:00
|
|
|
func (bt *Icingabeat) Stop() {
|
2016-12-21 17:23:49 +01:00
|
|
|
bt.client.Close()
|
2016-12-21 14:49:18 +01:00
|
|
|
close(bt.done)
|
2016-12-02 11:51:44 +01:00
|
|
|
}
|