Skip to content

Commit 8affac7

Browse files
author
Philipp Dorschner
authored
Feature/threshold ranges (#15)
* Implement threshold ranges. * Update version, commit and date * Update Go version Fixes #14
1 parent 5491115 commit 8affac7

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Go
2020
uses: actions/setup-go@v2
2121
with:
22-
go-version: 1.16
22+
go-version: 1.19
2323

2424
- name: Test
2525
run: go test -v ./...

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ linters:
1717
- gosec
1818
- funlen
1919
- scopelint
20+
- varcheck
21+
- deadcode
22+
- structcheck

cmd/query.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ type QueryConfig struct {
1212
Query string
1313
MessageKey string
1414
MessageLen int
15-
Critical uint
16-
Warning uint
15+
Critical string
16+
Warning string
1717
}
1818

19-
var cliQueryConfig QueryConfig
19+
var (
20+
cliQueryConfig QueryConfig
21+
rc int
22+
)
2023

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

47-
rc := 3
50+
rc = check.Unknown
4851
output := fmt.Sprintf("Total hits: %d", total)
4952

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

60-
if total >= cliQueryConfig.Critical {
63+
crit, err := check.ParseThreshold(cliQueryConfig.Critical)
64+
if err != nil {
65+
check.ExitError(err)
66+
}
67+
68+
warn, err := check.ParseThreshold(cliQueryConfig.Warning)
69+
if err != nil {
70+
check.ExitError(err)
71+
}
72+
73+
if crit.DoesViolate(float64(total)) {
6174
rc = check.Critical
62-
} else if total >= cliQueryConfig.Warning {
75+
} else if warn.DoesViolate(float64(total)) {
6376
rc = check.Warning
64-
} else if total < cliQueryConfig.Warning {
77+
} else {
6578
rc = check.OK
6679
}
6780

6881
p := perfdata.PerfdataList{
69-
{Label: "total", Value: total,
70-
Warn: &check.Threshold{Upper: float64(cliQueryConfig.Warning)},
71-
Crit: &check.Threshold{Upper: float64(cliQueryConfig.Critical)},
72-
},
82+
{Label: "total", Value: total, Warn: warn, Crit: crit},
7383
}
7484

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

96106
fs.SortFlags = false

go.mod

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
module check_elasticsearch
22

3-
go 1.16
3+
go 1.19
44

55
require (
66
github.com/NETWAYS/go-check v0.3.0
7-
github.com/elastic/go-elasticsearch/v7 v7.17.1
7+
github.com/elastic/go-elasticsearch/v7 v7.17.7
88
github.com/spf13/cobra v1.6.1
99
)
10+
11+
require (
12+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
13+
github.com/mitchellh/go-ps v1.0.0 // indirect
14+
github.com/sirupsen/logrus v1.9.0 // indirect
15+
github.com/spf13/pflag v1.0.5 // indirect
16+
golang.org/x/sys v0.2.0 // indirect
17+
)

go.sum

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/elastic/go-elasticsearch/v7 v7.17.1 h1:49mHcHx7lpCL8cW1aioEwSEVKQF3s+Igi4Ye/QTWwmk=
8-
github.com/elastic/go-elasticsearch/v7 v7.17.1/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
7+
github.com/elastic/go-elasticsearch/v7 v7.17.7 h1:pcYNfITNPusl+cLwLN6OLmVT+F73Els0nbaWOmYachs=
8+
github.com/elastic/go-elasticsearch/v7 v7.17.7/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
99
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
1010
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1111
github.com/jarcoal/httpmock v1.1.0/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik=
@@ -14,8 +14,9 @@ github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN
1414
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1515
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1616
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
17-
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
1817
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
18+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
19+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1920
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
2021
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
2122
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
@@ -25,8 +26,10 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
2526
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2627
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2728
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28-
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
2929
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
31+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
32+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3033
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3134
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3235
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
)
77

88
var (
9-
version = "0.1.1"
10-
commit = "b344608"
11-
date = "20.04.2022"
9+
version = "0.2.0"
10+
commit = "f9eefa1"
11+
date = "25.11.2022"
1212
)
1313

1414
func main() {

0 commit comments

Comments
 (0)