Skip to content

Commit

Permalink
Feature/threshold ranges (#15)
Browse files Browse the repository at this point in the history
* Implement threshold ranges.
* Update version, commit and date
* Update Go version

Fixes #14
  • Loading branch information
Philipp Dorschner authored Nov 28, 2022
1 parent 5491115 commit 8affac7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.19

- name: Test
run: go test -v ./...
Expand Down
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ linters:
- gosec
- funlen
- scopelint
- varcheck
- deadcode
- structcheck
36 changes: 23 additions & 13 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ type QueryConfig struct {
Query string
MessageKey string
MessageLen int
Critical uint
Warning uint
Critical string
Warning string
}

var cliQueryConfig QueryConfig
var (
cliQueryConfig QueryConfig
rc int
)

var queryCmd = &cobra.Command{
Use: "query",
Expand Down Expand Up @@ -44,7 +47,7 @@ https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-
check.ExitError(err)
}

rc := 3
rc = check.Unknown
output := fmt.Sprintf("Total hits: %d", total)

if len(messages) > 0 {
Expand All @@ -57,19 +60,26 @@ https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-
}
}

if total >= cliQueryConfig.Critical {
crit, err := check.ParseThreshold(cliQueryConfig.Critical)
if err != nil {
check.ExitError(err)
}

warn, err := check.ParseThreshold(cliQueryConfig.Warning)
if err != nil {
check.ExitError(err)
}

if crit.DoesViolate(float64(total)) {
rc = check.Critical
} else if total >= cliQueryConfig.Warning {
} else if warn.DoesViolate(float64(total)) {
rc = check.Warning
} else if total < cliQueryConfig.Warning {
} else {
rc = check.OK
}

p := perfdata.PerfdataList{
{Label: "total", Value: total,
Warn: &check.Threshold{Upper: float64(cliQueryConfig.Warning)},
Crit: &check.Threshold{Upper: float64(cliQueryConfig.Critical)},
},
{Label: "total", Value: total, Warn: warn, Crit: crit},
}

check.ExitRaw(rc, output, "|", p.String())
Expand All @@ -88,9 +98,9 @@ func init() {
"Message of messagekey to display")
fs.IntVarP(&cliQueryConfig.MessageLen, "msglen", "m", 80,
"Number of characters to display in latest message")
fs.UintVarP(&cliQueryConfig.Warning, "warning", "w", 20,
fs.StringVarP(&cliQueryConfig.Warning, "warning", "w", "20",
"Warning threshold for total hits")
fs.UintVarP(&cliQueryConfig.Critical, "critical", "c", 50,
fs.StringVarP(&cliQueryConfig.Critical, "critical", "c", "50",
"Critical threshold for total hits")

fs.SortFlags = false
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
module check_elasticsearch

go 1.16
go 1.19

require (
github.com/NETWAYS/go-check v0.3.0
github.com/elastic/go-elasticsearch/v7 v7.17.1
github.com/elastic/go-elasticsearch/v7 v7.17.7
github.com/spf13/cobra v1.6.1
)

require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.2.0 // indirect
)
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elastic/go-elasticsearch/v7 v7.17.1 h1:49mHcHx7lpCL8cW1aioEwSEVKQF3s+Igi4Ye/QTWwmk=
github.com/elastic/go-elasticsearch/v7 v7.17.1/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
github.com/elastic/go-elasticsearch/v7 v7.17.7 h1:pcYNfITNPusl+cLwLN6OLmVT+F73Els0nbaWOmYachs=
github.com/elastic/go-elasticsearch/v7 v7.17.7/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jarcoal/httpmock v1.1.0/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
Expand All @@ -14,8 +14,9 @@ github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand All @@ -25,8 +26,10 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

var (
version = "0.1.1"
commit = "b344608"
date = "20.04.2022"
version = "0.2.0"
commit = "f9eefa1"
date = "25.11.2022"
)

func main() {
Expand Down

0 comments on commit 8affac7

Please sign in to comment.