mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-04-15 07:16:34 +00:00
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package collect
|
|
|
|
import (
|
|
"bytes"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"regexp"
|
|
|
|
_ "github.com/lib/pq"
|
|
"github.com/pkg/errors"
|
|
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
|
|
)
|
|
|
|
func Postgres(c *Collector, databaseCollector *troubleshootv1beta2.Database) (CollectorResult, error) {
|
|
databaseConnection := DatabaseConnection{}
|
|
|
|
db, err := sql.Open("postgres", databaseCollector.URI)
|
|
if err != nil {
|
|
databaseConnection.Error = err.Error()
|
|
} else {
|
|
query := `select version()`
|
|
row := db.QueryRow(query)
|
|
version := ""
|
|
if err := row.Scan(&version); err != nil {
|
|
databaseConnection.Error = err.Error()
|
|
} else {
|
|
databaseConnection.IsConnected = true
|
|
|
|
postgresVersion, err := parsePostgresVersion(version)
|
|
if err != nil {
|
|
databaseConnection.Version = "Unknown"
|
|
databaseConnection.Error = err.Error()
|
|
} else {
|
|
databaseConnection.Version = postgresVersion
|
|
}
|
|
}
|
|
}
|
|
|
|
b, err := json.Marshal(databaseConnection)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "failed to marshal database connection")
|
|
}
|
|
|
|
collectorName := databaseCollector.CollectorName
|
|
if collectorName == "" {
|
|
collectorName = "postgres"
|
|
}
|
|
|
|
output := NewResult()
|
|
output.SaveResult(c.BundlePath, fmt.Sprintf("postgres/%s.json", collectorName), bytes.NewBuffer(b))
|
|
|
|
return output, nil
|
|
}
|
|
|
|
func parsePostgresVersion(postgresVersion string) (string, error) {
|
|
re := regexp.MustCompile("PostgreSQL ([0-9.]*)")
|
|
matches := re.FindStringSubmatch(postgresVersion)
|
|
if len(matches) < 2 {
|
|
return "", errors.Errorf("postgres version did not match regex: %q", postgresVersion)
|
|
}
|
|
|
|
return matches[1], nil
|
|
|
|
}
|