| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package exim |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "bytes" |
| 8 | "fmt" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | func (c *Collector) collect() (map[string]int64, error) { |
| 14 | resp, err := c.exec.countMessagesInQueue() |
| 15 | if err != nil { |
| 16 | return nil, err |
| 17 | } |
| 18 | |
| 19 | emails, err := parseResponse(resp) |
| 20 | if err != nil { |
| 21 | return nil, err |
| 22 | } |
| 23 | |
| 24 | mx := map[string]int64{ |
| 25 | "emails": emails, |
| 26 | } |
| 27 | |
| 28 | return mx, nil |
| 29 | } |
| 30 | |
| 31 | func parseResponse(resp []byte) (int64, error) { |
| 32 | sc := bufio.NewScanner(bytes.NewReader(resp)) |
| 33 | sc.Scan() |
| 34 | |
| 35 | line := strings.TrimSpace(sc.Text()) |
| 36 | |
| 37 | emails, err := strconv.ParseInt(line, 10, 64) |
| 38 | if err != nil { |
| 39 | return 0, fmt.Errorf("invalid response '%s': %v", line, err) |
| 40 | } |
| 41 | |
| 42 | return emails, nil |
| 43 | } |