mirror of
https://github.com/Icinga/icingabeat.git
synced 2025-07-27 15:54:01 +02:00
Improve closing actions and connection error handling
This commit is contained in:
parent
811e7afc94
commit
ee3ced346c
@ -56,19 +56,6 @@ func requestURL(bt *Icingabeat, method, path string) (*http.Response, error) {
|
|||||||
return response, err
|
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
|
// New beater
|
||||||
func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
|
func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
|
||||||
config := config.DefaultConfig
|
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 {
|
func (bt *Icingabeat) Run(b *beat.Beat) error {
|
||||||
logp.Info("icingabeat is running! Hit CTRL-C to stop it.")
|
logp.Info("icingabeat is running! Hit CTRL-C to stop it.")
|
||||||
|
|
||||||
bt.client = b.Publisher.Connect()
|
|
||||||
apiAvailable, connectionerr := connectionTest(bt)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
ticker := time.NewTicker(2 * time.Second)
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
|
|
||||||
if apiAvailable {
|
response, responseErr := requestURL(bt, "POST", "/v1/events?queue=icingabeat&types=CheckResult")
|
||||||
logp.Info("API seems available")
|
if responseErr == nil {
|
||||||
|
|
||||||
|
bt.client = b.Publisher.Connect()
|
||||||
|
|
||||||
response, _ := requestURL(bt, "POST", "/v1/events?queue=icingabeat&types=CheckResult")
|
|
||||||
reader := bufio.NewReader(response.Body)
|
reader := bufio.NewReader(response.Body)
|
||||||
bt.mutex.Lock()
|
bt.mutex.Lock()
|
||||||
bt.closer = response.Body
|
bt.closer = response.Body
|
||||||
@ -133,8 +118,14 @@ func (bt *Icingabeat) Run(b *beat.Beat) error {
|
|||||||
bt.client.PublishEvent(event)
|
bt.client.PublishEvent(event)
|
||||||
logp.Info("Event sent")
|
logp.Info("Event sent")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-bt.done:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logp.Info("Cannot connect to API", connectionerr)
|
logp.Info("Error connecting to API:", responseErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@ -143,18 +134,16 @@ func (bt *Icingabeat) Run(b *beat.Beat) error {
|
|||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop Icingabeat
|
// Stop Icingabeat
|
||||||
func (bt *Icingabeat) Stop() {
|
func (bt *Icingabeat) Stop() {
|
||||||
bt.client.Close()
|
|
||||||
bt.mutex.Lock()
|
bt.mutex.Lock()
|
||||||
if bt.closer != nil {
|
if bt.closer != nil {
|
||||||
bt.closer.Close()
|
bt.closer.Close()
|
||||||
bt.closer = nil
|
bt.closer = nil
|
||||||
}
|
}
|
||||||
bt.mutex.Unlock()
|
bt.mutex.Unlock()
|
||||||
|
bt.client.Close()
|
||||||
close(bt.done)
|
close(bt.done)
|
||||||
logp.Info("CTRL+C hit!!!")
|
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ icingabeat:
|
|||||||
# Icinga 2 API endpoint
|
# Icinga 2 API endpoint
|
||||||
host: "demo.icinga.com"
|
host: "demo.icinga.com"
|
||||||
# Port of Icinga 2 API
|
# Port of Icinga 2 API
|
||||||
port: 5666
|
port: 5665
|
||||||
# User for Icinga 2 API
|
# User for Icinga 2 API
|
||||||
user: "root"
|
user: "root"
|
||||||
# Password for the Icinga 2 API user
|
# Password for the Icinga 2 API user
|
||||||
|
Loading…
x
Reference in New Issue
Block a user