| 
									
										
										
										
											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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-03 04:35:24 -07:00
										 |  |  | //go:build !noentropy
 | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | // +build !noentropy
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 	"github.com/prometheus/procfs" | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type entropyCollector struct { | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 	fs              procfs.FS | 
					
						
							|  |  |  | 	entropyAvail    *prometheus.Desc | 
					
						
							|  |  |  | 	entropyPoolSize *prometheus.Desc | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger          *slog.Logger | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 	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.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewEntropyCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 	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{ | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 		fs: fs, | 
					
						
							| 
									
										
										
										
											2017-02-28 08:44:53 -08:00
										 |  |  | 		entropyAvail: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 			prometheus.BuildFQName(namespace, "", "entropy_available_bits"), | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | 			"Bits of available entropy.", | 
					
						
							|  |  |  | 			nil, nil, | 
					
						
							|  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 		entropyPoolSize: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2020-06-16 08:29:22 -07:00
										 |  |  | 			prometheus.BuildFQName(namespace, "", "entropy_pool_size_bits"), | 
					
						
							|  |  |  | 			"Bits of entropy pool.", | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 			nil, nil, | 
					
						
							|  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 		logger: logger, | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | 	}, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:47:20 -08:00
										 |  |  | func (c *entropyCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 	stats, err := c.fs.KernelRandom() | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 		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 { | 
					
						
							| 
									
										
										
										
											2020-06-18 22:50:51 -07:00
										 |  |  | 		return fmt.Errorf("couldn't get entropy poolsize") | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric( | 
					
						
							| 
									
										
										
										
											2020-06-15 21:35:51 -07:00
										 |  |  | 		c.entropyPoolSize, prometheus.GaugeValue, float64(*stats.PoolSize)) | 
					
						
							| 
									
										
										
										
											2016-01-12 15:32:50 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |