aboutsummaryrefslogtreecommitdiff
path: root/collector/xfs_linux.go
blob: 36dfff50003460f770ef90ca15fd748817c2ba35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2017 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.

// +build !noxfs

package collector

import (
	"fmt"

	"github.com/go-kit/kit/log"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/procfs/xfs"
)

// An xfsCollector is a Collector which gathers metrics from XFS filesystems.
type xfsCollector struct {
	fs     xfs.FS
	logger log.Logger
}

func init() {
	registerCollector("xfs", defaultEnabled, NewXFSCollector)
}

// NewXFSCollector returns a new Collector exposing XFS statistics.
func NewXFSCollector(logger log.Logger) (Collector, error) {
	fs, err := xfs.NewFS(*procPath, *sysPath)
	if err != nil {
		return nil, fmt.Errorf("failed to open sysfs: %w", err)
	}

	return &xfsCollector{
		fs:     fs,
		logger: logger,
	}, nil
}

// Update implements Collector.
func (c *xfsCollector) Update(ch chan<- prometheus.Metric) error {
	stats, err := c.fs.SysStats()
	if err != nil {
		return fmt.Errorf("failed to retrieve XFS stats: %w", err)
	}

	for _, s := range stats {
		c.updateXFSStats(ch, s)
	}

	return nil
}

