| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package samba |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "bytes" |
| 8 | "errors" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | func (c *Collector) collect() (map[string]int64, error) { |
| 14 | bs, err := c.exec.profile() |
| 15 | if err != nil { |
| 16 | return nil, err |
| 17 | } |
| 18 | |
| 19 | mx := make(map[string]int64) |
| 20 | |
| 21 | if err := c.collectSmbStatusProfile(mx, bs); err != nil { |
| 22 | return nil, err |
| 23 | } |
| 24 | |
| 25 | c.once.Do(func() { |
| 26 | c.addCharts(mx) |
| 27 | }) |
| 28 | |
| 29 | return mx, nil |
| 30 | } |
| 31 | |
| 32 | func (c *Collector) collectSmbStatusProfile(mx map[string]int64, profileData []byte) error { |
| 33 | sc := bufio.NewScanner(bytes.NewReader(profileData)) |
| 34 | |
| 35 | for sc.Scan() { |
| 36 | line := strings.TrimSpace(sc.Text()) |
| 37 | |
| 38 | switch { |
| 39 | case strings.HasPrefix(line, "syscall_"): |
| 40 | case strings.HasPrefix(line, "smb2_"): |
| 41 | default: |
| 42 | continue |
| 43 | } |
| 44 | |
| 45 | key, value, ok := strings.Cut(line, ":") |
| 46 | if !ok { |
| 47 | c.Debugf("failed to parse line: '%s'", line) |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | key, value = strings.TrimSpace(key), strings.TrimSpace(value) |
| 52 | |
| 53 | if !strings.HasSuffix(key, "count") && !strings.HasSuffix(key, "bytes") { |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | v, err := strconv.ParseInt(value, 10, 64) |
| 58 | if err != nil { |
| 59 | c.Debugf("failed to parse value in '%s': %v", line, err) |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | mx[key] = v |
| 64 | } |
| 65 | |
| 66 | if len(mx) == 0 { |
| 67 | return errors.New("unexpected smbstatus profile response: no metrics found") |
| 68 | } |
| 69 | |
| 70 | return nil |
| 71 | } |