Check API connection properly

This commit is contained in:
Blerim Sheqa 2016-12-21 14:49:18 +01:00
parent 09dfabda0b
commit 811e7afc94
3 changed files with 17661 additions and 63 deletions

View File

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@ -21,6 +20,7 @@ import (
// Icingabeat type // Icingabeat type
type Icingabeat struct { type Icingabeat struct {
done chan struct{}
config config.Config config config.Config
client publisher.Client client publisher.Client
@ -28,7 +28,7 @@ type Icingabeat struct {
mutex sync.Mutex mutex sync.Mutex
} }
func requestURL(icingabeat *Icingabeat, method, path string) *http.Response { func requestURL(bt *Icingabeat, method, path string) (*http.Response, error) {
transport := &http.Transport{ transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
} }
@ -37,35 +37,36 @@ func requestURL(icingabeat *Icingabeat, method, path string) *http.Response {
Transport: transport, Transport: transport,
} }
url := fmt.Sprintf("https://%s:%v%s", icingabeat.config.Host, icingabeat.config.Port, path) url := fmt.Sprintf("https://%s:%v%s", bt.config.Host, bt.config.Port, path)
request, err := http.NewRequest(method, url, nil) request, err := http.NewRequest(method, url, nil)
if err != nil { if err != nil {
log.Fatalln(err) logp.Info("Request:", err)
} }
request.Header.Add("Accept", "application/json") request.Header.Add("Accept", "application/json")
request.SetBasicAuth(icingabeat.config.User, icingabeat.config.Password) request.SetBasicAuth(bt.config.User, bt.config.Password)
response, err := client.Do(request) response, err := client.Do(request)
if err != nil { if err != nil {
log.Fatalln(err) return nil, err
} }
return response return response, err
} }
func apiStatus(icingabeat *Icingabeat) bool { func connectionTest(icingabeat *Icingabeat) (bool, error) {
response, err := requestURL(icingabeat, "GET", "/v1")
response := requestURL(icingabeat, "GET", "/v1/status") if err != nil {
return false, err
if response.StatusCode == 200 {
return true
} }
log.Println("Request:", response.Request.URL) logp.Debug("Connection test request URL:", response.Request.URL.String())
log.Fatalln("Error:", response.Status) logp.Debug("Connection test status:", response.Status)
return false
return true, nil
} }
// New beater // New beater
@ -76,6 +77,7 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
} }
bt := &Icingabeat{ bt := &Icingabeat{
done: make(chan struct{}),
config: config, config: config,
} }
return bt, nil return bt, nil
@ -86,14 +88,15 @@ 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() bt.client = b.Publisher.Connect()
apiAvailable, connectionerr := connectionTest(bt)
if apiStatus(bt) { for {
logp.Info("API is here!!!") ticker := time.NewTicker(2 * time.Second)
} else {
logp.Info("API not available")
}
response := requestURL(bt, "POST", "/v1/events?queue=icingabeat&types=CheckResult") if apiAvailable {
logp.Info("API seems available")
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
@ -130,8 +133,17 @@ func (bt *Icingabeat) Run(b *beat.Beat) error {
bt.client.PublishEvent(event) bt.client.PublishEvent(event)
logp.Info("Event sent") logp.Info("Event sent")
} }
} else {
logp.Info("Cannot connect to API", connectionerr)
}
select {
case <-bt.done:
return nil return nil
case <-ticker.C:
}
}
//return nil
} }
// Stop Icingabeat // Stop Icingabeat
@ -143,4 +155,6 @@ func (bt *Icingabeat) Stop() {
bt.closer = nil bt.closer = nil
} }
bt.mutex.Unlock() bt.mutex.Unlock()
close(bt.done)
logp.Info("CTRL+C hit!!!")
} }

View File

@ -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: 5665 port: 5666
# 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

File diff suppressed because it is too large Load Diff