54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
type CapacityCheckResult struct {
|
|
Status string `json:"status"`
|
|
ActiveConnections int64 `json:"active_connections"`
|
|
KafkaLag float64 `json:"kafka_lag"`
|
|
Findings []string `json:"findings"`
|
|
}
|
|
|
|
type capacityCheckReport struct {
|
|
Status string `json:"status"`
|
|
Totals struct {
|
|
ActiveConnections int64 `json:"active_connections"`
|
|
KafkaLag float64 `json:"kafka_lag"`
|
|
} `json:"totals"`
|
|
Findings []string `json:"findings"`
|
|
}
|
|
|
|
type CapacityCheckCommand struct {
|
|
bin string
|
|
}
|
|
|
|
func NewCapacityCheckCommand(bin string) *CapacityCheckCommand {
|
|
return &CapacityCheckCommand{bin: strings.TrimSpace(bin)}
|
|
}
|
|
|
|
func (c *CapacityCheckCommand) CheckCapacity(ctx context.Context) (CapacityCheckResult, error) {
|
|
if c.bin == "" {
|
|
return CapacityCheckResult{}, errors.New("capacity-check bin is empty")
|
|
}
|
|
output, err := exec.CommandContext(ctx, c.bin).CombinedOutput()
|
|
var report capacityCheckReport
|
|
if jsonErr := json.Unmarshal(output, &report); jsonErr != nil {
|
|
if err != nil {
|
|
return CapacityCheckResult{}, errors.New(err.Error() + ": " + strings.TrimSpace(string(output)))
|
|
}
|
|
return CapacityCheckResult{}, jsonErr
|
|
}
|
|
return CapacityCheckResult{
|
|
Status: report.Status,
|
|
ActiveConnections: report.Totals.ActiveConnections,
|
|
KafkaLag: report.Totals.KafkaLag,
|
|
Findings: report.Findings,
|
|
}, nil
|
|
}
|