| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | // Copyright 2016 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 !nonfs
 | 
					
						
							| 
									
										
										
										
											2020-06-12 01:26:30 -07:00
										 |  |  | // +build !nonfs
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	"reflect" | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	"github.com/prometheus/procfs/nfs" | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	nfsSubsystem = "nfs" | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | type nfsCollector struct { | 
					
						
							| 
									
										
										
										
											2019-05-06 21:38:21 -07:00
										 |  |  | 	fs                                nfs.FS | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	nfsNetReadsDesc                   *prometheus.Desc | 
					
						
							|  |  |  | 	nfsNetConnectionsDesc             *prometheus.Desc | 
					
						
							|  |  |  | 	nfsRPCOperationsDesc              *prometheus.Desc | 
					
						
							|  |  |  | 	nfsRPCRetransmissionsDesc         *prometheus.Desc | 
					
						
							|  |  |  | 	nfsRPCAuthenticationRefreshesDesc *prometheus.Desc | 
					
						
							|  |  |  | 	nfsProceduresDesc                 *prometheus.Desc | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger                            *slog.Logger | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2018-02-16 06:42:47 -08:00
										 |  |  | 	registerCollector("nfs", defaultEnabled, NewNfsCollector) | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 08:44:53 -08:00
										 |  |  | // NewNfsCollector returns a new Collector exposing NFS statistics.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewNfsCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2019-05-06 21:38:21 -07:00
										 |  |  | 	fs, err := nfs.NewFS(*procPath) | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return nil, fmt.Errorf("failed to open procfs: %w", err) | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &nfsCollector{ | 
					
						
							|  |  |  | 		fs: fs, | 
					
						
							|  |  |  | 		nfsNetReadsDesc: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			prometheus.BuildFQName(namespace, nfsSubsystem, "packets_total"), | 
					
						
							|  |  |  | 			"Total NFSd network packets (sent+received) by protocol type.", | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 			[]string{"protocol"}, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nfsNetConnectionsDesc: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			prometheus.BuildFQName(namespace, nfsSubsystem, "connections_total"), | 
					
						
							|  |  |  | 			"Total number of NFSd TCP connections.", | 
					
						
							|  |  |  | 			nil, | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nfsRPCOperationsDesc: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			prometheus.BuildFQName(namespace, nfsSubsystem, "rpcs_total"), | 
					
						
							|  |  |  | 			"Total number of RPCs performed.", | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 			nil, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nfsRPCRetransmissionsDesc: prometheus.NewDesc( | 
					
						
							|  |  |  | 			prometheus.BuildFQName(namespace, nfsSubsystem, "rpc_retransmissions_total"), | 
					
						
							|  |  |  | 			"Number of RPC transmissions performed.", | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nfsRPCAuthenticationRefreshesDesc: prometheus.NewDesc( | 
					
						
							|  |  |  | 			prometheus.BuildFQName(namespace, nfsSubsystem, "rpc_authentication_refreshes_total"), | 
					
						
							|  |  |  | 			"Number of RPC authentication refreshes performed.", | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nfsProceduresDesc: prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			prometheus.BuildFQName(namespace, nfsSubsystem, "requests_total"), | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 			"Number of NFS procedures invoked.", | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			[]string{"proto", "method"}, | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 			nil, | 
					
						
							|  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 		logger: logger, | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	}, nil | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:47:20 -08:00
										 |  |  | func (c *nfsCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							| 
									
										
										
										
											2019-05-06 21:38:21 -07:00
										 |  |  | 	stats, err := c.fs.ClientRPCStats() | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 		if errors.Is(err, os.ErrNotExist) { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 			c.logger.Debug("Not collecting NFS metrics", "err", err) | 
					
						
							| 
									
										
										
										
											2020-02-19 07:11:29 -08:00
										 |  |  | 			return ErrNoData | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return fmt.Errorf("failed to retrieve nfs stats: %w", err) | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.updateNFSNetworkStats(ch, &stats.Network) | 
					
						
							|  |  |  | 	c.updateNFSClientRPCStats(ch, &stats.ClientRPC) | 
					
						
							|  |  |  | 	c.updateNFSRequestsv2Stats(ch, &stats.V2Stats) | 
					
						
							|  |  |  | 	c.updateNFSRequestsv3Stats(ch, &stats.V3Stats) | 
					
						
							|  |  |  | 	c.updateNFSRequestsv4Stats(ch, &stats.ClientV4Stats) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // updateNFSNetworkStats collects statistics for network packets/connections.
 | 
					
						
							|  |  |  | func (c *nfsCollector) updateNFSNetworkStats(ch chan<- prometheus.Metric, s *nfs.Network) { | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric(c.nfsNetReadsDesc, prometheus.CounterValue, | 
					
						
							|  |  |  | 		float64(s.UDPCount), "udp") | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric(c.nfsNetReadsDesc, prometheus.CounterValue, | 
					
						
							|  |  |  | 		float64(s.TCPCount), "tcp") | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric(c.nfsNetConnectionsDesc, prometheus.CounterValue, | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 		float64(s.TCPConnect)) | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // updateNFSClientRPCStats collects statistics for kernel server RPCs.
 | 
					
						
							|  |  |  | func (c *nfsCollector) updateNFSClientRPCStats(ch chan<- prometheus.Metric, s *nfs.ClientRPC) { | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric(c.nfsRPCOperationsDesc, prometheus.CounterValue, | 
					
						
							|  |  |  | 		float64(s.RPCCount)) | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric(c.nfsRPCRetransmissionsDesc, prometheus.CounterValue, | 
					
						
							|  |  |  | 		float64(s.Retransmissions)) | 
					
						
							|  |  |  | 	ch <- prometheus.MustNewConstMetric(c.nfsRPCAuthenticationRefreshesDesc, prometheus.CounterValue, | 
					
						
							|  |  |  | 		float64(s.AuthRefreshes)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // updateNFSRequestsv2Stats collects statistics for NFSv2 requests.
 | 
					
						
							|  |  |  | func (c *nfsCollector) updateNFSRequestsv2Stats(ch chan<- prometheus.Metric, s *nfs.V2Stats) { | 
					
						
							|  |  |  | 	const proto = "2" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	v := reflect.ValueOf(s).Elem() | 
					
						
							|  |  |  | 	for i := 0; i < v.NumField(); i++ { | 
					
						
							|  |  |  | 		field := v.Field(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric(c.nfsProceduresDesc, prometheus.CounterValue, | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			float64(field.Uint()), proto, v.Type().Field(i).Name) | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // updateNFSRequestsv3Stats collects statistics for NFSv3 requests.
 | 
					
						
							|  |  |  | func (c *nfsCollector) updateNFSRequestsv3Stats(ch chan<- prometheus.Metric, s *nfs.V3Stats) { | 
					
						
							|  |  |  | 	const proto = "3" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	v := reflect.ValueOf(s).Elem() | 
					
						
							|  |  |  | 	for i := 0; i < v.NumField(); i++ { | 
					
						
							|  |  |  | 		field := v.Field(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric(c.nfsProceduresDesc, prometheus.CounterValue, | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			float64(field.Uint()), proto, v.Type().Field(i).Name) | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 04:40:38 -08:00
										 |  |  | // updateNFSRequestsv4Stats collects statistics for NFSv4 requests.
 | 
					
						
							|  |  |  | func (c *nfsCollector) updateNFSRequestsv4Stats(ch chan<- prometheus.Metric, s *nfs.ClientV4Stats) { | 
					
						
							|  |  |  | 	const proto = "4" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	v := reflect.ValueOf(s).Elem() | 
					
						
							|  |  |  | 	for i := 0; i < v.NumField(); i++ { | 
					
						
							|  |  |  | 		field := v.Field(i) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ch <- prometheus.MustNewConstMetric(c.nfsProceduresDesc, prometheus.CounterValue, | 
					
						
							| 
									
										
										
										
											2018-02-20 22:25:41 -08:00
										 |  |  | 			float64(field.Uint()), proto, v.Type().Field(i).Name) | 
					
						
							| 
									
										
										
										
											2016-11-29 05:32:52 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |