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 cmd/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func queryCPU() {
check.ExitError(err)
}

dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)

err = dbConnection.QueryRow(
`SELECT hqs.overall_cpu_usage,
Expand Down
4 changes: 2 additions & 2 deletions cmd/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func queryDatastore() {
check.ExitError(err)
}

dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)

err = dbConnection.QueryRow(`SELECT ds.capacity, ds.free_space
FROM datastore ds
Expand Down Expand Up @@ -102,7 +102,7 @@ func queryDatastores() {
}

// Collect query results.
dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)

rows, err := dbConnection.Query(`SELECT o.object_name, ds.capacity, ds.free_space
FROM datastore ds
Expand Down
2 changes: 1 addition & 1 deletion cmd/hba.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func queryHba() {
check.ExitError(err)
}

dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)

err = dbConnection.QueryRow(`SELECT hardware_num_hba
FROM host_system
Expand Down
2 changes: 1 addition & 1 deletion cmd/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func queryMemory() {
check.ExitError(err)
}

dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)

err = dbConnection.QueryRow(
`SELECT hqs.overall_memory_usage_mb,
Expand Down
2 changes: 1 addition & 1 deletion cmd/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func queryNic() {
check.ExitError(err)
}

dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)

err = dbConnection.QueryRow(`SELECT hardware_num_nic
FROM host_system
Expand Down
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ var username string
var password string
var credentialsFile string

var useTLS bool
var caCertPath string
var clientCertPath string
var clientCertKey string

var pl check.PerfdataList

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -57,4 +62,8 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&username, "username", "u", "vspheredb", "Database username")
rootCmd.PersistentFlags().StringVarP(&password, "password", "P", "vspheredb", "Database password")
rootCmd.PersistentFlags().StringVarP(&credentialsFile, "credentials-file", "f", "", "Path to the credentials file")
rootCmd.PersistentFlags().BoolVar(&useTLS, "tls", false, "Use TLS to connect to the database")
rootCmd.PersistentFlags().StringVar(&caCertPath, "ca-cert", "", "CA certificate (file path)")
rootCmd.PersistentFlags().StringVar(&clientCertPath, "client-cert", "", "Client certificate (file path)")
rootCmd.PersistentFlags().StringVar(&clientCertKey, "client-key", "", "Client certificate key (file path)")
}
2 changes: 1 addition & 1 deletion cmd/temperature.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func queryTemperature() {
check.ExitError(err)
}

dbConnection := internal.DBConnection(host, port, username, password, database)
dbConnection := internal.DBConnection(host, port, username, password, database, useTLS, caCertPath, clientCertPath, clientCertKey)
defer dbConnection.Close()

rows, err := dbConnection.Query(`SELECT se.name, se.current_reading
Expand Down
56 changes: 54 additions & 2 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package internal

import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/json"
"errors"
"fmt"
"os"
"time"

"github.com/NETWAYS/go-check"

"github.com/go-sql-driver/mysql"
// needed to use the MySQL driver for the sql module.
_ "github.com/go-sql-driver/mysql"
)
Expand Down Expand Up @@ -53,8 +57,56 @@ func ParseCredentialsFile(credentialsFile string, username *string, password *st
}

// 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)
func DBConnection(host string, port int16, username string, password string, database string, usetls bool, cacertPath string, clientCertPath string, clientKeyPath string) *sql.DB {
var connStr string

if usetls { //nolint:nestif
TLSConfig := tls.Config{}
complexConfig := false

if cacertPath != "" {
rootCertPool := x509.NewCertPool()

pem, err := os.ReadFile(cacertPath)
if err != nil {
check.ExitError(err)
}

if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
check.ExitError(errors.New("failed to append PEM"))
}

TLSConfig.RootCAs = rootCertPool
complexConfig = true
}

if clientCertPath != "" && clientKeyPath != "" {
clientCert := make([]tls.Certificate, 0, 1)

certs, err := tls.LoadX509KeyPair(clientCertPath, clientKeyPath)
if err != nil {
check.ExitError(err)
}

clientCert = append(clientCert, certs)

TLSConfig.Certificates = clientCert
complexConfig = true
}

err := mysql.RegisterTLSConfig("custom", &TLSConfig)
if err != nil {
check.ExitError(err)
}

if complexConfig {
connStr = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=custom", username, password, host, port, database)
} else {
connStr = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=true", username, password, host, port, database)
}
} else {
connStr = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", username, password, host, port, database)
}

// Open connection.
db, err := sql.Open("mysql", connStr)
Expand Down