-
Notifications
You must be signed in to change notification settings - Fork 118
/
add_haproxy.go
153 lines (133 loc) · 5.18 KB
/
add_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
// Copyright 2019 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package management
import (
"fmt"
"os"
"strings"
"github.com/AlekSi/pointer"
"github.com/percona/pmm/admin/agentlocal"
"github.com/percona/pmm/admin/commands"
"github.com/percona/pmm/admin/helpers"
"github.com/percona/pmm/api/managementpb/json/client"
"github.com/percona/pmm/api/managementpb/json/client/ha_proxy"
)
var addHAProxyResultT = commands.ParseTemplate(`
HAProxy Service added.
Service ID : {{ .Service.ServiceID }}
Service name: {{ .Service.ServiceName }}
`)
type addHAProxyResult struct {
Service *ha_proxy.AddHAProxyOKBodyService `json:"service"`
}
func (res *addHAProxyResult) Result() {}
func (res *addHAProxyResult) String() string {
return commands.RenderTemplate(addHAProxyResultT, res)
}
type addHAProxyCommand struct {
ServiceName string
Username string
Password string
CredentialsSource string
Scheme string
MetricsPath string
ListenPort uint16
NodeID string
Environment string
Cluster string
ReplicationSet string
CustomLabels string
MetricsMode string
SkipConnectionCheck bool
}
func (cmd *addHAProxyCommand) Run() (commands.Result, error) {
isSupported, err := helpers.IsHAProxySupported()
if !isSupported {
return nil, err
}
customLabels, err := commands.ParseCustomLabels(cmd.CustomLabels)
if err != nil {
return nil, err
}
if cmd.NodeID == "" {
status, err := agentlocal.GetStatus(agentlocal.DoNotRequestNetworkInfo)
if err != nil {
return nil, err
}
if cmd.NodeID == "" {
cmd.NodeID = status.NodeID
}
}
if cmd.MetricsPath != "" && !strings.HasPrefix(cmd.MetricsPath, "/") {
cmd.MetricsPath = fmt.Sprintf("/%s", cmd.MetricsPath)
}
params := &ha_proxy.AddHAProxyParams{
Body: ha_proxy.AddHAProxyBody{
ServiceName: cmd.ServiceName,
Username: cmd.Username,
Password: cmd.Password,
Scheme: cmd.Scheme,
MetricsPath: cmd.MetricsPath,
ListenPort: int64(cmd.ListenPort),
NodeID: cmd.NodeID,
Environment: cmd.Environment,
Cluster: cmd.Cluster,
ReplicationSet: cmd.ReplicationSet,
CustomLabels: customLabels,
MetricsMode: pointer.ToString(strings.ToUpper(cmd.MetricsMode)),
SkipConnectionCheck: cmd.SkipConnectionCheck,
CredentialsSource: cmd.CredentialsSource,
},
Context: commands.Ctx,
}
resp, err := client.Default.HAProxy.AddHAProxy(params)
if err != nil {
return nil, err
}
return &addHAProxyResult{
Service: resp.Payload.Service,
}, nil
}
// register command
var (
AddHAProxy addHAProxyCommand
AddHAProxyC = AddC.Command("haproxy", "Add HAProxy to monitoring")
)
func init() {
hostname, _ := os.Hostname()
defaultServiceName := hostname + "-haproxy"
serviceNameHelp := fmt.Sprintf("Service name (autodetected default: %s)", defaultServiceName)
AddHAProxyC.Arg("name", serviceNameHelp).Default(defaultServiceName).StringVar(&AddHAProxy.ServiceName)
AddHAProxyC.Flag("username", "HAProxy username").StringVar(&AddHAProxy.Username)
AddHAProxyC.Flag("password", "HAProxy password").StringVar(&AddHAProxy.Password)
AddHAProxyC.Flag("credentials-source", "Credentials provider").StringVar(&AddHAProxy.CredentialsSource)
AddHAProxyC.Flag("scheme", "Scheme to generate URI to exporter metrics endpoints").
PlaceHolder("http or https").StringVar(&AddHAProxy.Scheme)
AddHAProxyC.Flag("metrics-path", "Path under which metrics are exposed, used to generate URI").
PlaceHolder("/metrics").StringVar(&AddHAProxy.MetricsPath)
AddHAProxyC.Flag("listen-port", "Listen port of haproxy exposing the metrics for scraping metrics (Required)").Required().Uint16Var(&AddHAProxy.ListenPort)
AddHAProxyC.Flag("node-id", "Node ID (default is autodetected)").StringVar(&AddHAProxy.NodeID)
AddHAProxyC.Flag("environment", "Environment name like 'production' or 'qa'").
PlaceHolder("prod").StringVar(&AddHAProxy.Environment)
AddHAProxyC.Flag("cluster", "Cluster name").
PlaceHolder("east-cluster").StringVar(&AddHAProxy.Cluster)
AddHAProxyC.Flag("replication-set", "Replication set name").
PlaceHolder("rs1").StringVar(&AddHAProxy.ReplicationSet)
AddHAProxyC.Flag("custom-labels", "Custom user-assigned labels. Example: region=east,app=app1").StringVar(&AddHAProxy.CustomLabels)
AddHAProxyC.Flag("metrics-mode", "Metrics flow mode, can be push - agent will push metrics,"+
" pull - server scrape metrics from agent or auto - chosen by server").
Default("auto").
EnumVar(&AddHAProxy.MetricsMode, metricsModes...)
AddHAProxyC.Flag("skip-connection-check", "Skip connection check").BoolVar(&AddHAProxy.SkipConnectionCheck)
}