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