forked from cxfcxf/haproxyctl
/
haproxyctl.go
129 lines (121 loc) · 3.19 KB
/
haproxyctl.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
package main
import (
"flag"
"fmt"
haproxyctl "github.com/cxfcxf/haproxyctl/lib"
"net/http"
"strings"
)
var (
configcheck = flag.Bool("configcheck", false, "check config file")
action = flag.String("action", "", "tell me what action you wanna do?")
execution = flag.String("execution", "", "parameters to action")
binding = flag.String("binding", "", "http port you want program to bind to")
client = flag.String("client", "", "only one client ip are allowed")
f = flag.String("f", "/etc/haproxy/haproxy.cfg", "point configration file, default /etc/haproxy/haproxy.cfg")
)
func handler(w http.ResponseWriter, r *http.Request, h *haproxyctl.HaProxy, c string) {
usage := "please use /haproxyctl?action=xxxx&execution=yyyy"
if strings.Split(r.RemoteAddr, ":")[0] != c {
fmt.Fprintf(w, "sorry, you are not in the whitelist in order to exec api")
} else if r.URL.Path != "/haproxyctl" {
fmt.Fprintf(w, usage)
} else {
err := r.ParseForm()
if err != nil {
panic(err)
}
uri := r.Form
if uri.Get("action") == "" {
fmt.Fprintf(w, "you need to specify action")
} else {
action := uri.Get("action")
execution := uri.Get("execution")
switch action {
case "showstatus":
fmt.Fprintf(w, h.Showstatus())
case "socketexec":
res := h.Exec(execution)
for _, r := range res {
fmt.Fprintf(w, r)
}
case "showhealth":
res := h.Showhealth()
fmt.Fprintf(w, res)
case "showbackend":
res := h.ShowRegexp("BACKEND")
fmt.Fprintf(w, res)
case "showregexp":
res := h.ShowRegexp(execution)
fmt.Fprintf(w, res)
case "enable":
en := strings.Split(execution, "/")
res := h.EnableServer(en[0], en[1])
fmt.Fprintf(w, res)
case "enableall":
h.EnableAll(execution)
case "disable":
dis := strings.Split(execution, "/")
res := h.DisableServer(dis[0], dis[1])
fmt.Fprintf(w, res)
case "disableall":
h.DisableAll(execution)
default:
fmt.Fprintf(w, usage)
}
}
}
}
func main() {
flag.Parse()
haproxy := new(haproxyctl.HaProxy)
if len(*f) > 0 {
haproxy.Loadenv(*f)
} else {
haproxy.Loadenv("/etc/haproxy/haproxy.cfg")
}
if len(*binding) > 0 && len(*client) > 0 {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler(w, r, haproxy, *client)
})
http.ListenAndServe(*binding, nil)
}
if *configcheck {
haproxy.Configcheck()
}
if len(*action) > 0 {
switch *action {
case "socketexec":
res := haproxy.Exec(*execution)
for _, r := range res {
fmt.Print(r)
}
case "showstatus":
res := haproxy.Showstatus()
fmt.Print(res)
case "showbackend":
res := haproxy.ShowRegexp("BACKEND")
fmt.Print(res)
case "showhealth":
res := haproxy.Showhealth()
fmt.Print(res)
case "showregexp":
res := haproxy.ShowRegexp(*execution)
fmt.Print(res)
case "enable":
en := strings.Split(*execution, "/")
res := haproxy.EnableServer(en[0], en[1])
fmt.Printf(res)
case "enableall":
haproxy.EnableAll(*execution)
case "disable":
dis := strings.Split(*execution, "/")
res := haproxy.DisableServer(dis[0], dis[1])
fmt.Printf(res)
case "disableall":
haproxy.DisableAll(*execution)
default:
flag.PrintDefaults()
}
}
}