| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package bind |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 10 | ) |
| 11 | |
| 12 | type xml3Stats struct { |
| 13 | Server xml3Server `xml:"server"` |
| 14 | Views []xml3View `xml:"views>view"` |
| 15 | } |
| 16 | |
| 17 | type xml3Server struct { |
| 18 | CounterGroups []xml3CounterGroup `xml:"counters"` |
| 19 | } |
| 20 | |
| 21 | type xml3CounterGroup struct { |
| 22 | Type string `xml:"type,attr"` |
| 23 | Counters []struct { |
| 24 | Name string `xml:"name,attr"` |
| 25 | Value int64 `xml:",chardata"` |
| 26 | } `xml:"counter"` |
| 27 | } |
| 28 | |
| 29 | type xml3View struct { |
| 30 | Name string `xml:"name,attr"` |
| 31 | CounterGroups []xml3CounterGroup `xml:"counters"` |
| 32 | } |
| 33 | |
| 34 | func newXML3Client(client *http.Client, request web.RequestConfig) *xml3Client { |
| 35 | return &xml3Client{httpClient: client, request: request} |
| 36 | } |
| 37 | |
| 38 | type xml3Client struct { |
| 39 | httpClient *http.Client |
| 40 | request web.RequestConfig |
| 41 | } |
| 42 | |
| 43 | func (c xml3Client) serverStats() (*serverStats, error) { |
| 44 | req, err := web.NewHTTPRequestWithPath(c.request, "/server") |
| 45 | if err != nil { |
| 46 | return nil, fmt.Errorf("failed to create HTTP request: %v", err) |
| 47 | } |
| 48 | |
| 49 | var stats xml3Stats |
| 50 | |
| 51 | if err := web.DoHTTP(c.httpClient).RequestXML(req, &stats); err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | return convertXML(stats), nil |
| 56 | } |
| 57 | |
| 58 | func convertXML(xmlStats xml3Stats) *serverStats { |
| 59 | stats := serverStats{ |
| 60 | OpCodes: make(map[string]int64), |
| 61 | NSStats: make(map[string]int64), |
| 62 | QTypes: make(map[string]int64), |
| 63 | SockStats: make(map[string]int64), |
| 64 | Views: make(map[string]jsonView), |
| 65 | } |
| 66 | |
| 67 | var m map[string]int64 |
| 68 | |
| 69 | for _, group := range xmlStats.Server.CounterGroups { |
| 70 | switch group.Type { |
| 71 | default: |
| 72 | continue |
| 73 | case "opcode": |
| 74 | m = stats.OpCodes |
| 75 | case "qtype": |
| 76 | m = stats.QTypes |
| 77 | case "nsstat": |
| 78 | m = stats.NSStats |
| 79 | case "sockstat": |
| 80 | m = stats.SockStats |
| 81 | } |
| 82 | |
| 83 | for _, v := range group.Counters { |
| 84 | m[v.Name] = v.Value |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for _, view := range xmlStats.Views { |
| 89 | stats.Views[view.Name] = jsonView{ |
| 90 | Resolver: jsonViewResolver{ |
| 91 | Stats: make(map[string]int64), |
| 92 | QTypes: make(map[string]int64), |
| 93 | CacheStats: make(map[string]int64), |
| 94 | }, |
| 95 | } |
| 96 | for _, viewGroup := range view.CounterGroups { |
| 97 | switch viewGroup.Type { |
| 98 | default: |
| 99 | continue |
| 100 | case "resqtype": |
| 101 | m = stats.Views[view.Name].Resolver.QTypes |
| 102 | case "resstats": |
| 103 | m = stats.Views[view.Name].Resolver.Stats |
| 104 | case "cachestats": |
| 105 | m = stats.Views[view.Name].Resolver.CacheStats |
| 106 | } |
| 107 | for _, viewCounter := range viewGroup.Counters { |
| 108 | m[viewCounter.Name] = viewCounter.Value |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return &stats |
| 113 | } |