| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package rspamd |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | |
| 8 | "github.com/netdata/netdata/go/plugins/pkg/stm" |
| 9 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 10 | ) |
| 11 | |
| 12 | type rspamdStats struct { |
| 13 | Version string `json:"version"` |
| 14 | ConfigId string `json:"config_id"` |
| 15 | Scanned *int64 `json:"scanned" stm:"scanned"` |
| 16 | Learned *int64 `json:"learned" stm:"learned"` |
| 17 | Actions struct { |
| 18 | Reject int64 `json:"reject" stm:"reject"` |
| 19 | SoftReject int64 `json:"soft reject" stm:"soft_reject"` |
| 20 | RewriteSubject int64 `json:"rewrite subject" stm:"rewrite_subject"` |
| 21 | AddHeader int64 `json:"add header" stm:"add_header"` |
| 22 | Greylist int64 `json:"greylist" stm:"greylist"` |
| 23 | NoAction int64 `json:"no action" stm:"no_action"` |
| 24 | InvalidMaxAction int64 `json:"invalid max action" stm:"invalid_max_action"` |
| 25 | Custom int64 `json:"custom" stm:"custom"` |
| 26 | Discard int64 `json:"discard" stm:"discard"` |
| 27 | Quarantine int64 `json:"quarantine" stm:"quarantine"` |
| 28 | UnknownAction int64 `json:"unknown action" stm:"unknown_action"` |
| 29 | } `json:"actions" stm:"actions"` |
| 30 | ScanTimes []float64 `json:"scan_times"` |
| 31 | SpamCount int64 `json:"spam_count" stm:"spam_count"` |
| 32 | HamCount int64 `json:"ham_count" stm:"ham_count"` |
| 33 | Connections int64 `json:"connections" stm:"connections"` |
| 34 | ControlConnections int64 `json:"control_connections" stm:"control_connections"` |
| 35 | FuzzyHashes map[string]int64 `json:"fuzzy_hashes"` |
| 36 | } |
| 37 | |
| 38 | func (c *Collector) collect() (map[string]int64, error) { |
| 39 | stats, err := c.queryRspamdStats() |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | |
| 44 | mx := stm.ToMap(stats) |
| 45 | |
| 46 | return mx, nil |
| 47 | } |
| 48 | |
| 49 | func (c *Collector) queryRspamdStats() (*rspamdStats, error) { |
| 50 | req, err := web.NewHTTPRequestWithPath(c.RequestConfig, "/stat") |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | var stats rspamdStats |
| 56 | if err := web.DoHTTP(c.httpClient).RequestJSON(req, &stats); err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | if stats.Scanned == nil || stats.Learned == nil { |
| 61 | return nil, fmt.Errorf("unexpected response: not rspamd data") |
| 62 | } |
| 63 | |
| 64 | return &stats, nil |
| 65 | } |