Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.24
go-version: stable
check-latest: true

- name: Test
run: go test -v ./...
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea
.idea/
check_influxdb
vendor/
coverage.html
Expand Down
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ builds:
- CGO_ENABLED=0
goarch:
- amd64
- arm64
goos:
- linux
- windows
Expand All @@ -30,6 +31,7 @@ archives:
{{- if eq .Os "windows" }}Windows{{ end }}
{{- if eq .Os "darwin" }}Darwin{{ end }}
{{- if eq .Arch "amd64" }}_x86_64{{ end }}
{{- if eq .Arch "arm64" }}_arm64{{ end }}
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/check_influxdb.iml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Global Flags:
--key-file string Specify the Key File for TLS authentication (CHECK_INFLUXDB_KEY_FILE)
-i, --insecure Skip the verification of the server's TLS certificate
-t, --timeout int Timeout in seconds for the CheckPlugin (default 30)
--header strings Additional HTTP header to include in the request. Can be used multiple times.
Keys and values are separated by a colon (--header "X-Custom: example").
-h, --help help for check_influxdb
-v, --version version for check_influxdb
```
Expand Down
15 changes: 15 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
KeyFile string `env:"CHECK_INFLUXDB_KEY_FILE"`
Token string `env:"CHECK_INFLUXDB_TOKEN"`
Organization string `env:"CHECK_INFLUXDB_ORGANISATION"`
Headers []string
Port int
Insecure bool
Secure bool
Expand Down Expand Up @@ -82,6 +83,20 @@ func (c *Config) NewClient() *client.Client {
rt = checkhttpconfig.NewBasicAuthRoundTripper(u, p, rt)
}

// If extra headers are set, parse them and add them to the request
if len(c.Headers) > 0 {
headers := make(map[string]string)

for _, h := range c.Headers {
head := strings.Split(h, ":")
if len(head) == 2 {
headers[strings.TrimSpace(head[0])] = strings.TrimSpace(head[1])
}
}

rt = client.NewHeadersRoundTripper(headers, rt)
}

return client.NewClient(u.String(), c.Token, c.Organization, rt)
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ func TestHealthCmd(t *testing.T) {
args: []string{"run", "../main.go", "health"},
expected: "[UNKNOWN] - InfluxDB Version 3 not supported (*errors.errorString)\nexit status 3\n",
},
{
name: "health-extra-header",
httpreturn: func(w http.ResponseWriter, r *http.Request) {
foobar := r.Header.Get("X-Foobar")
if foobar == "Barfoo" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"checks":[],"message":"ready for queries and writes","name":"influxdb","status":"pass","version":"1.8.10"}`))
return
}
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`Wrong Header!`))
},
args: []string{"run", "../main.go", "--header", "X-Foobar: Barfoo", "health"},
expected: "[OK] - InfluxDB Status: pass\n",
},
}

for _, test := range tests {
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func init() {
"Skip the verification of the server's TLS certificate")
pfs.IntVarP(&Timeout, "timeout", "t", Timeout,
"Timeout in seconds for the CheckPlugin")
pfs.StringSliceVarP(&cliConfig.Headers, "header", "", nil,
`Additional HTTP header to include in the request. Can be used multiple times.
Keys and values are separated by a colon (--header "X-Custom: example").`)

rootCmd.Flags().SortFlags = false
pfs.SortFlags = false
Expand Down
36 changes: 36 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,39 @@ func (c *Client) Version() (influxdb.APIVersion, error) {

return v, nil
}

type headersRoundTripper struct {
headers map[string]string
rt http.RoundTripper
}

// NewHeadersRoundTripper adds the given headers to a request
func NewHeadersRoundTripper(headers map[string]string, rt http.RoundTripper) http.RoundTripper {
return &headersRoundTripper{headers, rt}
}

func (rt *headersRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// RoundTrip should not modify the request, except for
// consuming and closing the Request's Body.
req = cloneRequest(req)

for key, value := range rt.headers {
req.Header.Add(key, value)
}

return rt.rt.RoundTrip(req)
}

// cloneRequest returns a clone of the provided *http.Request
func cloneRequest(r *http.Request) *http.Request {
// Shallow copy of the struct.
r2 := new(http.Request)
*r2 = *r
// Deep copy of the Header.
r2.Header = make(http.Header)
for k, s := range r.Header {
r2.Header[k] = s
}

return r2
}