Improve closing actions and connection error handling

This commit is contained in:
Blerim Sheqa 2016-12-21 17:23:49 +01:00
parent 811e7afc94
commit ee3ced346c
2 changed files with 13 additions and 24 deletions

View File

@ -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!!!")
}

View File

@ -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