|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /*
-
- Copyright (c) [2023] [pcm]
- [pcm-coordinator] is licensed under Mulan PSL v2.
- You can use this software according to the terms and conditions of the Mulan PSL v2.
- You may obtain a copy of Mulan PSL v2 at:
- http://license.coscl.org.cn/MulanPSL2
- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- See the Mulan PSL v2 for more details.
-
- */
-
- package tracker
-
- import (
- "context"
- "github.com/prometheus/alertmanager/api/v2/client"
- "github.com/prometheus/alertmanager/cli"
- "github.com/prometheus/client_golang/api"
- v1 "github.com/prometheus/client_golang/api/prometheus/v1"
- "github.com/prometheus/client_golang/prometheus"
- "github.com/prometheus/common/model"
- "net/url"
- "strconv"
- "strings"
- "sync"
- "time"
- )
-
- var (
- ClusterCpuUtilisationGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_cpu_utilisation",
- Help: "Cluster CPU Utilisation Rate.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterCpuAvailGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_cpu_avail",
- Help: "Cluster CPU Available.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterCpuTotalGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_cpu_total",
- Help: "Cluster CPU Total.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterMemoryUtilisationGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_memory_utilisation",
- Help: "Cluster Memory Utilisation Rate.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterMemoryAvailGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_memory_avail",
- Help: "Cluster Memory Available.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterMemoryTotalGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_memory_total",
- Help: "Cluster Memory Total.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterDiskUtilisationGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_disk_utilisation",
- Help: "Cluster Disk Utilisation Rate.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterDiskAvailGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_disk_avail",
- Help: "Cluster Disk Available.",
- }, []string{"cluster_name", "adapter_id"})
- ClusterDiskTotalGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
- Name: "cluster_disk_total",
- Help: "Cluster Disk Total.",
- }, []string{"cluster_name", "adapter_id"})
-
- metrics = []prometheus.Collector{
- ClusterCpuUtilisationGauge,
- ClusterCpuAvailGauge,
- ClusterCpuTotalGauge,
- ClusterMemoryUtilisationGauge,
- ClusterMemoryAvailGauge,
- ClusterMemoryTotalGauge,
- ClusterDiskUtilisationGauge,
- ClusterDiskAvailGauge,
- ClusterDiskTotalGauge,
- }
- )
-
- func init() {
- prometheus.MustRegister(metrics...)
- }
-
- type Prometheus struct {
- prometheus Interface
- client v1.API
- }
-
- // NewPrometheus 初始化Prometheus客户端
- func NewPrometheus(address string) (Prometheus, error) {
- cfg := api.Config{
- Address: address,
- }
-
- promClient, err := api.NewClient(cfg)
- return Prometheus{client: v1.NewAPI(promClient)}, err
- }
-
- func NewAlertClient(address string) *client.AlertmanagerAPI {
- alertManagerClient := cli.NewAlertmanagerClient(&url.URL{Host: address})
- return alertManagerClient
- }
-
- func ParseTime(timestamp string) (time.Time, error) {
- // Parse time params
- startInt, err := strconv.ParseInt(timestamp, 10, 64)
- if err != nil {
- return time.Now(), err
- }
- return time.Unix(startInt, 0), nil
-
- }
-
- func (p Prometheus) GetNamedMetricsByTime(metrics []string, start, end string, step time.Duration, o QueryOption) []Metric {
- var res []Metric
- var mtx sync.Mutex
- var wg sync.WaitGroup
- opts := NewQueryOptions()
- o.Apply(opts)
-
- for _, metric := range metrics {
- wg.Add(1)
- go func(metric string) {
- parsedResp := Metric{MetricName: metric}
- startTimestamp, err := ParseTime(start)
- if err != nil {
- return
- }
- endTimestamp, err := ParseTime(end)
- if err != nil {
- return
- }
- timeRange := v1.Range{
- Start: startTimestamp,
- End: endTimestamp,
- Step: step,
- }
- value, _, err := p.client.QueryRange(context.Background(), makeExpr(metric, *opts), timeRange)
- if err != nil {
- parsedResp.Error = err.Error()
- } else {
- parsedResp.MetricData = parseQueryRangeResp(value, genMetricFilter(o))
- }
-
- mtx.Lock()
- res = append(res, parsedResp)
- mtx.Unlock()
-
- wg.Done()
- }(metric)
- }
-
- wg.Wait()
-
- return res
- }
-
- func parseQueryRangeResp(value model.Value, metricFilter func(metric model.Metric) bool) MetricData {
- res := MetricData{MetricType: MetricTypeMatrix}
-
- data, _ := value.(model.Matrix)
-
- for _, v := range data {
- if metricFilter != nil && !metricFilter(v.Metric) {
- continue
- }
- mv := MetricValue{
- Metadata: make(map[string]string),
- }
-
- for k, v := range v.Metric {
- mv.Metadata[string(k)] = string(v)
- }
-
- for _, k := range v.Values {
- mv.Series = append(mv.Series, Point{float64(k.Timestamp) / 1000, float64(k.Value)})
- }
-
- res.MetricValues = append(res.MetricValues, mv)
- }
-
- return res
- }
-
- func (p Prometheus) GetNamedMetrics(metrics []string, ts time.Time, o QueryOption) []Metric {
- var res []Metric
- var mtx sync.Mutex
- var wg sync.WaitGroup
-
- opts := NewQueryOptions()
- o.Apply(opts)
-
- for _, metric := range metrics {
- wg.Add(1)
- go func(metric string) {
- parsedResp := Metric{MetricName: metric}
-
- value, _, err := p.client.Query(context.Background(), makeExpr(metric, *opts), ts)
- if err != nil {
- parsedResp.Error = err.Error()
- } else {
- parsedResp.MetricData = parseQueryResp(value, genMetricFilter(o))
- }
-
- mtx.Lock()
- res = append(res, parsedResp)
- mtx.Unlock()
-
- wg.Done()
- }(metric)
- }
-
- wg.Wait()
-
- return res
- }
- func parseQueryResp(value model.Value, metricFilter func(metric model.Metric) bool) MetricData {
- res := MetricData{MetricType: MetricTypeVector}
-
- data, _ := value.(model.Vector)
-
- for _, v := range data {
- if metricFilter != nil && !metricFilter(v.Metric) {
- continue
- }
- mv := MetricValue{
- Metadata: make(map[string]string),
- }
-
- for k, v := range v.Metric {
- mv.Metadata[string(k)] = string(v)
- }
-
- mv.Sample = &Point{float64(v.Timestamp) / 1000, float64(v.Value)}
-
- res.MetricValues = append(res.MetricValues, mv)
- }
-
- return res
- }
-
- func genMetricFilter(o QueryOption) func(metric model.Metric) bool {
- if o != nil {
- if po, ok := o.(PodOption); ok {
- if po.NamespacedResourcesFilter != "" {
- namespacedPodsMap := make(map[string]struct{})
- for _, s := range strings.Split(po.NamespacedResourcesFilter, "|") {
- namespacedPodsMap[s] = struct{}{}
- }
- return func(metric model.Metric) bool {
- if len(metric) == 0 {
- return false
- }
- _, ok := namespacedPodsMap[string(metric["namespace"])+"/"+string(metric["pod"])]
- return ok
- }
- }
- }
- }
- return func(metric model.Metric) bool {
- return true
- }
- }
-
- func (p Prometheus) GetRawData(expr string, o QueryOption) (model.Value, error) {
- opts := NewQueryOptions()
- o.Apply(opts)
- value, _, err := p.client.Query(context.Background(), makeExpr(expr, *opts), time.Now())
- if err != nil {
- return nil, err
- }
- return value, nil
- }
-
- func AddAlertRule() {
-
- }
|