forked from netdata/netdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,101 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## All available configuration options, their descriptions and default values: | ||
## https://github.com/netdata/netdata/tree/master/src/go/collectors/go.d.plugin/modules/rspamd#readme | ||
|
||
#jobs: | ||
# - name: local | ||
# url: http://127.0.0.1:11334 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package rspamd | ||
|
||
import "github.com/netdata/netdata/go/go.d.plugin/agent/module" | ||
|
||
const ( | ||
prioClassifications = module.Priority + iota | ||
prioActions | ||
prioScans | ||
prioLearns | ||
prioConnections | ||
prioControlConnections | ||
) | ||
|
||
var charts = module.Charts{ | ||
classificationsChartTmpl.Copy(), | ||
|
||
actionsChart.Copy(), | ||
|
||
scanChartTmpl.Copy(), | ||
learnChartTmpl.Copy(), | ||
|
||
connectionsChartTmpl.Copy(), | ||
controlConnectionsChartTmpl.Copy(), | ||
} | ||
|
||
var ( | ||
classificationsChartTmpl = module.Chart{ | ||
ID: "classifications", | ||
Title: "Classifications", | ||
Units: "messages/s", | ||
Fam: "classification", | ||
Ctx: "rspamd.classifications", | ||
Type: module.Stacked, | ||
Priority: prioClassifications, | ||
Dims: module.Dims{ | ||
{ID: "ham_count", Name: "ham", Algo: module.Incremental}, | ||
{ID: "spam_count", Name: "spam", Algo: module.Incremental}, | ||
}, | ||
} | ||
|
||
actionsChart = module.Chart{ | ||
ID: "actions", | ||
Title: "Actions", | ||
Units: "messages/s", | ||
Fam: "actions", | ||
Ctx: "rspamd.actions", | ||
Type: module.Stacked, | ||
Priority: prioActions, | ||
Dims: module.Dims{ | ||
{ID: "actions_reject", Name: "reject", Algo: module.Incremental}, | ||
{ID: "actions_soft_reject", Name: "soft_reject", Algo: module.Incremental}, | ||
{ID: "actions_rewrite_subject", Name: "rewrite_subject", Algo: module.Incremental}, | ||
{ID: "actions_add_header", Name: "add_header", Algo: module.Incremental}, | ||
{ID: "actions_greylist", Name: "greylist", Algo: module.Incremental}, | ||
{ID: "actions_custom", Name: "custom", Algo: module.Incremental}, | ||
{ID: "actions_discard", Name: "discard", Algo: module.Incremental}, | ||
{ID: "actions_quarantine", Name: "quarantine", Algo: module.Incremental}, | ||
{ID: "actions_no_action", Name: "no_action", Algo: module.Incremental}, | ||
}, | ||
} | ||
|
||
scanChartTmpl = module.Chart{ | ||
ID: "scans", | ||
Title: "Scanned messages", | ||
Units: "messages/s", | ||
Fam: "training", | ||
Ctx: "rspamd.scans", | ||
Priority: prioScans, | ||
Dims: module.Dims{ | ||
{ID: "scanned", Name: "scanned", Algo: module.Incremental}, | ||
}, | ||
} | ||
|
||
learnChartTmpl = module.Chart{ | ||
ID: "learns", | ||
Title: "Learned messages", | ||
Units: "messages/s", | ||
Fam: "training", | ||
Ctx: "rspamd.learns", | ||
Priority: prioLearns, | ||
Dims: module.Dims{ | ||
{ID: "learned", Name: "learned", Algo: module.Incremental}, | ||
}, | ||
} | ||
|
||
connectionsChartTmpl = module.Chart{ | ||
ID: "connections", | ||
Title: "Connections", | ||
Units: "connections/s", | ||
Fam: "connections", | ||
Ctx: "rspamd.connections", | ||
Priority: prioConnections, | ||
Dims: module.Dims{ | ||
{ID: "connections", Name: "connections", Algo: module.Incremental}, | ||
}, | ||
} | ||
controlConnectionsChartTmpl = module.Chart{ | ||
ID: "control_connections", | ||
Title: "Control connections", | ||
Units: "connections/s", | ||
Fam: "connections", | ||
Ctx: "rspamd.control_connections", | ||
Priority: prioControlConnections, | ||
Dims: module.Dims{ | ||
{ID: "control_connections", Name: "control_connections", Algo: module.Incremental}, | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
package rspamd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/netdata/netdata/go/go.d.plugin/pkg/stm" | ||
"github.com/netdata/netdata/go/go.d.plugin/pkg/web" | ||
) | ||
|
||
type rspamdStats struct { | ||
Version string `json:"version"` | ||
ConfigId string `json:"config_id"` | ||
Scanned int64 `json:"scanned" stm:"scanned"` | ||
Learned int64 `json:"learned" stm:"learned"` | ||
Actions struct { | ||
Reject int64 `json:"reject" stm:"reject"` | ||
SoftReject int64 `json:"soft reject" stm:"soft_reject"` | ||
RewriteSubject int64 `json:"rewrite subject" stm:"rewrite_subject"` | ||
AddHeader int64 `json:"add header" stm:"add_header"` | ||
Greylist int64 `json:"greylist" stm:"greylist"` | ||
NoAction int64 `json:"no action" stm:"no_action"` | ||
InvalidMaxAction int64 `json:"invalid max action" stm:"invalid_max_action"` | ||
Custom int64 `json:"custom" stm:"custom"` | ||
Discard int64 `json:"discard" stm:"discard"` | ||
Quarantine int64 `json:"quarantine" stm:"quarantine"` | ||
UnknownAction int64 `json:"unknown action" stm:"unknown_action"` | ||
} `json:"actions" stm:"actions"` | ||
ScanTimes []float64 `json:"scan_times"` | ||
SpamCount int64 `json:"spam_count" stm:"spam_count"` | ||
HamCount int64 `json:"ham_count" stm:"ham_count"` | ||
Connections int64 `json:"connections" stm:"connections"` | ||
ControlConnections int64 `json:"control_connections" stm:"control_connections"` | ||
FuzzyHashes map[string]int64 `json:"fuzzy_hashes"` | ||
} | ||
|
||
func (r *Rspamd) collect() (map[string]int64, error) { | ||
stats, err := r.queryRspamdStats() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mx := stm.ToMap(stats) | ||
|
||
return mx, nil | ||
} | ||
|
||
func (r *Rspamd) queryRspamdStats() (*rspamdStats, error) { | ||
req, err := web.NewHTTPRequest(r.Request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.URL.Path = "/stat" | ||
|
||
var stats rspamdStats | ||
if err := r.doOKDecode(req, &stats); err != nil { | ||
return nil, err | ||
} | ||
|
||
if stats.Version == "" || stats.ConfigId == "" || len(stats.FuzzyHashes) == 0 { | ||
return nil, fmt.Errorf("unexpected response: not rspamd data") | ||
} | ||
|
||
return &stats, nil | ||
} | ||
|
||
func (r *Rspamd) doOKDecode(req *http.Request, in interface{}) error { | ||
resp, err := r.httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("error on HTTP request '%s': %v", req.URL, err) | ||
} | ||
defer closeBody(resp) | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("'%s' returned HTTP status code: %d", req.URL, resp.StatusCode) | ||
} | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(in); err != nil { | ||
return fmt.Errorf("error on decoding response from '%s': %v", req.URL, err) | ||
} | ||
return nil | ||
} | ||
|
||
func closeBody(resp *http.Response) { | ||
if resp != nil && resp.Body != nil { | ||
_, _ = io.Copy(io.Discard, resp.Body) | ||
_ = resp.Body.Close() | ||
} | ||
} |
Oops, something went wrong.