// updateXFSStats collects statistics for a single XFS filesystem.
func (c *xfsCollector) updateXFSStats(ch chan<- prometheus.Metric, s *xfs.Stats) {
	const (
		subsystem = "xfs"
	)

	var (
		labels = []string{"device"}
	)

	// Metric names and descriptions are sourced from:
	// http://xfs.org/index.php/Runtime_Stats.
	//
	// Each metric has a name that roughly follows the pattern of
	// "node_xfs_category_value_total", using the categories and value names
	// found on the XFS wiki.
	//
	// Note that statistics for more than one internal B-tree are measured,
	// and as such, each one must be differentiated by name.
	metrics := []struct {
		name  string
		desc  string
		value float64
	}{
		{
			name:  "extent_allocation_extents_allocated_total",
			desc:  "Number of extents allocated for a filesystem.",
			value: float64(s.ExtentAllocation.ExtentsAllocated),
		},
		{
			name:  "extent_allocation_blocks_allocated_total",
			desc:  "Number of blocks allocated for a filesystem.",
			value: float64(s.ExtentAllocation.BlocksAllocated),
		},
		{
			name:  "extent_allocation_extents_freed_total",
			desc:  "Number of extents freed for a filesystem.",
			value: float64(s.ExtentAllocation.ExtentsFreed),
		},
		{
			name:  "extent_allocation_blocks_freed_total",
			desc:  "Number of blocks freed for a filesystem.",
			value: float64(s.ExtentAllocation.BlocksFreed),
		},
		{
			name:  "allocation_btree_lookups_total",
			desc:  "Number of allocation B-tree lookups for a filesystem.",
			value: float64(s.AllocationBTree.Lookups),
		},
		{
			name:  "allocation_btree_compares_total",
			desc:  "Number of allocation B-tree compares for a filesystem.",
			value: float64(s.AllocationBTree.Compares),
		},
		{
			name:  "allocation_btree_records_inserted_total",
			desc:  "Number of allocation B-tree records inserted for a filesystem.",
			value: float64(s.AllocationBTree.RecordsInserted),
		},
		{
			name:  "allocation_btree_records_deleted_total",
			desc:  "Number of allocation B-tree records deleted for a filesystem.",
			value: float64(s.AllocationBTree.RecordsDeleted),
		},
		{
			name:  "block_mapping_reads_total",
			desc:  "Number of block map for read operations for a filesystem.",
			value: float64(s.BlockMapping.Reads),
		},
		{
			name:  "block_mapping_writes_total",
			desc:  "Number of block map for write operations for a filesystem.",
			value: float64(s.BlockMapping.Writes),
		},
		{
			name:  "block_mapping_unmaps_total",
			desc:  "Number of block unmaps (deletes) for a filesystem.",
			value: float64(s.BlockMapping.Unmaps),
		},
		{
			name:  "block_mapping_extent_list_insertions_total",
			desc:  "Number of extent list insertions for a filesystem.",
			value: float64(s.BlockMapping.ExtentListInsertions),
		},
		{
			name:  "block_mapping_extent_list_deletions_total",
			desc:  "Number of extent list deletions for a filesystem.",
			value: float64(s.BlockMapping.ExtentListDeletions),
		},
		{
			name:  "block_mapping_extent_list_lookups_total",
			desc:  "Number of extent list lookups for a filesystem.",
			value: float64(s.BlockMapping.ExtentListLookups),
		},
		{
			name:  "block_mapping_extent_list_compares_total",
			desc:  "Number of extent list compares for a filesystem.",
			value: float64(s.BlockMapping.ExtentListCompares),
		},
		{
			name:  "block_map_btree_lookups_total",
			desc:  "Number of block map B-tree lookups for a filesystem.",
			value: float64(s.BlockMapBTree.Lookups),
		},
		{
			name:  "block_map_btree_compares_total",
			desc:  "Number of block map B-tree compares for a filesystem.",
			value: float64(s.BlockMapBTree.Compares),
		},
		{
			name:  "block_map_btree_records_inserted_total",
			desc:  "Number of block map B-tree records inserted for a filesystem.",
			value: float64(s.BlockMapBTree.RecordsInserted),
		},
		{
			name:  "block_map_btree_records_deleted_total",
			desc:  "Number of block map B-tree records deleted for a filesystem.",
			value: float64(s.BlockMapBTree.RecordsDeleted),
		},
		{
			name:  "directory_operation_lookup_total",
			desc:  "Number of file name directory lookups which miss the operating systems directory name lookup cache.",
			value: float64(s.DirectoryOperation.Lookups),
		},
		{
			name:  "directory_operation_create_total",
			desc:  "Number of times a new directory entry was created for a filesystem.",
			value: float64(s.DirectoryOperation.Creates),
		},
		{
			name:  "directory_operation_remove_total",
			desc:  "Number of times an existing directory entry was created for a filesystem.",
			value: float64(s.DirectoryOperation.Removes),
		},
		{
			name:  "directory_operation_getdents_total",
			desc:  "Number of times the directory getdents operation was performed for a filesystem.",
			value: float64(s.DirectoryOperation.Getdents),
		},
		{
			name:  "read_calls_total",
			desc:  "Number of read(2) system calls made to files in a filesystem.",
			value: float64(s.ReadWrite.Read),
		},
		{
			name:  "write_calls_total",
			desc:  "Number of write(2) system calls made to files in a filesystem.",
			value: float64(s.ReadWrite.Write),
		},
		{
			name:  "vnode_active_total",
			desc:  "Number of vnodes not on free lists for a filesystem.",
			value: float64(s.Vnode.Active),
		},
		{
			name:  "vnode_allocate_total",
			desc:  "Number of times vn_alloc called for a filesystem.",
			value: float64(s.Vnode.Allocate),
		},
		{
			name:  "vnode_get_total",
			desc:  "Number of times vn_get called for a filesystem.",
			value: float64(s.Vnode.Get),
		},
		{
			name:  "vnode_hold_total",
			desc:  "Number of times vn_hold called for a filesystem.",
			value: float64(s.Vnode.Hold),
		},
		{
			name:  "vnode_release_total",
			desc:  "Number of times vn_rele called for a filesystem.",
			value: float64(s.Vnode.Release),
		},
		{
			name:  "vnode_reclaim_total",
			desc:  "Number of times vn_reclaim called for a filesystem.",
			value: float64(s.Vnode.Reclaim),
		},
		{
			name:  "vnode_remove_total",
			desc:  "Number of times vn_remove called for a filesystem.",
			value: float64(s.Vnode.Remove),
		},
	}

	for _, m := range metrics {
		desc := prometheus.NewDesc(
			prometheus.BuildFQName(namespace, subsystem, m.name),
			m.desc,
			labels,
			nil,
		)

		ch <- prometheus.MustNewConstMetric(
			desc,
			prometheus.CounterValue,
			m.value,
			s.Name,
		)
	}
}