From 1b800026ce40c191d90939fb47fec4055a65cd07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Tue, 28 Apr 2026 11:41:39 +0200 Subject: [PATCH 1/2] Upate linter and fix linter findings --- .github/workflows/golangci-lint.yaml | 2 +- cmd/datastore.go | 7 +++++-- cmd/root.go | 3 +++ internal/utils.go | 19 ++++++++++++++----- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index 7d53aa0..fb12032 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -17,4 +17,4 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v9 with: - version: v2.1.6 + version: v2.11.2 diff --git a/cmd/datastore.go b/cmd/datastore.go index 47f3bc2..83f5633 100644 --- a/cmd/datastore.go +++ b/cmd/datastore.go @@ -126,7 +126,8 @@ func queryDatastores() { // Process query results. for rows.Next() { // Read row into variables. - if err = rows.Scan(&datastoreName, &capacity, &freeSpace); err != nil { + err = rows.Scan(&datastoreName, &capacity, &freeSpace) + if err != nil { check.ExitError(err) } @@ -137,7 +138,9 @@ func queryDatastores() { pr := result.PartialResult{ Output: fmt.Sprintf("Used storage for datastore %s: %d%%", datastoreName, perfData.Value), } - if err = pr.SetState(state); err != nil { + + err = pr.SetState(state) + if err != nil { check.ExitError(err) } diff --git a/cmd/root.go b/cmd/root.go index 77eac7e..37712a6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,10 +35,13 @@ Icinga2 admins to trigger alerts on their side of the monitoring.`, PersistentPreRun: func(cmd *cobra.Command, _ []string) { if machine == "" { cmd.DisableAutoGenTag = true + check.Exitf(check.Unknown, "Error: --machine flag is required") } + if host == "" { cmd.DisableAutoGenTag = true + check.Exitf(check.Unknown, "Error: --host flag is required") } // Parse credentials file. diff --git a/internal/utils.go b/internal/utils.go index d5a8fcb..8cfbf69 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -1,10 +1,12 @@ package internal import ( + "context" "database/sql" "encoding/json" "fmt" "os" + "time" "github.com/NETWAYS/go-check" @@ -19,7 +21,7 @@ type Credentials struct { Password string `json:"password"` } -// Try to parse a given credentialsFile and write the parsed credentials to +// ParseCredentialsFile tries to parse a given credentialsFile and write the parsed credentials to // `user` and `password` variables // Credential files are required to be JSON object of the following spec: // `{"username": "vspheredb", "password": "vspheredb"}` @@ -27,7 +29,8 @@ type Credentials struct { // If parsing fails, check exits with UNKNOWN state. func ParseCredentialsFile(credentialsFile string, username *string, password *string) { // Check if file exists, exit with UNKNOWN otherwise. - if _, err := os.Stat(credentialsFile); os.IsNotExist(err) { + _, err := os.Stat(credentialsFile) + if os.IsNotExist(err) { check.ExitError(err) } @@ -39,7 +42,9 @@ func ParseCredentialsFile(credentialsFile string, username *string, password *st // Parse file contents into known JSON struct. var data Credentials - if err := json.Unmarshal(content, &data); err != nil { + + err = json.Unmarshal(content, &data) + if err != nil { check.ExitError(err) } @@ -47,7 +52,7 @@ func ParseCredentialsFile(credentialsFile string, username *string, password *st *password = data.Password } -// Establishes and checks DB connection and returns the connection. +// DBConnection establishes and checks DB connection and returns the connection. func DBConnection(host string, port int16, username string, password string, database string) *sql.DB { connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, database) @@ -57,7 +62,11 @@ func DBConnection(host string, port int16, username string, password string, dat check.ExitError(err) } // Test connection. - if err := db.Ping(); err != nil { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + err = db.PingContext(ctx) + if err != nil { check.ExitError(err) } From e9ab91e1486a5a0c00247c296b1929cd360a64e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20K=C3=A4stle?= Date: Tue, 28 Apr 2026 12:26:02 +0200 Subject: [PATCH 2/2] Update golang and packages --- go.mod | 4 ++-- go.sum | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 83850a8..a6f062a 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,11 @@ module github.com/NETWAYS/check_vspheredb_data -go 1.24 +go 1.24.0 require github.com/NETWAYS/go-check v0.6.4 require ( - filippo.io/edwards25519 v1.1.1 // indirect + filippo.io/edwards25519 v1.2.0 // indirect github.com/go-sql-driver/mysql v1.9.3 github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/cobra v1.10.2 diff --git a/go.sum b/go.sum index b3d8da0..cb78063 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -filippo.io/edwards25519 v1.1.1 h1:YpjwWWlNmGIDyXOn8zLzqiD+9TyIlPhGFG96P39uBpw= -filippo.io/edwards25519 v1.1.1/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo= +filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc= github.com/NETWAYS/go-check v0.6.4 h1:4WETSVNZNEP0Yudcp5xlvxq6RGn920cmUKq4fz/P1GQ= github.com/NETWAYS/go-check v0.6.4/go.mod h1:8/GWnq8SirreAixgRmcp82JG16NnEl38rHq9phICy9s= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=