2016-12-27 12:46:25 +01:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2023-01-03 20:16:40 +01:00
|
|
|
//go:build (darwin || linux || openbsd || netbsd) && !nomeminfo
|
|
|
|
// +build darwin linux openbsd netbsd
|
2016-12-27 12:46:25 +01:00
|
|
|
// +build !nomeminfo
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-07 22:27:52 +02:00
|
|
|
"strings"
|
2016-12-27 12:46:25 +01:00
|
|
|
|
2020-11-14 11:53:51 +01:00
|
|
|
"github.com/go-kit/log/level"
|
2016-12-27 12:46:25 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
memInfoSubsystem = "memory"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("meminfo", defaultEnabled, NewMeminfoCollector)
|
2016-12-27 12:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update calls (*meminfoCollector).getMemInfo to get the platform specific
|
|
|
|
// memory metrics.
|
2017-02-28 19:47:20 +01:00
|
|
|
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) error {
|
2018-09-07 22:27:52 +02:00
|
|
|
var metricType prometheus.ValueType
|
2016-12-27 12:46:25 +01:00
|
|
|
memInfo, err := c.getMemInfo()
|
|
|
|
if err != nil {
|
2020-06-15 22:27:14 +02:00
|
|
|
return fmt.Errorf("couldn't get meminfo: %w", err)
|
2016-12-27 12:46:25 +01:00
|
|
|
}
|
2024-07-14 14:27:55 +02:00
|
|
|
level.Debug(c.logger).Log("msg", "Set node_mem", "memInfo", fmt.Sprintf("%v", memInfo))
|
2016-12-27 12:46:25 +01:00
|
|
|
for k, v := range memInfo {
|
2018-09-07 22:27:52 +02:00
|
|
|
if strings.HasSuffix(k, "_total") {
|
|
|
|
metricType = prometheus.CounterValue
|
|
|
|
} else {
|
|
|
|
metricType = prometheus.GaugeValue
|
|
|
|
}
|
2016-12-27 12:46:25 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, memInfoSubsystem, k),
|
2016-12-27 12:46:25 +01:00
|
|
|
fmt.Sprintf("Memory information field %s.", k),
|
|
|
|
nil, nil,
|
|
|
|
),
|
2018-09-07 22:27:52 +02:00
|
|
|
metricType, v,
|
2016-12-27 12:46:25 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|