master
go 78 lines 1.74 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package squid
4
5 import (
6 "bufio"
7 "fmt"
8 "io"
9 "strconv"
10 "strings"
11
12 "github.com/netdata/netdata/go/plugins/pkg/web"
13 )
14
15 const (
16 // https://wiki.squid-cache.org/Features/CacheManager/Index#controlling-access-to-the-cache-manager
17 urlPathServerStats = "/squid-internal-mgr/counters"
18 )
19
20 var statsCounters = map[string]bool{
21 "client_http.kbytes_in": true,
22 "client_http.kbytes_out": true,
23 "server.all.errors": true,
24 "server.all.requests": true,
25 "server.all.kbytes_out": true,
26 "server.all.kbytes_in": true,
27 "client_http.errors": true,
28 "client_http.hits": true,
29 "client_http.requests": true,
30 "client_http.hit_kbytes_out": true,
31 }
32
33 func (c *Collector) collect() (map[string]int64, error) {
34 mx := make(map[string]int64)
35
36 if err := c.collectCounters(mx); err != nil {
37 return nil, err
38 }
39
40 return mx, nil
41 }
42
43 func (c *Collector) collectCounters(mx map[string]int64) error {
44 req, err := web.NewHTTPRequestWithPath(c.RequestConfig, urlPathServerStats)
45 if err != nil {
46 return fmt.Errorf("failed to create '%s' request: %w", urlPathServerStats, err)
47 }
48
49 return web.DoHTTP(c.httpClient).Request(req, func(body io.Reader) error {
50 sc := bufio.NewScanner(body)
51
52 for sc.Scan() {
53 key, value, ok := strings.Cut(sc.Text(), "=")
54 if !ok {
55 continue
56 }
57
58 key, value = strings.TrimSpace(key), strings.TrimSpace(value)
59
60 if !statsCounters[key] {
61 continue
62 }
63
64 v, err := strconv.ParseInt(value, 10, 64)
65 if err != nil {
66 c.Debugf("failed to parse key %s value %s: %v", key, value, err)
67 continue
68 }
69
70 mx[key] = v
71 }
72
73 if len(mx) == 0 {
74 return fmt.Errorf("unexpected response from '%s': no metrics found", req.URL)
75 }
76 return nil
77 })
78 }