Skip to content

Commit b794543

Browse files
Muhammad Shahzebfs185143
authored andcommitted
Add Disk IO stats and ext4 FS stats (prometheus#651)
Adds function to return disk stats from: * /sys/block/<disk>/device/ioerr_cnt: number of SCSI commands that completed with an error * /sys/block/<disk>/device/iodone_cnt: number of completed or rejected SCSI commands Add function to return stats for ext4 filesystem * /sys/fs/ext4/<partition>/errors_count: number of ext4 errors * /sys/fs/ext4/<partition>/warning_count: number of ext4 warning log messages * /sys/fs/ext4/<partition>/msg_count: number of other ext4 log messages Signed-off-by: Muhammad Shahzeb <[email protected]> Signed-off-by: fs185143 <[email protected]>
1 parent a7e955a commit b794543

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

blockdevice/stats.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ type BlockQueueStats struct {
178178
WriteZeroesMaxBytes uint64
179179
}
180180

181+
type IODeviceStats struct {
182+
IODoneCount uint64
183+
IOErrCount uint64
184+
}
185+
181186
// DeviceMapperInfo models the devicemapper files that are located in the sysfs tree for each block device
182187
// and described in the kernel documentation:
183188
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm
@@ -210,6 +215,7 @@ const (
210215
sysBlockDM = "dm"
211216
sysUnderlyingDev = "slaves"
212217
sysBlockSize = "size"
218+
sysDevicePath = "device"
213219
)
214220

215221
// FS represents the pseudo-filesystems proc and sys, which provides an
@@ -485,3 +491,25 @@ func (fs FS) SysBlockDeviceSize(device string) (uint64, error) {
485491
}
486492
return filesystem.SectorSize * size, nil
487493
}
494+
495+
// SysBlockDeviceIO returns stats for the block device io counters
496+
// IO done count: /sys/block/<disk>/device/iodone_cnt
497+
// IO error count: /sys/block/<disk>/device/ioerr_cnt.
498+
func (fs FS) SysBlockDeviceIOStat(device string) (IODeviceStats, error) {
499+
var (
500+
ioDeviceStats IODeviceStats
501+
err error
502+
)
503+
for file, p := range map[string]*uint64{
504+
"iodone_cnt": &ioDeviceStats.IODoneCount,
505+
"ioerr_cnt": &ioDeviceStats.IOErrCount,
506+
} {
507+
var val uint64
508+
val, err = util.ReadHexFromFile(fs.sys.Path(sysBlockPath, device, sysDevicePath, file))
509+
if err != nil {
510+
return IODeviceStats{}, err
511+
}
512+
*p = val
513+
}
514+
return ioDeviceStats, nil
515+
}

ext4/ext4.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2019 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// Package btrfs provides access to statistics exposed by ext4 filesystems.
15+
package ext4
16+
17+
import (
18+
"path/filepath"
19+
"strings"
20+
21+
"github.com/prometheus/procfs/internal/fs"
22+
"github.com/prometheus/procfs/internal/util"
23+
)
24+
25+
const (
26+
sysFSPath = "fs"
27+
sysFSExt4Path = "ext4"
28+
)
29+
30+
// Stats contains statistics for a single Btrfs filesystem.
31+
// See Linux fs/btrfs/sysfs.c for more information.
32+
type Stats struct {
33+
Name string
34+
35+
Errors uint64
36+
Warnings uint64
37+
Messages uint64
38+
}
39+
40+
// FS represents the pseudo-filesystems proc and sys, which provides an
41+
// interface to kernel data structures.
42+
type FS struct {
43+
proc *fs.FS
44+
sys *fs.FS
45+
}
46+
47+
// NewDefaultFS returns a new blockdevice fs using the default mountPoints for proc and sys.
48+
// It will error if either of these mount points can't be read.
49+
func NewDefaultFS() (FS, error) {
50+
return NewFS(fs.DefaultProcMountPoint, fs.DefaultSysMountPoint)
51+
}
52+
53+
// NewFS returns a new XFS handle using the given proc and sys mountPoints. It will error
54+
// if either of the mounts point can't be read.
55+
func NewFS(procMountPoint string, sysMountPoint string) (FS, error) {
56+
if strings.TrimSpace(procMountPoint) == "" {
57+
procMountPoint = fs.DefaultProcMountPoint
58+
}
59+
procfs, err := fs.NewFS(procMountPoint)
60+
if err != nil {
61+
return FS{}, err
62+
}
63+
if strings.TrimSpace(sysMountPoint) == "" {
64+
sysMountPoint = fs.DefaultSysMountPoint
65+
}
66+
sysfs, err := fs.NewFS(sysMountPoint)
67+
if err != nil {
68+
return FS{}, err
69+
}
70+
return FS{&procfs, &sysfs}, nil
71+
}
72+
73+
// ProcStat returns stats for the filesystem.
74+
func (fs FS) ProcStat() ([]*Stats, error) {
75+
matches, err := filepath.Glob(fs.sys.Path("fs/ext4/*"))
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
stats := make([]*Stats, 0, len(matches))
81+
for _, m := range matches {
82+
s := &Stats{}
83+
84+
// "*" used in glob above indicates the name of the filesystem.
85+
name := filepath.Base(m)
86+
s.Name = name
87+
for file, p := range map[string]*uint64{
88+
"errors_count": &s.Errors,
89+
"warning_count": &s.Warnings,
90+
"msg_count": &s.Messages,
91+
} {
92+
var val uint64
93+
val, err = util.ReadUintFromFile(fs.sys.Path(sysFSPath, sysFSExt4Path, name, file))
94+
if err == nil {
95+
*p = val
96+
}
97+
}
98+
99+
stats = append(stats, s)
100+
}
101+
102+
return stats, nil
103+
}

internal/util/parse.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package util
1515

1616
import (
17+
"errors"
1718
"os"
1819
"strconv"
1920
"strings"
@@ -110,3 +111,16 @@ func ParseBool(b string) *bool {
110111
}
111112
return &truth
112113
}
114+
115+
// ReadHexFromFile reads a file and attempts to parse a uint64 from a hexadecimal format 0xXX.
116+
func ReadHexFromFile(path string) (uint64, error) {
117+
data, err := os.ReadFile(path)
118+
if err != nil {
119+
return 0, err
120+
}
121+
hexString := strings.TrimSpace(string(data))
122+
if !strings.HasPrefix(hexString, "0x") {
123+
return 0, errors.New("invalid format: hex string does not start with '0x'")
124+
}
125+
return strconv.ParseUint(hexString[2:], 16, 64)
126+
}

0 commit comments

Comments
 (0)