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
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v9
with:
version: v2.1.6
version: v2.11.2
7 changes: 5 additions & 2 deletions cmd/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
19 changes: 14 additions & 5 deletions internal/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package internal

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"os"
"time"

"github.com/NETWAYS/go-check"

Expand All @@ -19,15 +21,16 @@ 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"}`
//
// 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)
}

Expand All @@ -39,15 +42,17 @@ 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)
}

*username = data.Username
*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)

Expand All @@ -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)
}

Expand Down