node_exporter/collector/entropy_linux.go

81 lines
2.2 KiB
Go
Raw Normal View History

2016-01-12 15:32:50 -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.
//go:build !noentropy
2016-01-12 15:32:50 -08:00
// +build !noentropy
package collector
import (
"fmt"
"github.com/go-kit/log"
2016-01-12 15:32:50 -08:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
2016-01-12 15:32:50 -08:00
)
type entropyCollector struct {
fs procfs.FS
entropyAvail *prometheus.Desc
entropyPoolSize *prometheus.Desc
logger log.Logger
2016-01-12 15:32:50 -08:00
}
func init() {
registerCollector("entropy", defaultEnabled, NewEntropyCollector)
2016-01-12 15:32:50 -08:00
}
2017-02-28 08:44:53 -08:00
// NewEntropyCollector returns a new Collector exposing entropy stats.
func NewEntropyCollector(logger log.Logger) (Collector, error) {
fs, err := procfs.NewFS(*procPath)
if err != nil {
return nil, fmt.Errorf("failed to open procfs: %w", err)
}
2016-01-12 15:32:50 -08:00
return &entropyCollector{
fs: fs,
2017-02-28 08:44:53 -08:00
entropyAvail: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "entropy_available_bits"),
2016-01-12 15:32:50 -08:00
"Bits of available entropy.",
nil, nil,
),
entropyPoolSize: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "entropy_pool_size_bits"),
"Bits of entropy pool.",
nil, nil,
),
logger: logger,
2016-01-12 15:32:50 -08:00
}, nil
}
func (c *entropyCollector) Update(ch chan<- prometheus.Metric) error {
stats, err := c.fs.KernelRandom()
2016-01-12 15:32:50 -08:00
if err != nil {
return fmt.Errorf("failed to get kernel random stats: %w", err)
}
if stats.EntropyAvaliable == nil {
return fmt.Errorf("couldn't get entropy_avail")
}
ch <- prometheus.MustNewConstMetric(
c.entropyAvail, prometheus.GaugeValue, float64(*stats.EntropyAvaliable))
if stats.PoolSize == nil {
return fmt.Errorf("couldn't get entropy poolsize")
2016-01-12 15:32:50 -08:00
}
ch <- prometheus.MustNewConstMetric(
c.entropyPoolSize, prometheus.GaugeValue, float64(*stats.PoolSize))
2016-01-12 15:32:50 -08:00
return nil
}