| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package fluentd |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "path" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 12 | ) |
| 13 | |
| 14 | const pluginsPath = "/api/plugins.json" |
| 15 | |
| 16 | type pluginsInfo struct { |
| 17 | Payload []pluginData `json:"plugins"` |
| 18 | } |
| 19 | |
| 20 | type pluginData struct { |
| 21 | ID string `json:"plugin_id"` |
| 22 | Type string `json:"type"` |
| 23 | Category string `json:"plugin_category"` |
| 24 | RetryCount *int64 `json:"retry_count"` |
| 25 | BufferTotalQueuedSize *int64 `json:"buffer_total_queued_size"` |
| 26 | BufferQueueLength *int64 `json:"buffer_queue_length"` |
| 27 | } |
| 28 | |
| 29 | func (p pluginData) hasCategory() bool { |
| 30 | return p.RetryCount != nil |
| 31 | } |
| 32 | |
| 33 | func (p pluginData) hasBufferQueueLength() bool { |
| 34 | return p.BufferQueueLength != nil |
| 35 | } |
| 36 | |
| 37 | func (p pluginData) hasBufferTotalQueuedSize() bool { |
| 38 | return p.BufferTotalQueuedSize != nil |
| 39 | } |
| 40 | |
| 41 | func newAPIClient(client *http.Client, request web.RequestConfig) *apiClient { |
| 42 | return &apiClient{httpClient: client, request: request} |
| 43 | } |
| 44 | |
| 45 | type apiClient struct { |
| 46 | httpClient *http.Client |
| 47 | request web.RequestConfig |
| 48 | } |
| 49 | |
| 50 | func (a apiClient) getPluginsInfo() (*pluginsInfo, error) { |
| 51 | req, err := a.createRequest(pluginsPath) |
| 52 | if err != nil { |
| 53 | return nil, fmt.Errorf("error on creating request : %v", err) |
| 54 | } |
| 55 | |
| 56 | var info pluginsInfo |
| 57 | if err := web.DoHTTP(a.httpClient).RequestJSON(req, &info); err != nil { |
| 58 | return nil, fmt.Errorf("error on decoding request : %v", err) |
| 59 | } |
| 60 | |
| 61 | return &info, nil |
| 62 | } |
| 63 | |
| 64 | func (a apiClient) createRequest(urlPath string) (*http.Request, error) { |
| 65 | req := a.request.Copy() |
| 66 | u, err := url.Parse(req.URL) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | u.Path = path.Join(u.Path, urlPath) |
| 72 | req.URL = u.String() |
| 73 | return web.NewHTTPRequest(req) |
| 74 | } |