summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/activemq/apiclient.go
blob: fd790b88506d8d548ec27de396a3d64d30798a4b (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
// SPDX-License-Identifier: GPL-3.0-or-later

package activemq

import (
	"encoding/xml"
	"fmt"
	"net/http"

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

type topics struct {
	XMLName xml.Name `xml:"topics"`
	Items   []topic  `xml:"topic"`
}

type topic struct {
	XMLName xml.Name `xml:"topic"`
	Name    string   `xml:"name,attr"`
	Stats   stats    `xml:"stats"`
}

type queues struct {
	XMLName xml.Name `xml:"queues"`
	Items   []queue  `xml:"queue"`
}

type queue struct {
	XMLName xml.Name `xml:"queue"`
	Name    string   `xml:"name,attr"`
	Stats   stats    `xml:"stats"`
}

type stats struct {
	XMLName       xml.Name `xml:"stats"`
	Size          int64    `xml:"size,attr"`
	ConsumerCount int64    `xml:"consumerCount,attr"`
	EnqueueCount  int64    `xml:"enqueueCount,attr"`
	DequeueCount  int64    `xml:"dequeueCount,attr"`
}

const pathStats = "/%s/xml/%s.jsp"

func newAPIClient(client *http.Client, request web.RequestConfig, webadmin string) *apiClient {
	return &apiClient{
		httpClient: client,
		request:    request,
		webadmin:   webadmin,
	}
}

type apiClient struct {
	httpClient *http.Client
	request    web.RequestConfig
	webadmin   string
}

func (a *apiClient) getQueues() (*queues, error) {
	req, err := web.NewHTTPRequestWithPath(a.request, fmt.Sprintf(pathStats, a.webadmin, keyQueues))
	if err != nil {
		return nil, fmt.Errorf("failed to create HTTP request '%s': %v", a.request.URL, err)
	}

	var queues queues

	if err := web.DoHTTP(a.httpClient).RequestXML(req, &queues); err != nil {
		return nil, err
	}

	return &queues, nil
}

func (a *apiClient) getTopics() (*topics, error) {
	req, err := web.NewHTTPRequestWithPath(a.request, fmt.Sprintf(pathStats, a.webadmin, keyTopics))
	if err != nil {
		return nil, fmt.Errorf("failed to create HTTP request '%s': %v", a.request.URL, err)
	}

	var topics topics

	if err := web.DoHTTP(a.httpClient).RequestXML(req, &topics); err != nil {
		return nil, err
	}

	return &topics, nil
}