| 
									
										
										
										
											2017-02-07 06:30:49 -08:00
										 |  |  | // Copyright 2017 The Prometheus Authors
 | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | // 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 !nobuddyinfo && !netbsd
 | 
					
						
							|  |  |  | // +build !nobuddyinfo,!netbsd
 | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 	"strconv" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/client_golang/prometheus" | 
					
						
							| 
									
										
										
										
											2017-02-07 06:30:49 -08:00
										 |  |  | 	"github.com/prometheus/procfs" | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	buddyInfoSubsystem = "buddyinfo" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type buddyinfoCollector struct { | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 	fs     procfs.FS | 
					
						
							|  |  |  | 	desc   *prometheus.Desc | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	logger *slog.Logger | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							| 
									
										
										
										
											2017-09-28 06:06:26 -07:00
										 |  |  | 	registerCollector("buddyinfo", defaultDisabled, NewBuddyinfoCollector) | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewBuddyinfoCollector returns a new Collector exposing buddyinfo stats.
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | func NewBuddyinfoCollector(logger *slog.Logger) (Collector, error) { | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 	desc := prometheus.NewDesc( | 
					
						
							| 
									
										
										
										
											2018-02-12 09:53:31 -08:00
										 |  |  | 		prometheus.BuildFQName(namespace, buddyInfoSubsystem, "blocks"), | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 		"Count of free blocks according to size.", | 
					
						
							|  |  |  | 		[]string{"node", "zone", "size"}, nil, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2019-04-10 09:16:12 -07:00
										 |  |  | 	fs, err := procfs.NewFS(*procPath) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-11-29 05:51:31 -08:00
										 |  |  | 		return nil, fmt.Errorf("failed to open procfs: %w", err) | 
					
						
							| 
									
										
										
										
											2019-04-10 09:16:12 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-12-31 08:19:37 -08:00
										 |  |  | 	return &buddyinfoCollector{fs, desc, logger}, nil | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Update calls (*buddyinfoCollector).getBuddyInfo to get the platform specific
 | 
					
						
							|  |  |  | // buddyinfo metrics.
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:47:20 -08:00
										 |  |  | func (c *buddyinfoCollector) Update(ch chan<- prometheus.Metric) error { | 
					
						
							| 
									
										
										
										
											2019-06-12 11:47:16 -07:00
										 |  |  | 	buddyInfo, err := c.fs.BuddyInfo() | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-06-15 13:27:14 -07:00
										 |  |  | 		return fmt.Errorf("couldn't get buddyinfo: %w", err) | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-07 06:30:49 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	c.logger.Debug("Set node_buddy", "buddyInfo", buddyInfo) | 
					
						
							| 
									
										
										
										
											2017-02-07 08:48:07 -08:00
										 |  |  | 	for _, entry := range buddyInfo { | 
					
						
							|  |  |  | 		for size, value := range entry.Sizes { | 
					
						
							|  |  |  | 			ch <- prometheus.MustNewConstMetric( | 
					
						
							|  |  |  | 				c.desc, | 
					
						
							|  |  |  | 				prometheus.GaugeValue, value, | 
					
						
							|  |  |  | 				entry.Node, entry.Zone, strconv.Itoa(size), | 
					
						
							|  |  |  | 			) | 
					
						
							| 
									
										
										
										
											2017-02-06 07:56:58 -08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |