Skip to content

Commit 4d322a4

Browse files
committed
Add Disk IO stats and ext4 FS stats
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 <mhmdshahzeb1993@gmail.com>
1 parent 0e0b4b1 commit 4d322a4

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-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
@@ -209,6 +214,7 @@ const (
209214
sysBlockQueue = "queue"
210215
sysBlockDM = "dm"
211216
sysUnderlyingDev = "slaves"
217+
sysDevicePath = "device"
212218
)
213219

214220
// FS represents the pseudo-filesystems proc and sys, which provides an
@@ -474,3 +480,25 @@ func (fs FS) SysBlockDeviceUnderlyingDevices(device string) (UnderlyingDeviceInf
474480
return UnderlyingDeviceInfo{DeviceNames: underlying}, nil
475481

476482
}
483+
484+
// SysBlockDeviceIO returns stats for the block device io counters
485+
// IO done count: /sys/block/<disk>/device/iodone_cnt
486+
// IO error count: /sys/block/<disk>/device/ioerr_cnt.
487+
func (fs FS) SysBlockDeviceIOStat(device string) (IODeviceStats, error) {
488+
var (
489+
ioDeviceStats IODeviceStats
490+
err error
491+
)
492+
for file, p := range map[string]*uint64{
493+
"iodone_cnt": &ioDeviceStats.IODoneCount,
494+
"ioerr_cnt": &ioDeviceStats.IOErrCount,
495+
} {
496+
var val uint64
497+
val, err = util.ReadHexFromFile(fs.sys.Path(sysBlockPath, device, sysDevicePath, file))
498+
if err != nil {
499+
return IODeviceStats{}, err
500+
}
501+
*p = val
502+
}
503+
return ioDeviceStats, nil
504+
}

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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,16 @@ func ParseBool(b string) *bool {
110110
}
111111
return &truth
112112
}
113+
114+
// ReadHexFromFile reads a file and attempts to parse a uint64 from a hexadecimal format 0xXX.
115+
func ReadHexFromFile(path string) (uint64, error) {
116+
data, err := os.ReadFile(path)
117+
if err != nil {
118+
return 0, err
119+
}
120+
hexString := strings.TrimSpace(string(data))
121+
if !strings.HasPrefix(hexString, "0x") {
122+
return 0, fmt.Errorf("invalid format: hex string does not start with '0x'")
123+
}
124+
return strconv.ParseUint(hexString[2:], 16, 64)
125+
}

0 commit comments

Comments
 (0)