/
main.go
123 lines (103 loc) · 3.62 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"log"
"net/http"
"time"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
status "github.com/Financial-Times/service-status-go/httphandlers"
"github.com/gorilla/mux"
"github.com/jawher/mow.cli"
)
var (
checks []fthealth.Check
hostPath *string
awsEbsMountPath *string
ntpTimeDrift *string
ntpPollingPeriod *string
apiServerURL *string
rootDiskThresholdPercent *int
mountsThresholdPercent *int
)
const (
memoryThresholdPercent = 15
)
func main() {
app := cli.App("System-healthcheck", "A service that report on current VM status at __health")
hostPath = app.String(cli.StringOpt{
Name: "hostPath",
Value: "",
Desc: "The dir path of the mounted host fs (in the container)",
EnvVar: "SYS_HC_HOST_PATH",
})
rootDiskThresholdPercent = app.Int(cli.IntOpt{
Name: "rootDiskThresholdPercent",
Value: 20,
Desc: "For monitoring the root disk of the instances: when the free space goes below this percentage, the health check will fail",
EnvVar: "ROOT_DISK_THRESHOLD_PERCENT",
})
awsEbsMountPath = app.String(cli.StringOpt{
Name: "awsEbsMountPath",
Value: "",
Desc: "The folder path where the AWS EBSs are mounted by Kubernetes",
EnvVar: "AWS_EBS_MOUNTS_PATH",
})
mountsThresholdPercent = app.Int(cli.IntOpt{
Name: "mountsThresholdPercent",
Value: 10,
Desc: "For monitoring the AWS EBSs that are mounted by Kubernetes: when the free space goes bellow this percentage, the health check will fail",
EnvVar: "MOUNTS_THRESHOLD_PERCENT",
})
ntpTimeDrift = app.String(cli.StringOpt{
Name: "ntpTimeDrift",
Value: "2s",
Desc: "Time drift to allow for in NTP check, either in past or future",
EnvVar: "NTP_TIME_DRIFT",
})
apiServerURL = app.String(cli.StringOpt{
Name: "apiServerURL",
Value: "",
Desc: "The URL of the Kubernetes API server of the cluster",
EnvVar: "K8S_API_SERVER_URL",
})
ntpTimeDriftDuration, err := time.ParseDuration(*ntpTimeDrift)
if err != nil {
ntpTimeDriftDuration = time.Second * 2
log.Printf("Invalid time drift, using default 2s")
}
ntpPollingPeriod = app.String(cli.StringOpt{
Name: "ntpPollingPeriod",
Value: "1m",
Desc: "Polling period for NTP check",
EnvVar: "NTP_POLLING_PERIOD",
})
ntpPollingPeriodDuration, err := time.ParseDuration(*ntpPollingPeriod)
if err != nil {
ntpPollingPeriodDuration = time.Minute
log.Printf("Invalid polling period drift, using default 1m")
}
ntpChecker := &ntpCheckerImpl{
timeDrift: ntpTimeDriftDuration,
pollingPeriod: ntpPollingPeriodDuration,
}
apiServerChecker := &apiServerCheckerImpl{url: *apiServerURL}
checks = append(checks, diskFreeCheckerImpl{*rootDiskThresholdPercent, *mountsThresholdPercent}.Checks()...)
checks = append(checks, memoryCheckerImpl{memoryThresholdPercent}.Checks()...)
checks = append(checks, loadAverageCheckerImpl{}.Checks()...)
checks = append(checks, ntpChecker.Checks()...)
checks = append(checks, apiServerChecker.Checks()...)
r := mux.NewRouter()
timedHC := fthealth.TimedHealthCheck{
HealthCheck: fthealth.HealthCheck{
SystemCode: "upp-system-healthcheck",
Name: "System Healthcheck",
Description: "Monitors system parameters.",
Checks: checks,
},
Timeout: 10 * time.Second,
}
r.HandleFunc("/__health", fthealth.Handler(timedHC))
gtgService := newGtgService(*rootDiskThresholdPercent, *mountsThresholdPercent, memoryThresholdPercent, *apiServerURL)
r.HandleFunc(status.GTGPath, status.NewGoodToGoHandler(gtgService.Check))
log.Print("Starting http server on 8080\n")
panic(http.ListenAndServe(":8080", r))
}