github.com/klaytn/klaytn@v1.12.1/cmd/homi/setup/prometheus_config.go (about)

     1  // Copyright 2018 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package setup
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"text/template"
    23  )
    24  
    25  type PrometheusConfig struct {
    26  	CnIps []string
    27  	PnIps []string
    28  }
    29  
    30  func NewPrometheusConfig(cnNum int, cnNetworkIp string, pnNum int, pnNetworkId1 string, pnNetworkId2 string) PrometheusConfig {
    31  	var cnIps []string
    32  	for i := 1; i <= cnNum; i++ {
    33  		cnIps = append(cnIps, fmt.Sprintf("%s.%d", cnNetworkIp, 100+i))
    34  	}
    35  
    36  	var pnIps []string
    37  	for i := 1; i <= pnNum; i++ {
    38  		instanceId := 100
    39  		if i%2 == 1 {
    40  			instanceId += i/2 + 1
    41  			pnIps = append(pnIps, fmt.Sprintf("%s.%d", pnNetworkId1, instanceId))
    42  		} else {
    43  			instanceId += i / 2
    44  			pnIps = append(pnIps, fmt.Sprintf("%s.%d", pnNetworkId2, instanceId))
    45  		}
    46  	}
    47  
    48  	return PrometheusConfig{
    49  		CnIps: cnIps,
    50  		PnIps: pnIps,
    51  	}
    52  }
    53  
    54  func (pConfig PrometheusConfig) String() string {
    55  	tmpl, err := template.New("PrometheusConfig").Parse(pTemplate)
    56  	if err != nil {
    57  		fmt.Printf("Failed to parse template, %v", err)
    58  		return ""
    59  	}
    60  
    61  	result := new(bytes.Buffer)
    62  	err = tmpl.Execute(result, pConfig)
    63  	if err != nil {
    64  		fmt.Printf("Failed to render template, %v", err)
    65  		return ""
    66  	}
    67  
    68  	return result.String()
    69  }
    70  
    71  var pTemplate = `global:
    72    scrape_interval:     15s
    73    evaluation_interval: 15s
    74  
    75  scrape_configs:
    76    - job_name: 'klaytn'
    77      static_configs:
    78      - targets:
    79        {{- range .CnIps }}
    80        - "{{ . }}:61001"
    81        {{- end }}
    82        labels:
    83          job: 'klaytn-cn'
    84      {{- if gt (len .PnIps) 0 }}
    85      - targets:
    86        {{- range .PnIps }}
    87        - "{{ . }}:61001"
    88        {{- end }}
    89        labels:
    90          job: 'klaytn-pn'
    91      {{ end }}
    92  `