From ee3ced346cebf328d85ac52f56e91fc92620d0f4 Mon Sep 17 00:00:00 2001 From: Blerim Sheqa Date: Wed, 21 Dec 2016 17:23:49 +0100 Subject: [PATCH] Improve closing actions and connection error handling --- beater/icingabeat.go | 35 ++++++++++++----------------------- icingabeat.yml | 2 +- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/beater/icingabeat.go b/beater/icingabeat.go index 03f01ca6..8a13090a 100644 --- a/beater/icingabeat.go +++ b/beater/icingabeat.go @@ -56,19 +56,6 @@ func requestURL(bt *Icingabeat, method, path string) (*http.Response, error) { return response, err } -func connectionTest(icingabeat *Icingabeat) (bool, error) { - response, err := requestURL(icingabeat, "GET", "/v1") - - if err != nil { - return false, err - } - - logp.Debug("Connection test request URL:", response.Request.URL.String()) - logp.Debug("Connection test status:", response.Status) - - return true, nil -} - // New beater func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) { config := config.DefaultConfig @@ -87,16 +74,14 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) { func (bt *Icingabeat) Run(b *beat.Beat) error { logp.Info("icingabeat is running! Hit CTRL-C to stop it.") - bt.client = b.Publisher.Connect() - apiAvailable, connectionerr := connectionTest(bt) - for { ticker := time.NewTicker(2 * time.Second) - if apiAvailable { - logp.Info("API seems available") + response, responseErr := requestURL(bt, "POST", "/v1/events?queue=icingabeat&types=CheckResult") + if responseErr == nil { + + bt.client = b.Publisher.Connect() - response, _ := requestURL(bt, "POST", "/v1/events?queue=icingabeat&types=CheckResult") reader := bufio.NewReader(response.Body) bt.mutex.Lock() bt.closer = response.Body @@ -133,8 +118,14 @@ func (bt *Icingabeat) Run(b *beat.Beat) error { bt.client.PublishEvent(event) logp.Info("Event sent") } + + select { + case <-bt.done: + return nil + default: + } } else { - logp.Info("Cannot connect to API", connectionerr) + logp.Info("Error connecting to API:", responseErr) } select { @@ -143,18 +134,16 @@ func (bt *Icingabeat) Run(b *beat.Beat) error { case <-ticker.C: } } - //return nil } // Stop Icingabeat func (bt *Icingabeat) Stop() { - bt.client.Close() bt.mutex.Lock() if bt.closer != nil { bt.closer.Close() bt.closer = nil } bt.mutex.Unlock() + bt.client.Close() close(bt.done) - logp.Info("CTRL+C hit!!!") } diff --git a/icingabeat.yml b/icingabeat.yml index e26d48d4..6220991a 100644 --- a/icingabeat.yml +++ b/icingabeat.yml @@ -8,7 +8,7 @@ icingabeat: # Icinga 2 API endpoint host: "demo.icinga.com" # Port of Icinga 2 API - port: 5666 + port: 5665 # User for Icinga 2 API user: "root" # Password for the Icinga 2 API user