forked from DataDog/datadog-agent
/
disk_nix.go
168 lines (137 loc) · 4.98 KB
/
disk_nix.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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build !windows
// +build !windows
package disk
import (
"fmt"
"path/filepath"
"github.com/shirou/gopsutil/disk"
"github.com/StackVista/stackstate-agent/pkg/aggregator"
"github.com/StackVista/stackstate-agent/pkg/autodiscovery/integration"
core "github.com/StackVista/stackstate-agent/pkg/collector/corechecks"
"github.com/StackVista/stackstate-agent/pkg/util/log"
)
// for testing
var (
diskPartitions = disk.Partitions
diskUsage = disk.Usage
)
// Check stores disk-specific additional fields
type Check struct {
core.CheckBase
cfg *diskConfig
// sts
// topologyCollector collects all disk topology and produces it using the Batcher
topologyCollector *TopologyCollector
}
// Run executes the check
func (c *Check) Run() error {
sender, err := aggregator.GetSender(c.ID())
if err != nil {
return err
}
partitions, err := c.collectPartitionMetrics(sender)
if err != nil {
return err
}
err = c.collectDiskMetrics(sender)
if err != nil {
return err
}
sender.Commit()
//sts
// produce disk topology
err = c.topologyCollector.BuildTopology(partitions)
if err != nil {
return err
}
//sts
return nil
}
func (c *Check) collectPartitionMetrics(sender aggregator.Sender) ([]disk.PartitionStat, error) {
partitions, err := diskPartitions(true)
if err != nil {
return nil, err
}
// sts - collect disk partitions to create host topology
parts := make([]disk.PartitionStat, 0)
for _, partition := range partitions {
if c.excludeDisk(partition.Mountpoint, partition.Device, partition.Fstype) {
continue
}
// Get disk metrics here to be able to exclude on total usage
usage, err := diskUsage(partition.Mountpoint)
if err != nil {
log.Debugf("Unable to get disk metrics of %s mount point: %s", partition.Mountpoint, err)
continue
}
// Exclude disks with total disk size 0
if usage.Total == 0 {
continue
}
tags := make([]string, 0, 2)
if c.cfg.tagByFilesystem {
tags = append(tags, partition.Fstype, fmt.Sprintf("filesystem:%s", partition.Fstype))
}
var deviceName string
if c.cfg.useMount {
deviceName = partition.Mountpoint
} else {
deviceName = partition.Device
}
tags = append(tags, fmt.Sprintf("device:%s", deviceName))
tags = append(tags, fmt.Sprintf("device_name:%s", filepath.Base(partition.Device)))
tags = c.applyDeviceTags(partition.Device, partition.Mountpoint, tags)
// sts - keep the partitions
parts = append(parts, partition)
c.sendPartitionMetrics(sender, usage, tags)
}
return parts, nil
}
func (c *Check) collectDiskMetrics(sender aggregator.Sender) error {
iomap, err := ioCounters()
if err != nil {
return err
}
for deviceName, ioCounter := range iomap {
tags := []string{}
tags = append(tags, fmt.Sprintf("device:%s", deviceName))
tags = append(tags, fmt.Sprintf("device_name:%s", deviceName))
tags = c.applyDeviceTags(deviceName, "", tags)
c.sendDiskMetrics(sender, ioCounter, tags)
}
return nil
}
func (c *Check) sendPartitionMetrics(sender aggregator.Sender, usage *disk.UsageStat, tags []string) {
// Disk metrics
// For legacy reasons, the standard unit it kB
sender.Gauge(fmt.Sprintf(diskMetric, "total"), float64(usage.Total)/1024, "", tags)
sender.Gauge(fmt.Sprintf(diskMetric, "used"), float64(usage.Used)/1024, "", tags)
sender.Gauge(fmt.Sprintf(diskMetric, "free"), float64(usage.Free)/1024, "", tags)
// FIXME(8.x): use percent, a lot more logical than in_use
sender.Gauge(fmt.Sprintf(diskMetric, "in_use"), usage.UsedPercent/100, "", tags)
// Inodes metrics
sender.Gauge(fmt.Sprintf(inodeMetric, "total"), float64(usage.InodesTotal), "", tags)
sender.Gauge(fmt.Sprintf(inodeMetric, "used"), float64(usage.InodesUsed), "", tags)
sender.Gauge(fmt.Sprintf(inodeMetric, "free"), float64(usage.InodesFree), "", tags)
// FIXME(8.x): use percent, a lot more logical than in_use
sender.Gauge(fmt.Sprintf(inodeMetric, "in_use"), usage.InodesUsedPercent/100, "", tags)
}
func (c *Check) sendDiskMetrics(sender aggregator.Sender, ioCounter disk.IOCountersStat, tags []string) {
sender.MonotonicCount(fmt.Sprintf(diskMetric, "read_time"), float64(ioCounter.ReadTime), "", tags)
sender.MonotonicCount(fmt.Sprintf(diskMetric, "write_time"), float64(ioCounter.WriteTime), "", tags)
// FIXME(8.x): These older metrics are kept here for backwards compatibility, but they are wrong: the value is not a percentage
sender.Rate(fmt.Sprintf(diskMetric, "read_time_pct"), float64(ioCounter.ReadTime)*100/1000, "", tags)
sender.Rate(fmt.Sprintf(diskMetric, "write_time_pct"), float64(ioCounter.WriteTime)*100/1000, "", tags)
}
// Configure the disk check
func (c *Check) Configure(data integration.Data, initConfig integration.Data, source string) error {
err := c.CommonConfigure(data, source)
if err != nil {
return err
}
return c.instanceConfigure(data)
}