master
go 87 lines 1.92 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package activemq
4
5 import (
6 "encoding/xml"
7 "fmt"
8 "net/http"
9
10 "github.com/netdata/netdata/go/plugins/pkg/web"
11 )
12
13 type topics struct {
14 XMLName xml.Name `xml:"topics"`
15 Items []topic `xml:"topic"`
16 }
17
18 type topic struct {
19 XMLName xml.Name `xml:"topic"`
20 Name string `xml:"name,attr"`
21 Stats stats `xml:"stats"`
22 }
23
24 type queues struct {
25 XMLName xml.Name `xml:"queues"`
26 Items []queue `xml:"queue"`
27 }
28
29 type queue struct {
30 XMLName xml.Name `xml:"queue"`
31 Name string `xml:"name,attr"`
32 Stats stats `xml:"stats"`
33 }
34
35 type stats struct {
36 XMLName xml.Name `xml:"stats"`
37 Size int64 `xml:"size,attr"`
38 ConsumerCount int64 `xml:"consumerCount,attr"`
39 EnqueueCount int64 `xml:"enqueueCount,attr"`
40 DequeueCount int64 `xml:"dequeueCount,attr"`
41 }
42
43 const pathStats = "/%s/xml/%s.jsp"
44
45 func newAPIClient(client *http.Client, request web.RequestConfig, webadmin string) *apiClient {
46 return &apiClient{
47 httpClient: client,
48 request: request,
49 webadmin: webadmin,
50 }
51 }
52
53 type apiClient struct {
54 httpClient *http.Client
55 request web.RequestConfig
56 webadmin string
57 }
58
59 func (a *apiClient) getQueues() (*queues, error) {
60 req, err := web.NewHTTPRequestWithPath(a.request, fmt.Sprintf(pathStats, a.webadmin, keyQueues))
61 if err != nil {
62 return nil, fmt.Errorf("failed to create HTTP request '%s': %v", a.request.URL, err)
63 }
64
65 var queues queues
66
67 if err := web.DoHTTP(a.httpClient).RequestXML(req, &queues); err != nil {
68 return nil, err
69 }
70
71 return &queues, nil
72 }
73
74 func (a *apiClient) getTopics() (*topics, error) {
75 req, err := web.NewHTTPRequestWithPath(a.request, fmt.Sprintf(pathStats, a.webadmin, keyTopics))
76 if err != nil {
77 return nil, fmt.Errorf("failed to create HTTP request '%s': %v", a.request.URL, err)
78 }
79
80 var topics topics
81
82 if err := web.DoHTTP(a.httpClient).RequestXML(req, &topics); err != nil {
83 return nil, err
84 }
85
86 return &topics, nil
87 }