Skip to content

Commit

Permalink
use config file for ng-monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar committed Oct 28, 2021
1 parent 68e6bf2 commit 1dc5bbc
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 44 deletions.
15 changes: 15 additions & 0 deletions embed/templates/config/ngmonitoring.yml.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# NG Monitoring Server Configuration.

# Server address.
addr = "0.0.0.0:{{.Port}}"

[log]
# Log path
path = "{{.LogDir}}"

# Log level: INFO, WARN, ERROR
level = "INFO"

[pd]
# Addresses of PD instances within the TiDB cluster. Multiple addresses are separated by commas, e.g. "10.0.0.1:2379","10.0.0.2:2379"
endpoints = [{{.PDAddrs}}]
27 changes: 7 additions & 20 deletions embed/templates/scripts/run_prometheus.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#!/bin/bash
set -e

function hup_handle {
childpid=$(cat /proc/$$/task/$$/children)
child=($childpid)
kill -HUP ${child[@]:0:2}
wait
}
trap hup_handle HUP

DEPLOY_DIR={{.DeployDir}}
cd "${DEPLOY_DIR}" || exit 1

Expand All @@ -21,27 +13,22 @@ numactl --cpunodebind={{.NumaNode}} --membind={{.NumaNode}} bin/ng-monitoring-se
{{- else}}
bin/ng-monitoring-server \
{{- end}}
--addr "0.0.0.0:{{.NgPort}}" \
--pd.endpoints {{.PdList}} \
--log.path "{{.LogDir}}" \
--config {{.DeployDir}}/conf/ngmonitoring.yml \
>/dev/null 2>&1 &
ng_pid=$!
fi

exec > >(tee -i -a "{{.LogDir}}/prometheus.log")
exec 2>&1

{{- if .NumaNode}}
numactl --cpunodebind={{.NumaNode}} --membind={{.NumaNode}} bin/prometheus/prometheus \
exec numactl --cpunodebind={{.NumaNode}} --membind={{.NumaNode}} bin/prometheus/prometheus \
{{- else}}
bin/prometheus/prometheus \
exec bin/prometheus/prometheus \
{{- end}}
--config.file="{{.DeployDir}}/conf/prometheus.yml" \
--web.listen-address=":{{.Port}}" \
--web.external-url="http://{{.IP}}:{{.Port}}/" \
--web.enable-admin-api \
--log.level="info" \
--storage.tsdb.path="{{.DataDir}}" \
--storage.tsdb.retention="{{.Retention}}" \
2>&1 | tee -i -a "{{.LogDir}}/prometheus.log" &
prometheus_pid=$!

