ssh_crawler/stat/disk_io.go
insanity 806f406ee0 ing
2017-10-24 16:09:17 +09:00

63 lines
1.2 KiB
Go

package stat
import (
"bufio"
"fmt"
"io"
"strings"
)
type DiskIOStat struct {
Device,
ReadCompleted,
ReadMerged,
SectorRead,
TimeSpentReading,
WritesCompleted,
WritesMerged,
SectorsWritten,
TimeSpentWriting,
IoInProg,
TimeSpentIO,
WeightedTimeSpentIO string
}
func (diskio DiskIOStat) Command() string {
return "cat /proc/diskstats"
}
func (diskio DiskIOStat) Parse(r io.Reader) (*[]DiskIOStat, error) {
var (
stats = []DiskIOStat{}
scanner = bufio.NewScanner(r)
)
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
if len(parts) < 4 {
return nil, fmt.Errorf("invalid line in %s: %s", "/proc/diskstats", scanner.Text())
}
deviceName := parts[2]
if !strings.HasPrefix(deviceName, "sd") {
continue
}
stats = append(stats, DiskIOStat{
Device: deviceName,
ReadCompleted: parts[3],
ReadMerged: parts[4],
SectorRead: parts[5],
TimeSpentReading: parts[6],
WritesCompleted: parts[7],
WritesMerged: parts[8],
SectorsWritten: parts[9],
TimeSpentWriting: parts[10],
IoInProg: parts[11],
TimeSpentIO: parts[12],
WeightedTimeSpentIO: parts[13],
})
}
return &stats, scanner.Err()
}