Repository navigation
Expand file tree
/
Copy pathhaproxy.go
More file actions
182 lines (142 loc) · 3.01 KB
/
Copy pathhaproxy.go
File metadata and controls
182 lines (142 loc) · 3.01 KB
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 (
"net"
"fmt"
"bufio"
"errors"
"strings"
"encoding/json"
"io/ioutil"
"os/exec"
"bytes"
"strconv"
)
// 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
}
}
/*
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(binary, config, pidfile string) 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 -st 1234
*/
arg0 := "-f"
arg1 := config
arg2 := "-p"
arg3 := pidfile
arg4 := "-st"
arg5 := 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(arg5) > 0 {
cmd = exec.Command(binary, arg0, arg1 ,arg2, arg3, arg4, arg5)
} else {
cmd = exec.Command(binary, arg0, arg1 ,arg2, arg3 )
}
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
}
}
}
}