#trap 'kill $ng_pid $prometheus_pid; exit' CHLD
wait
--storage.tsdb.retention="{{.Retention}}"
25 changes: 14 additions & 11 deletions pkg/cluster/spec/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ func (i *MonitorInstance) InitConfig(
WithNumaNode(spec.NumaNode).
WithRetention(spec.Retention)

// launch ng-monitoring
var pds []string
if servers, found := topoHasField("PDServers"); found {
for i := 0; i < servers.Len(); i++ {
pd := servers.Index(i).Interface().(*PDSpec)
pds = append(pds, fmt.Sprintf("%s:%d", pd.Host, pd.ClientPort))
}
}
cfg = cfg.WithNgPort(spec.NgPort).WithPdList(pds)

fp := filepath.Join(paths.Cache, fmt.Sprintf("run_prometheus_%s_%d.sh", i.GetHost(), i.GetPort()))
if err := cfg.ConfigToFile(fp); err != nil {
return err
Expand All @@ -195,10 +185,11 @@ func (i *MonitorInstance) InitConfig(
return err
}

ngcfg := config.NewNgMonitoringConfig(clusterName, clusterVersion, enableTLS)

monitoredOptions := i.topo.GetMonitoredOptions()

// transfer config
fp = filepath.Join(paths.Cache, fmt.Sprintf("prometheus_%s_%d.yml", i.GetHost(), i.GetPort()))
cfig := config.NewPrometheusConfig(clusterName, clusterVersion, enableTLS)
if monitoredOptions != nil {
cfig.AddBlackbox(i.GetHost(), uint64(monitoredOptions.BlackboxExporterPort))
Expand All @@ -210,6 +201,7 @@ func (i *MonitorInstance) InitConfig(
pd := servers.Index(i).Interface().(*PDSpec)
uniqueHosts.Insert(pd.Host)
cfig.AddPD(pd.Host, uint64(pd.ClientPort))
ngcfg.AddPD(pd.Host, uint64(pd.ClientPort))
}
}
if servers, found := topoHasField("TiKVServers"); found {
Expand Down Expand Up @@ -322,6 +314,17 @@ func (i *MonitorInstance) InitConfig(
return err
}

ngcfg.AddPort(spec.NgPort).AddLog(paths.Log)
fp = filepath.Join(paths.Cache, fmt.Sprintf("ngmonitoring_%s_%d.yml", i.GetHost(), i.GetPort()))
if err := ngcfg.ConfigToFile(fp); err != nil {
return err
}
dst = filepath.Join(paths.Deploy, "conf", "ngmonitoring.yml")
if err := e.Transfer(ctx, fp, dst, false, 0); err != nil {
return err
}

fp = filepath.Join(paths.Cache, fmt.Sprintf("prometheus_%s_%d.yml", i.GetHost(), i.GetPort()))
if err := cfig.ConfigToFile(fp); err != nil {
return err
}
Expand Down
98 changes: 98 additions & 0 deletions pkg/cluster/template/config/ngmonitoring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"bytes"
"fmt"
"os"
"path"
"text/template"

"github.com/pingcap/tiup/embed"
)

// NgMonitoringConfig represent the data to generate NgMonitoring config
type NgMonitoringConfig struct {
ClusterName string
TLSEnabled bool
Port int
PDAddrs string
LogDir string
}

// NewNgMonitoringConfig returns a PrometheusConfig
func NewNgMonitoringConfig(clusterName, clusterVersion string, enableTLS bool) *NgMonitoringConfig {
cfg := &NgMonitoringConfig{
ClusterName: clusterName,
TLSEnabled: enableTLS,
}
return cfg
}

// AddPD add a PD address
func (c *NgMonitoringConfig) AddPD(ip string, port uint64) *NgMonitoringConfig {
if c.PDAddrs == "" {
c.PDAddrs = fmt.Sprintf("\"%s:%d\"", ip, port)
} else {
c.PDAddrs += fmt.Sprintf(",\"%s:%d\"", ip, port)
}
return c
}

// AddLog add logdir to ng-monitoring conf
func (c *NgMonitoringConfig) AddLog(logdir string) *NgMonitoringConfig {
c.LogDir = logdir
return c
}

// AddPort add port to ng-monitoring conf
func (c *NgMonitoringConfig) AddPort(port int) *NgMonitoringConfig {
c.Port = port
return c
}

// ConfigWithTemplate generate the Prometheus config content by tpl
func (c *NgMonitoringConfig) ConfigWithTemplate(tpl string) ([]byte, error) {
tmpl, err := template.New("NgMonitoring").Parse(tpl)
if err != nil {
return nil, err
}

content := bytes.NewBufferString("")
if err := tmpl.Execute(content, c); err != nil {
return nil, err
}

return content.Bytes(), nil
}

// Config generate the config file data.
func (c *NgMonitoringConfig) Config() ([]byte, error) {
fp := path.Join("templates", "config", "ngmonitoring.yml.tpl")
tpl, err := embed.ReadTemplate(fp)
if err != nil {
return nil, err
}
return c.ConfigWithTemplate(string(tpl))
}

// ConfigToFile write config content to specific path
func (c *NgMonitoringConfig) ConfigToFile(file string) error {
config, err := c.Config()
if err != nil {
return err
}
return os.WriteFile(file, config, 0755)
}
13 changes: 0 additions & 13 deletions pkg/cluster/template/scripts/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"os"
"path"
"regexp"
"strings"
"text/template"

"github.com/pingcap/tiup/embed"
Expand Down Expand Up @@ -55,24 +54,12 @@ func (c *PrometheusScript) WithPort(port int) *PrometheusScript {
return c
}

// WithNgPort set NgPort field of PrometheusScript
func (c *PrometheusScript) WithNgPort(port int) *PrometheusScript {
c.NgPort = port
return c
}

// WithNumaNode set NumaNode field of PrometheusScript
func (c *PrometheusScript) WithNumaNode(numa string) *PrometheusScript {
c.NumaNode = numa
return c
}

// WithPdList set Pd field of PrometheusScript
func (c *PrometheusScript) WithPdList(pds []string) *PrometheusScript {
c.PdList = strings.Join(pds, ",")
return c
}

// WithRetention set Retention field of PrometheusScript
func (c *PrometheusScript) WithRetention(retention string) *PrometheusScript {
valid, _ := regexp.MatchString("^[1-9]\\d*d$", retention)
Expand Down

0 comments on commit 1dc5bbc

Please sign in to comment.