| 
									
										
										
										
											2015-09-26 08:36:40 -07: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 !nofilefd
 | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | // +build !nofilefd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2022-07-27 11:59:39 -07:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	"os" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2016-08-11 16:21:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	fileFDStatSubsystem = "filefd" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | type fileFDStatCollector struct { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger *slog.Logger | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 	registerCollector(fileFDStatSubsystem, defaultEnabled, NewFileFDStatCollector) | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewFileFDStatCollector returns a new Collector exposing file-nr stats.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewFileFDStatCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 	return &fileFDStatCollector{logger}, nil | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | func (c *fileFDStatCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							|  |  |  | 	fileFDStat, err := parseFileFDStats(procFilePath("sys/fs/file-nr")) | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 		return fmt.Errorf("couldn't get file-nr: %w", err) | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for name, value := range fileFDStat { | 
					
						
							| 
									
										
										
										
											2016-01-13 06:09:01 -08:00
										 |  |  | 		v, err := strconv.ParseFloat(value, 64) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 			return fmt.Errorf("invalid value %s in file-nr: %w", value, err) | 
					
						
							| 
									
										
										
										
											2016-01-13 06:09:01 -08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-08-11 16:21:00 -07:00
										 |  |  | 		ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 			prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 				prometheus.BuildFQName(namespace, fileFDStatSubsystem, name), | 
					
						
							| 
									
										
										
										
											2016-08-11 16:21:00 -07:00
										 |  |  | 				fmt.Sprintf("File descriptor statistics: %s.", name), | 
					
						
							|  |  |  | 				nil, nil, | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			prometheus.GaugeValue, v, | 
					
						
							|  |  |  | 		) | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-11 16:21:00 -07:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | func parseFileFDStats(filename string) (map[string]string, error) { | 
					
						
							|  |  |  | 	file, err := os.Open(filename) | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	defer file.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 11:59:39 -07:00
										 |  |  | 	content, err := io.ReadAll(file) | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	parts := bytes.Split(bytes.TrimSpace(content), []byte("\u0009")) | 
					
						
							|  |  |  | 	if len(parts) < 3 { | 
					
						
							|  |  |  | 		return nil, fmt.Errorf("unexpected number of file stats in %q", filename) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 	var fileFDStat = map[string]string{} | 
					
						
							|  |  |  | 	// The file-nr proc is only 1 line with 3 values.
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | 	fileFDStat["allocated"] = string(parts[0]) | 
					
						
							| 
									
										
										
										
											2016-06-20 09:08:26 -07:00
										 |  |  | 	// The second value is skipped as it will always be zero in linux 2.6.
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | 	fileFDStat["maximum"] = string(parts[2]) | 
					
						
							| 
									
										
										
										
											2015-09-08 07:14:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return fileFDStat, nil | 
					
						
							|  |  |  | } |