summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/logstash/collect.go
blob: 3eceb9bf61bb3d01dfa37ada7c98617d695b9063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-3.0-or-later

package logstash

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"

	"github.com/netdata/netdata/go/go.d.plugin/pkg/stm"
	"github.com/netdata/netdata/go/go.d.plugin/pkg/web"
)

const urlPathNodeStatsAPI = "/_node/stats"

func (l *Logstash) collect() (map[string]int64, error) {
	stats, err := l.queryNodeStats()
	if err != nil {
		return nil, err
	}

	l.updateCharts(stats.Pipelines)

	return stm.ToMap(stats), nil
}

func (l *Logstash) updateCharts(pipelines map[string]pipelineStats) {
	seen := make(map[string]bool)

	for id := range pipelines {
		seen[id] = true
		if !l.pipelines[id] {
			l.pipelines[id] = true
			l.addPipelineCharts(id)
		}
	}

	for id := range l.pipelines {
		if !seen[id] {
			delete(l.pipelines, id)
			l.removePipelineCharts(id)
		}
	}
}

func (l *Logstash) queryNodeStats() (*nodeStats, error) {
	req, _ := web.NewHTTPRequest(l.Request.Copy())
	req.URL.Path = urlPathNodeStatsAPI

	var stats nodeStats

	if err := l.doWithDecode(&stats, req); err != nil {
		return nil, err
	}

	return &stats, nil
}

func (l *Logstash) doWithDecode(dst interface{}, req *http.Request) error {
	l.Debugf("executing %s '%s'", req.Method, req.URL)
	resp, err := l.httpClient.Do(req)
	if err != nil {
		return err
	}
	defer closeBody(resp)

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("%s returned %d status code (%s)", req.URL, resp.StatusCode, resp.Status)
	}

	content, err := io.ReadAll(resp.Body)
	if err != nil {
		return fmt.Errorf("error on reading response from %s : %v", req.URL, err)
	}

	if err := json.Unmarshal(content, dst); err != nil {
		return fmt.Errorf("error on parsing response from %s : %v", req.URL, err)
	}

	return nil
}

func closeBody(resp *http.Response) {
	if resp != nil && resp.Body != nil {
		_, _ = io.Copy(io.Discard, resp.Body)
		_ = resp.Body.Close()
	}
}