| 
									
										
										
										
											2016-12-26 13:50:52 -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 !nofilesystem
 | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | // +build !nofilesystem
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"golang.org/x/sys/unix" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2021-03-23 04:00:06 -07:00
										 |  |  | 	defMountPointsExcluded = "^/(dev)($|/)" | 
					
						
							|  |  |  | 	defFSTypesExcluded     = "^devfs$" | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Expose filesystem fullness.
 | 
					
						
							| 
									
										
										
										
											2017-02-28 10:47:20 -08:00
										 |  |  | func (c *filesystemCollector) GetStats() ([]filesystemStats, error) { | 
					
						
							| 
									
										
										
										
											2021-12-01 02:21:31 -08:00
										 |  |  | 	n, err := unix.Getfsstat(nil, unix.MNT_NOWAIT) | 
					
						
							| 
									
										
										
										
											2018-04-16 03:39:15 -07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	buf := make([]unix.Statfs_t, n) | 
					
						
							| 
									
										
										
										
											2021-12-01 02:21:31 -08:00
										 |  |  | 	_, err = unix.Getfsstat(buf, unix.MNT_NOWAIT) | 
					
						
							| 
									
										
										
										
											2018-04-16 03:39:15 -07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-28 10:47:20 -08:00
										 |  |  | 	stats := []filesystemStats{} | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 	for _, fs := range buf { | 
					
						
							| 
									
										
										
										
											2022-05-23 06:44:16 -07:00
										 |  |  | 		mountpoint := unix.ByteSliceToString(fs.Mntonname[:]) | 
					
						
							| 
									
										
										
										
											2024-11-05 01:36:13 -08:00
										 |  |  | 		if c.mountPointFilter.ignored(mountpoint) { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 			c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint) | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-23 06:44:16 -07:00
										 |  |  | 		device := unix.ByteSliceToString(fs.Mntfromname[:]) | 
					
						
							|  |  |  | 		fstype := unix.ByteSliceToString(fs.Fstypename[:]) | 
					
						
							| 
									
										
										
										
											2024-11-05 01:36:13 -08:00
										 |  |  | 		if c.fsTypeFilter.ignored(fstype) { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 			c.logger.Debug("Ignoring fs type", "type", fstype) | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 02:21:31 -08:00
										 |  |  | 		if (fs.Flags & unix.MNT_IGNORE) != 0 { | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 			c.logger.Debug("Ignoring mount flagged as ignore", "mountpoint", mountpoint) | 
					
						
							| 
									
										
										
										
											2021-12-01 02:21:31 -08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 		var ro float64 | 
					
						
							| 
									
										
										
										
											2021-12-01 02:21:31 -08:00
										 |  |  | 		if (fs.Flags & unix.MNT_RDONLY) != 0 { | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 			ro = 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		stats = append(stats, filesystemStats{ | 
					
						
							| 
									
										
										
										
											2017-01-03 06:55:40 -08:00
										 |  |  | 			labels: filesystemLabels{ | 
					
						
							|  |  |  | 				device:     device, | 
					
						
							| 
									
										
										
										
											2019-07-19 07:51:17 -07:00
										 |  |  | 				mountPoint: rootfsStripPrefix(mountpoint), | 
					
						
							| 
									
										
										
										
											2017-01-03 06:55:40 -08:00
										 |  |  | 				fsType:     fstype, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			size:      float64(fs.Blocks) * float64(fs.Bsize), | 
					
						
							|  |  |  | 			free:      float64(fs.Bfree) * float64(fs.Bsize), | 
					
						
							|  |  |  | 			avail:     float64(fs.Bavail) * float64(fs.Bsize), | 
					
						
							|  |  |  | 			files:     float64(fs.Files), | 
					
						
							|  |  |  | 			filesFree: float64(fs.Ffree), | 
					
						
							|  |  |  | 			ro:        ro, | 
					
						
							| 
									
										
										
										
											2016-12-26 13:50:52 -08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return stats, nil | 
					
						
							|  |  |  | } |