| 
									
										
										
										
											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 !nosockstat
 | 
					
						
							| 
									
										
										
										
											2015-09-09 09:37:48 -07:00
										 |  |  | // +build !nosockstat
 | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-08-11 16:36:42 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	"github.com/prometheus/procfs" | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	sockStatSubsystem = "sockstat" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-09 11:05:16 -07:00
										 |  |  | // Used for calculating the total memory bytes on TCP and UDP.
 | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | var pageSize = os.Getpagesize() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | type sockStatCollector struct { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger *slog.Logger | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 	registerCollector(sockStatSubsystem, defaultEnabled, NewSockStatCollector) | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-09 11:05:16 -07:00
										 |  |  | // NewSockStatCollector returns a new Collector exposing socket stats.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewSockStatCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 	return &sockStatCollector{logger}, nil | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:47:20 -08:00
										 |  |  | func (c *sockStatCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	fs, err := procfs.NewFS(*procPath) | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return fmt.Errorf("failed to open procfs: %w", err) | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// If IPv4 and/or IPv6 are disabled on this kernel, handle it gracefully.
 | 
					
						
							|  |  |  | 	stat4, err := fs.NetSockstat() | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case err == nil: | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 	case errors.Is(err, os.ErrNotExist): | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 		c.logger.Debug("IPv4 sockstat statistics not found, skipping") | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return fmt.Errorf("failed to get IPv4 sockstat data: %w", err) | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	stat6, err := fs.NetSockstat6() | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case err == nil: | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 	case errors.Is(err, os.ErrNotExist): | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 		c.logger.Debug("IPv6 sockstat statistics not found, skipping") | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return fmt.Errorf("failed to get IPv6 sockstat data: %w", err) | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	stats := []struct { | 
					
						
							|  |  |  | 		isIPv6 bool | 
					
						
							|  |  |  | 		stat   *procfs.NetSockstat | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			stat: stat4, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			isIPv6: true, | 
					
						
							|  |  |  | 			stat:   stat6, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	for _, s := range stats { | 
					
						
							|  |  |  | 		c.update(ch, s.isIPv6, s.stat) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | func (c *sockStatCollector) update(ch chan<- prometheus.Metric, isIPv6 bool, s *procfs.NetSockstat) { | 
					
						
							|  |  |  | 	if s == nil { | 
					
						
							|  |  |  | 		// IPv6 disabled or similar; nothing to do.
 | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// If sockstat contains the number of used sockets, export it.
 | 
					
						
							|  |  |  | 	if !isIPv6 && s.Used != nil { | 
					
						
							|  |  |  | 		// TODO: this must be updated if sockstat6 ever exports this data.
 | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 			prometheus.NewDesc( | 
					
						
							|  |  |  | 				prometheus.BuildFQName(namespace, sockStatSubsystem, "sockets_used"), | 
					
						
							|  |  |  | 				"Number of IPv4 sockets in use.", | 
					
						
							|  |  |  | 				nil, | 
					
						
							|  |  |  | 				nil, | 
					
						
							|  |  |  | 			), | 
					
						
							|  |  |  | 			prometheus.GaugeValue, | 
					
						
							|  |  |  | 			float64(*s.Used), | 
					
						
							|  |  |  | 		) | 
					
						
							| 
									
										
										
										
											2017-02-28 10:31:35 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	// A name and optional value for a sockstat metric.
 | 
					
						
							|  |  |  | 	type ssPair struct { | 
					
						
							|  |  |  | 		name string | 
					
						
							|  |  |  | 		v    *int | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 	// Previously these metric names were generated directly from the file output.
 | 
					
						
							|  |  |  | 	// In order to keep the same level of compatibility, we must map the fields
 | 
					
						
							|  |  |  | 	// to their correct names.
 | 
					
						
							|  |  |  | 	for _, p := range s.Protocols { | 
					
						
							|  |  |  | 		pairs := []ssPair{ | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "inuse", | 
					
						
							|  |  |  | 				v:    &p.InUse, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "orphan", | 
					
						
							|  |  |  | 				v:    p.Orphan, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "tw", | 
					
						
							|  |  |  | 				v:    p.TW, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "alloc", | 
					
						
							|  |  |  | 				v:    p.Alloc, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "mem", | 
					
						
							|  |  |  | 				v:    p.Mem, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				name: "memory", | 
					
						
							|  |  |  | 				v:    p.Memory, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-07-05 14:22:26 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-25 11:41:38 -08:00
										 |  |  | 		// Also export mem_bytes values for sockets which have a mem value
 | 
					
						
							|  |  |  | 		// stored in pages.
 | 
					
						
							|  |  |  | 		if p.Mem != nil { | 
					
						
							|  |  |  | 			v := *p.Mem * pageSize | 
					
						
							|  |  |  | 			pairs = append(pairs, ssPair{ | 
					
						
							|  |  |  | 				name: "mem_bytes", | 
					
						
							|  |  |  | 				v:    &v, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, pair := range pairs { | 
					
						
							|  |  |  | 			if pair.v == nil { | 
					
						
							|  |  |  | 				// This value is not set for this protocol; nothing to do.
 | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 				prometheus.NewDesc( | 
					
						
							|  |  |  | 					prometheus.BuildFQName( | 
					
						
							|  |  |  | 						namespace, | 
					
						
							|  |  |  | 						sockStatSubsystem, | 
					
						
							|  |  |  | 						fmt.Sprintf("%s_%s", p.Protocol, pair.name), | 
					
						
							|  |  |  | 					), | 
					
						
							|  |  |  | 					fmt.Sprintf("Number of %s sockets in state %s.", p.Protocol, pair.name), | 
					
						
							|  |  |  | 					nil, | 
					
						
							|  |  |  | 					nil, | 
					
						
							|  |  |  | 				), | 
					
						
							|  |  |  | 				prometheus.GaugeValue, | 
					
						
							|  |  |  | 				float64(*pair.v), | 
					
						
							|  |  |  | 			) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-04 12:34:10 -07:00
										 |  |  | } |