-
Notifications
You must be signed in to change notification settings - Fork 19
/
haproxy.go
222 lines (166 loc) · 3.49 KB
/
haproxy.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"net"
"fmt"
"bufio"
"errors"
"strings"
"encoding/json"
"io/ioutil"
"os/exec"
"bytes"
"strconv"
)
var Binary string
var PidFile string
func SetPidFileName(c string) error {
PidFile = c
return nil
}
func SetBinaryFileName(c string) error {
Binary = c
return nil
}
// Executes a arbitrariry HAproxy command on the unix socket
func HaproxyCmd(cmd string) (string, error){
// connect to haproxy
conn, err_conn := net.Dial("unix", "/tmp/haproxy.stats.sock")
defer conn.Close()
if err_conn != nil {
return "", errors.New("Unable to connect to Haproxy socket")
} else {
fmt.Fprint(conn, cmd)
response := ""
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
response += (scanner.Text() + "\n")
}
if err := scanner.Err(); err != nil {
return "", err
} else {
return response, nil
}
}
}
/*
Backends
*/
// Sets the weight of a backend
func SetWeight(backend string, server string, weight int) (string, error){
result, err := HaproxyCmd("set weight " + backend + "/" + server + " " + strconv.Itoa(weight) +"\n")
if err != nil {
return "", err
} else {
return result, nil
}
}
/*
Frontends
*/
// Adds an ACL.
// We need to match a frontend name to an id. This is somewhat awkard.
func AddAcl(frontend string,acl string, pattern string)(string, error) {
result, err := HaproxyCmd("add acl " + acl + pattern)
if err != nil {
return "", err
} else {
return result, nil
}
}
/*
Stats
*/
/* get the basic stats in CSV format
@parameter statsType takes the form of:
- all
- frontend
- backend
*/
func GetStats(statsType string) ([]StatsGroup, error) {
var Stats []StatsGroup
var cmdString string
switch statsType {
case "all":
cmdString = "show stat -1\n"
case "backend":
cmdString = "show stat -1 2 -1\n"
case "frontend":
cmdString = "show stat -1 1 -1\n"
case "server":
cmdString = "show stat -1 4 -1\n"
}
result, err := HaproxyCmd(cmdString)
if err != nil {
return Stats, err
} else {
result, err := parse_csv(strings.Trim(result,"# "))
if err != nil {
return Stats, err
} else {
err := json.Unmarshal([]byte(result), &Stats)
if err != nil {
return Stats, err
} else {
return Stats, nil
}
}
}
}
/*
Reload
*/
// Configuration reload
func Reload() error {
pid, err := ioutil.ReadFile(PidFile)
if err !=nil {
return err
}
/* Setup all the command line parameters so we get an executable similar to
/usr/local/bin/haproxy -f resources/haproxy_new.cfg -p resources/haproxy-private.pid -sf 1234
*/
arg0 := "-f"
arg1 := ConfigFile
arg2 := "-p"
arg3 := PidFile
arg4 := "-D"
arg5 := "-sf"
arg6 := strings.Trim(string(pid),"\n")
var cmd *exec.Cmd
// If this is the first run, the PID value will be empty, otherwise it will be > 0
if len(arg6) > 0 {
cmd = exec.Command(Binary, arg0, arg1 ,arg2, arg3, arg4, arg5, arg6)
} else {
cmd = exec.Command(Binary, arg0, arg1 ,arg2, arg3, arg4 )
}
var out bytes.Buffer
cmd.Stdout = &out
cmdErr := cmd.Run()
if cmdErr != nil {
fmt.Println(cmdErr.Error())
return cmdErr
}
log.Info("HaproxyReload: " + out.String() + string(pid))
return nil
}
/*
Info
*/
func GetInfo() (Info, error) {
var Info Info
result, err := HaproxyCmd("show info \n")
if err != nil {
return Info, err
} else {
result, err := parse_multi_line(result)
if err != nil {
return Info, err
} else {
err := json.Unmarshal([]byte(result), &Info)
if err != nil {
return Info, err
} else {
return Info, nil
}
}
}
}