From 4f734b851d5e55d2ea6ebe0f758376c85427c1e1 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Mon, 18 Sep 2023 08:41:16 +0200 Subject: [PATCH 1/3] Update contrib --- contrib/icinga2-commands.conf | 63 ++++++---------------------- contrib/icinga2-service-example.conf | 8 +--- 2 files changed, 14 insertions(+), 57 deletions(-) diff --git a/contrib/icinga2-commands.conf b/contrib/icinga2-commands.conf index fc5d961..ab42a6c 100644 --- a/contrib/icinga2-commands.conf +++ b/contrib/icinga2-commands.conf @@ -27,21 +27,21 @@ object CheckCommand "influxdb" { order = 1 description = "Timeout for the check" } - "--tls" = { + "--secure" = { set_if = "$influxdb_tls$" order = 1 - description = "Use secure connection" + description = "Use a HTTPS connection" } "--insecure" = { set_if = "$influxdb_insecure$" order = 1 - description = "Allow use of self signed certificates when using SSL" + description = "Skip the verification of the TLS certificate" } "--token" = { value = "$influxdb_token$" required = true order = 1 - description = "The token which allows access to the API" + description = "Token for server authentication" } "--org" = { value = "$influxdb_org$" @@ -57,48 +57,15 @@ object CheckCommand "influxdb" { set_if = {{ macro("$influxdb_query$") == true }} description = "The bucket where time series data is stored" } - "--start" = { - value = "$influxdb_start$" - order = 4 - description = "Specifies a start time range for your query" - } - "--end" = { - value = "$influxdb_end$" - order = 4 - description = "Specifies the end of a time range for your query" - } - "--measurement" = { - value = "$influxdb_measurement$" - order = 5 - description = "The data stored in the associated fields, e.g. 'disk'" - } - "--field" = { - value = "$influxdb_field$" - order = 6 - description = "The key-value pair that records metadata and the actual data value" - } - "--filter" = { - value = "$influxdb_filter$" - order = 7 - repeat_key = true - description = "Add a key=value filter to the query, e.g. 'hostname=example.com'" - } - "--raw-filter" = { - value = "$influxdb_raw_filter$" - order = 8 - repeat_key = true - description = "A fully customizable filter which will be added to the query." - } - "--aggregation" = { - value = "$influxdb_aggregation$" - order = 9 - description = "Function that returns an aggregated value across a set of points" - } - "--raw-query" = { - value = "$influxdb_raw_query$" - description = "An InfluxQL query which will be performed. Note: Only ONE value result will be evaluated" - } - "--value-by-key" = { + "--flux-query" = { + value = "$influxdb_flux_query$" + description = "Flux script as string" + } + "--flux-file" = { + value = "$influxdb_flux_string$" + description = "Path to flux file" + } + "--perfdata-label-by-key" = { value = "$influxdb_value_by_key$" description = "Sets the label for the perfdata of the given column key for the record" } @@ -106,10 +73,6 @@ object CheckCommand "influxdb" { value = "$influxdb_perfdata_label$" description = "Sets as custom label for the perfdata" } - "--verbose" = { - set_if = "$influxdb_verbose$" - description = "Display verbose output" - } "--critical" = { value = "$influxdb_critical$" description = "The critical threshold for a value" diff --git a/contrib/icinga2-service-example.conf b/contrib/icinga2-service-example.conf index 0079dfc..cfcbd1e 100644 --- a/contrib/icinga2-service-example.conf +++ b/contrib/icinga2-service-example.conf @@ -7,13 +7,7 @@ apply Service "influx_query" { vars.influxdb_token = "example_token" vars.influxdb_bucket = "example_bucket" vars.influxdb_org = "example_org" - vars.influxdb_start = "-48h" - vars.influxdb_measurement = "example_measurement" - vars.influxdb_field = "value" - vars.influxdb_filter = [ "metric=example_metric" ] - vars.influxdb_raw_filter = [ "group(columns: [\"hostname\"], mode: \"by\")", "aggregateWindow(every: 30m, fn: sum)" ] - vars.influxdb_aggregation = "last" - vars.influxdb_value_by_key = "hostname" + vars.influxdb_flux_query = "flux..." vars.influxdb_insecure = true vars.influxdb_critical = 100 vars.influxdb_warning = 50 From a6519f9a59db78ba1ba16a605463dbd139b2cef0 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Mon, 18 Sep 2023 08:51:47 +0200 Subject: [PATCH 2/3] Bumo to go 1.21 --- .github/workflows/build.yml | 2 +- .github/workflows/golangci-lint.yml | 2 ++ .golangci.yml | 1 + go.mod | 2 +- go.sum | 1 + 5 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d87907f..00fc675 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: 1.20 + go-version: 1.21 - name: Test run: go test -v ./... diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index d8d6fed..03ba16d 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -16,3 +16,5 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 diff --git a/.golangci.yml b/.golangci.yml index d2a23d6..e46cb5e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,6 +16,7 @@ linters: - forbidigo - gci - lll + - godot - gofumpt - gochecknoinits - gochecknoglobals diff --git a/go.mod b/go.mod index a08b350..d2cf25e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/NETWAYS/check_influxdb -go 1.20 +go 1.21 require ( github.com/NETWAYS/go-check v0.5.0 diff --git a/go.sum b/go.sum index bcc7b20..3808b82 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,7 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= From aa9336ecb8ac7c1ae90302cd7c188e2639bed777 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Mon, 18 Sep 2023 08:52:18 +0200 Subject: [PATCH 3/3] Add some more comments --- cmd/config.go | 2 ++ cmd/health.go | 3 +++ cmd/query.go | 2 ++ internal/api/api.go | 1 + internal/client/client.go | 3 ++- 5 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/config.go b/cmd/config.go index 575e01a..f8837dd 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -15,6 +15,7 @@ import ( "github.com/NETWAYS/go-check" ) +// Central Configuration for CLI type Config struct { BasicAuth string Hostname string @@ -85,6 +86,7 @@ func (c *Config) NewClient() *client.Client { return client.NewClient(u.String(), c.Token, c.Organization, rt) } +// Central timeout configuration for anything that needs it func (c *Config) timeoutContext() (context.Context, func()) { return context.WithTimeout(context.Background(), 10*time.Second) } diff --git a/cmd/health.go b/cmd/health.go index f993254..c7429f5 100644 --- a/cmd/health.go +++ b/cmd/health.go @@ -52,6 +52,8 @@ API translation: err error ) + // Uses the major version to determine which API to call. + // Can be extended in the future. switch apiversion.MajorVersion { case 1: health, err = checkV2Health(c.URL, c.Token, c.Client) @@ -65,6 +67,7 @@ API translation: check.ExitError(err) } + // Is this flexible enough? Might be better to use strings.Contains. switch health { case "pass": rc = 0 diff --git a/cmd/query.go b/cmd/query.go index 37ab2c7..202194f 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -195,6 +195,8 @@ var queryCmd = &cobra.Command{ check.ExitError(versionErr) } + // Uses the major version to determine which API to call. + // Can be extended in the future. switch apiversion.MajorVersion { case 2: queryFluxV2(fluxQuery, c.URL, c.Organization, c.Token, c.Client) diff --git a/internal/api/api.go b/internal/api/api.go index 8e5a575..2b462a1 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -7,6 +7,7 @@ import ( "strings" ) +// APIVersion is mainly for identifying the API Version of InfluxDB type APIVersion struct { Version string `json:"version"` MajorVersion int diff --git a/internal/client/client.go b/internal/client/client.go index 6119360..d3ff246 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -11,6 +11,8 @@ import ( "github.com/NETWAYS/check_influxdb/internal/api" ) +// Client is a small wrapper for HTTP connections, +// that is configured and used in the subcommands. type Client struct { Organization string URL string @@ -19,7 +21,6 @@ type Client struct { } func NewClient(url, token, org string, rt http.RoundTripper) *Client { - // Small wrapper c := &http.Client{ Transport: rt, }