forked from cloudfoundry/galera-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
93 lines (76 loc) · 1.86 KB
/
server.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
package main
import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/sttts/galera-healthcheck/healthcheck"
. "github.com/sttts/galera-healthcheck/logger"
_ "github.com/go-sql-driver/mysql"
)
var serverPort = flag.Int(
"port",
8080,
"Specifies the port of the healthcheck server",
)
var mysqlUser = flag.String(
"user",
"root",
"Specifies the MySQL user to connect as",
)
var mysqlPassword = flag.String(
"password",
"",
"Specifies the MySQL password to connect with",
)
var availableWhenDonor = flag.Bool(
"availWhenDonor",
true,
"Specifies if the healthcheck allows availability when in donor state",
)
var availableWhenReadOnly = flag.Bool(
"availWhenReadOnly",
false,
"Specifies if the healthcheck allows availability when in read only mode",
)
var pidfile = flag.String(
"pidfile",
"",
"Location for the pidfile",
)
var connectionCutterPath = flag.String(
"connectionCutterPath",
"",
"Location for the script which cuts mysql connections",
)
var healthchecker *healthcheck.Healthchecker
func handler(w http.ResponseWriter, r *http.Request) {
result, msg := healthchecker.Check()
if result != nil && result.Healthy {
w.WriteHeader(http.StatusOK)
} else if result != nil && !result.Healthy {
w.WriteHeader(http.StatusServiceUnavailable)
} else {
w.WriteHeader(http.StatusContinue)
}
fmt.Fprintf(w, "Galera Cluster Node status: %s", msg)
LogWithTimestamp(msg)
}
func main() {
flag.Parse()
err := ioutil.WriteFile(*pidfile, []byte(strconv.Itoa(os.Getpid())), 0644)
if err != nil {
panic(err)
}
db, _ := sql.Open("mysql", fmt.Sprintf("%s:%s@/", *mysqlUser, *mysqlPassword))
config := healthcheck.HealthcheckerConfig{
*availableWhenDonor,
*availableWhenReadOnly,
}
healthchecker = healthcheck.New(db, config)
http.HandleFunc("/", handler)
http.ListenAndServe(fmt.Sprintf(":%d", *serverPort), nil)
}