forked from cloudradar-monitoring/cagent
/
fs.go
49 lines (41 loc) · 1.48 KB
/
fs.go
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
package fs
import (
"context"
"time"
"github.com/shirou/gopsutil/disk"
)
const fsInfoRequestTimeout = time.Second * 10
type ioCountersMeasurement struct {
timestamp time.Time
counters *disk.IOCountersStat
}
type ioUsageInfo struct {
readBytesPerSecond float64
writeBytesPerSecond float64
readOperationsPerSecond float64
writeOperationsPerSecond float64
}
func getFsPartitionUsageInfo(mountPoint string) (*disk.UsageStat, error) {
ctx, cancel := context.WithTimeout(context.Background(), fsInfoRequestTimeout)
defer cancel()
return disk.UsageWithContext(ctx, mountPoint)
}
func calcIOCountersUsage(prev, curr *disk.IOCountersStat, timeDelta time.Duration) *ioUsageInfo {
deltaSeconds := timeDelta.Seconds()
return &ioUsageInfo{
readBytesPerSecond: float64(curr.ReadBytes-prev.ReadBytes) / deltaSeconds,
writeBytesPerSecond: float64(curr.WriteBytes-prev.WriteBytes) / deltaSeconds,
readOperationsPerSecond: float64(curr.ReadCount-prev.ReadCount) / deltaSeconds,
writeOperationsPerSecond: float64(curr.WriteCount-prev.WriteCount) / deltaSeconds,
}
}
func calcTotalIOUsage(partitionsUsageInfo map[string]*ioUsageInfo) *ioUsageInfo {
result := &ioUsageInfo{}
for _, info := range partitionsUsageInfo {
result.readBytesPerSecond += info.readBytesPerSecond
result.writeBytesPerSecond += info.writeBytesPerSecond
result.readOperationsPerSecond += info.readOperationsPerSecond
result.writeOperationsPerSecond += info.writeOperationsPerSecond
}
return result
}