-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbpinger.go
160 lines (122 loc) · 3.31 KB
/
dbpinger.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"fmt"
"log"
"os"
"database/sql"
"net/http"
"flag"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/gcfg.v1"
)
//Place here any values that you want to check
var dbStatusCheck = map[string]string {
// "wsrep_cluster_state_uuid" This two things are hardcoded
// "wsrep_local_state_uuid"
"wsrep_cluster_status" : "Primary",
"wsrep_connected" : "ON",
"wsrep_ready" : "ON",
"wsrep_local_state" : "4",
}
type Config struct {
Main struct {
Listen string
DBHost string
DBPort string
DBUser string
DBPass string
}
}
var confFile string
var cfg Config
func main() {
flag.StringVar(&confFile, "c", "/etc/dbpinger.conf", "Galera health status checker")
flag.Parse()
err := gcfg.ReadFileInto(&cfg, confFile)
if err != nil {
fmt.Println("Error: Failed to load config file " + confFile + " : "+err.Error());
fmt.Println(err)
os.Exit(1)
}
http.HandleFunc("/ping", pingHandler)
log.Println(http.ListenAndServe(":"+ cfg.Main.Listen, nil))
}
func pingHandler(w http.ResponseWriter, r *http.Request) {
if checkDB(cfg.Main.DBHost, cfg.Main.DBPort, cfg.Main.DBUser, cfg.Main.DBPass, "") {
w.WriteHeader(200)
} else {
w.WriteHeader(500)
}
}
func checkDB(dbhost, dbport, dbuser, dbpass, dbname string) bool {
db,err := newDBConnection(dbhost, dbport, dbuser, dbpass, dbname)
defer db.Close()
if err != nil {
log.Println("Error: Connection failed: "+err.Error())
return false
}
rows,err := db.Query("SHOW STATUS")
if err != nil {
log.Println("Error: Query failed: "+err.Error())
return false
}
tempMap := map[string]int{}
for k,_ := range dbStatusCheck {
tempMap[k] = 1
}
wsrep_cluster_state_uuid := "."
wsrep_local_state_uuid := "-"
defer rows.Close()
for rows.Next() {
var name string
var value string
err = rows.Scan(&name, &value)
if err != nil {
log.Println("Error: Get Rows failed: "+err.Error())
return false
}
if !checkValue(name, value) {
log.Println("Error: " + name + " : " + value)
return false
}
if tempMap[name] != 0 {
tempMap[name] = 2
}
if name == "wsrep_cluster_state_uuid" {
wsrep_cluster_state_uuid = value
}
if name == "wsrep_local_state_uuid" {
wsrep_local_state_uuid = value
}
}
if wsrep_local_state_uuid != wsrep_cluster_state_uuid {
log.Println("Error: wsrep_local_state_uuid and wsrep_cluster_state_uuid don't match or are missing")
return false
}
for k,v := range tempMap {
if v != 2 {
log.Println("Error: " + k + " is missing")
return false
}
}
return true
}
func checkValue(name,value string) bool {
//If is not in the list, we just ignore it (true)
if(dbStatusCheck[name] == "") {
return true
}
return dbStatusCheck[name] == value
}
//You must close the opened connection!
func newDBConnection(dbhost, dbport, dbuser, dbpass, dbname string) (*sql.DB, error) {
key := buildKey(dbhost, dbport, dbuser, dbpass, dbname)
newDB, err := sql.Open("mysql", key + "?timeout=15s&wait_timeout=5000")
if err != nil {
return newDB, err
}
return newDB, nil
}
func buildKey(host, port, user, pass, name string) string {
return fmt.Sprintf("%v:%v@tcp(%v:%v)/%v", user, pass, host, port, name)
}