Skip to content

Commit

Permalink
use common code for nagios threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Ermakov committed Feb 5, 2025
1 parent 8e000e4 commit 52820ab
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 194 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ go 1.22.7
require (
github.com/prometheus/client_golang v1.20.5
github.com/prometheus/common v0.62.0
github.com/sardinasystems/sensu-go-check-common v0.0.0-20250205120347-ebe0a44b4bbb
github.com/sensu/core/v2 v2.20.0
github.com/sensu/sensu-plugin-sdk v0.19.0
github.com/stretchr/testify v1.10.0
)

require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/echlebek/timeproxy v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand All @@ -29,7 +28,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/robertkrimen/otto v0.5.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsF
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sardinasystems/sensu-go-check-common v0.0.0-20250205120347-ebe0a44b4bbb h1:NVELET38v4brjYH/16X+Kuy1XNtIdcxZuEfXxioxY78=
github.com/sardinasystems/sensu-go-check-common v0.0.0-20250205120347-ebe0a44b4bbb/go.mod h1:Wq2gx5l1v+aa/l74sLiQHek1i916lkHa/OpZkJsZaqQ=
github.com/sensu/core/v2 v2.20.0 h1:QJQbqZiXSQ2dUE8LUvwUcKaqxHjYYCQwJ1Mil0cap4U=
github.com/sensu/core/v2 v2.20.0/go.mod h1:fzr3qioLAHyLsBfq9I6ELVjL01NUNjqmQx4kj2tbDLo=
github.com/sensu/sensu-api-tools v0.2.1 h1:wjT5AFdboQ3jDwzBMcD38VJEwwWhPIIuReZrCi9Ezos=
Expand Down
60 changes: 25 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import (
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/sardinasystems/sensu-go-check-common/nagios"
corev2 "github.com/sensu/core/v2"
"github.com/sensu/sensu-plugin-sdk/sensu"

"github.com/sardinasystems/sensu-go-prometheus-metric-check/utils"
)

// Config represents the check plugin config.
type Config struct {
sensu.PluginConfig
PrometheusURL string
Query string
WarningStr string
CriticalStr string
CriticalThreshold utils.NagiosThreshold
WarningThreshold utils.NagiosThreshold
CriticalThreshold nagios.Threshold
WarningThreshold nagios.Threshold
EmitPerfdata bool
Name string
DebugQuery bool
Expand Down Expand Up @@ -60,23 +57,27 @@ var (
Usage: "PromQL query",
Value: &plugin.Query,
},
&sensu.PluginConfigOption[string]{
Path: "warning",
Env: "PROMETHEUS_WARNING",
Argument: "warning",
Shorthand: "w",
Default: "",
Usage: "Warning level",
Value: &plugin.WarningStr,
&nagios.ThresholdConfigOption{
Option: sensu.PluginConfigOption[string]{
Path: "warning",
Env: "PROMETHEUS_WARNING",
Argument: "warning",
Shorthand: "w",
Default: "",
Usage: "Warning level",
},
Value: &plugin.WarningThreshold,
},
&sensu.PluginConfigOption[string]{
Path: "critical",
Env: "PROMETHEUS_CRITICAL",
Argument: "critical",
Shorthand: "c",
Default: "",
Usage: "Critical level",
Value: &plugin.CriticalStr,
&nagios.ThresholdConfigOption{
Option: sensu.PluginConfigOption[string]{
Path: "critical",
Env: "PROMETHEUS_CRITICAL",
Argument: "critical",
Shorthand: "c",
Default: "",
Usage: "Critical level",
},
Value: &plugin.CriticalThreshold,
},
&sensu.PluginConfigOption[bool]{
Path: "emit_perfdata",
Expand Down Expand Up @@ -133,17 +134,6 @@ func main() {
}

func checkArgs(event *corev2.Event) (int, error) {
var err error

plugin.WarningThreshold, err = utils.ParseThreshold(plugin.WarningStr)
if err != nil {
return sensu.CheckStateCritical, fmt.Errorf("--warning error: %v", err)
}

plugin.CriticalThreshold, err = utils.ParseThreshold(plugin.CriticalStr)
if err != nil {
return sensu.CheckStateCritical, fmt.Errorf("--critical error: %v", err)
}

if plugin.Name == "" {
qn := fmt.Sprintf("query_%s", plugin.Query)
Expand Down Expand Up @@ -210,10 +200,10 @@ func executeCheck(event *corev2.Event) (int, error) {

if !math.IsNaN(value) {
if crit {
fmt.Printf("CRITICAL: %s is %f which is out of %s", plugin.Name, value, plugin.CriticalStr)
fmt.Printf("CRITICAL: %s is %f which is out of %s", plugin.Name, value, plugin.CriticalThreshold.String())
state = sensu.CheckStateCritical
} else if warn {
fmt.Printf("WARNING: %s is %f which is out of %s", plugin.Name, value, plugin.WarningStr)
fmt.Printf("WARNING: %s is %f which is out of %s", plugin.Name, value, plugin.WarningThreshold.String())
state = sensu.CheckStateWarning
} else {
fmt.Printf("OK: %s is %f", plugin.Name, value)
Expand Down
77 changes: 0 additions & 77 deletions utils/nagios_threshold.go

This file was deleted.

79 changes: 0 additions & 79 deletions utils/nagios_threshold_test.go

This file was deleted.

0 comments on commit 52820ab

Please sign in to comment.