forked from deliveroo/pgbouncer-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
health.go
48 lines (43 loc) · 973 Bytes
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"context"
"fmt"
"net"
"net/http"
"os/exec"
"github.com/pkg/errors"
)
func handleHealth(ctx context.Context, _ http.ResponseWriter) error {
// This is a blind yes/no response
var err error
if config.EnhancedCheck {
_, err = getUsers(ctx, db)
if err != nil {
return errors.Wrap(err, "PGbouncer enhanced health check failed")
}
} else {
err = probeLocalPort(config.PGBouncerPort)
if err != nil {
return errors.Wrap(err, "PGbouncer port probe check failed")
}
}
if config.CheckDDAgent {
return getAgentHealth(ctx)
}
return nil
}
func probeLocalPort(port int) error {
conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port))
if err == nil {
conn.Close()
}
return err
}
func getAgentHealth(ctx context.Context) error {
psCmd := exec.CommandContext(ctx, "sudo", "-n", "datadog-agent", "health")
err := psCmd.Run()
if err != nil {
return errors.Wrap(err, "Datadog agent health command failed")
}
return nil
}