-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
180 lines (153 loc) · 4.76 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"database/sql"
"fmt"
"os"
"strconv"
"strings"
_ "github.com/lib/pq"
"github.com/voxelbrain/goptions"
)
const (
/* exit codes... */
BecauseConnectionFailed = 2
BecauseMasterQueryFailed = 3
BecauseSlaveQueryFailed = 4
BecauseXlogConversionFailed = 5
BecauseReplicationLag = 6
)
var debugging = false
func debug(f string, args ...interface{}) {
if debugging {
fmt.Fprintf(os.Stderr, "DEBUG> %s\n", fmt.Sprintf(f, args))
}
}
func query1(db *sql.DB, q string) (string, error) {
r, err := db.Query(q)
if err != nil {
return "", err
}
var v string
r.Next()
if err = r.Scan(&v); err != nil {
return "", err
}
return v, nil
}
func connect(host, port, user, password, dbname string) *sql.DB {
conn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
debug("connecting to `%s`", conn)
db, err := sql.Open("postgres", conn)
if err != nil {
fmt.Printf("Error connecting to %s:%s as user %s, database %s: %s\n",
host, port, user, dbname, err)
os.Exit(BecauseConnectionFailed)
return nil
}
return db
}
func xlog(s string) int64 {
l := strings.SplitN(s, "/", 2)
if len(l) != 2 {
fmt.Fprintf(os.Stderr, "xlog(%s) failed - not a valid xlog location?\n", s)
os.Exit(BecauseXlogConversionFailed)
}
a, err := strconv.ParseInt(l[0], 16, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "xlog(%s) failed - not a valid xlog location?\n", s)
os.Exit(BecauseXlogConversionFailed)
}
b, err := strconv.ParseInt(l[1], 16, 64)
if err != nil {
fmt.Fprintf(os.Stderr, "xlog(%s) failed - not a valid xlog location?\n", s)
os.Exit(BecauseXlogConversionFailed)
}
return a<<64 + b
}
type Master struct {
name string
xlog_location string
}
func QueryMaster(host, port, user, pass, dbname string) (m Master) {
m.name = host
db := connect(host, port, user, pass, dbname)
defer db.Close()
var err error
if m.xlog_location, err = query1(db, "SELECT pg_current_xlog_location()"); err != nil {
fmt.Printf("Failed to query current xlog location: %s\n", err)
os.Exit(BecauseMasterQueryFailed)
}
return
}
type Slave struct {
name string
recv_location string
rply_location string
behind int64
delay int64
}
func QuerySlave(host, port, user, pass, dbname string) (s Slave) {
s.name = host;
db := connect(host, port, user, pass, dbname)
defer db.Close()
var err error
if s.recv_location, err = query1(db, "SELECT pg_last_xlog_receive_location()"); err != nil {
fmt.Printf("Failed to query last received xlog location: %s\n", err)
os.Exit(BecauseSlaveQueryFailed)
}
if s.rply_location, err = query1(db, "SELECT pg_last_xlog_replay_location()"); err != nil {
fmt.Printf("Failed to query last replayed xlog location: %s\n", err)
os.Exit(BecauseSlaveQueryFailed)
}
return
}
func (s *Slave) Check(m Master) {
s.behind = xlog(s.recv_location) - xlog(m.xlog_location)
s.delay = xlog(s.rply_location) - xlog(s.recv_location)
}
func main() {
options := struct {
Master string `goptions:"-M, --master, description='Replication master host. May only be specified once'"`
Slaves []string `goptions:"-S, --slave, description='Replication slave host(s). May be specified more than once'"`
Port string `goptions:"-p, --port, description='TCP port that Postgres listens on'"`
User string `goptions:"-u, --user, description='User to connect as'"`
Password string `goptions:"-w, --password, description='Password to connect with'"`
Database string `goptions:"-d, --database, description='Database to use for testing'"`
Debug bool `goptions:"-D, --debug, description='Enable debugging output (to standard error)'"`
AcceptLag int64 `goptions:"-l, --lag, description='Maximum acceptable lag behind the master xlog position (bytes)'"`
}{
Port: "6432",
AcceptLag: 8192,
}
goptions.ParseAndFail(&options)
if options.Database == "" {
options.Database = options.User
}
debugging = options.Debug
var slaves = make([]Slave, len(options.Slaves))
for i, host := range options.Slaves {
slaves[i] = QuerySlave(host, options.Port,
options.User, options.Password, options.Database)
}
master := QueryMaster(options.Master, options.Port,
options.User, options.Password, options.Database)
fmt.Printf("%s: %s\n", master.name, master.xlog_location)
failed := false
for _, slave := range slaves {
slave.Check(master)
emsg := ""
if slave.behind > options.AcceptLag {
failed = true
emsg = " !! too far behind write master"
}
fmt.Printf("%s: %s %-12s to %s %-12s%s\n", slave.name,
slave.recv_location, fmt.Sprintf("(%d)", -1*slave.behind),
slave.rply_location, fmt.Sprintf("(%d)", -1*slave.delay),
emsg)
}
if failed {
fmt.Print("FAILED\n")
os.Exit(BecauseReplicationLag)
}
}