Skip to content

Commit

Permalink
Merge pull request #825 from kasbert/master
Browse files Browse the repository at this point in the history
Add disk and memory percent_used
  • Loading branch information
k8s-ci-robot authored Dec 15, 2023
2 parents 9f639dd + 0790063 commit bdaa44e
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions config/system-stats-monitor.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"disk/bytes_used": {
"displayName": "disk/bytes_used"
},
"disk/percent_used": {
"displayName": "disk/percent_used"
},
"disk/io_time": {
"displayName": "disk/io_time"
},
Expand Down Expand Up @@ -88,6 +91,9 @@
},
"memory/unevictable_used": {
"displayName": "memory/unevictable_used"
},
"memory/percent_used": {
"displayName": "memory/percent_used"
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions config/windows-system-stats-monitor.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"disk/bytes_used": {
"displayName": "disk/bytes_used"
},
"disk/percent_used": {
"displayName": "disk/percent_used"
},
"disk/io_time": {
"displayName": "disk/io_time"
},
Expand Down Expand Up @@ -88,6 +91,9 @@
},
"memory/unevictable_used": {
"displayName": "memory/unevictable_used"
},
"memory/percent_used": {
"displayName": "memory/percent_used"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/exporters/stackdriver/stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var NPDMetricToSDMetric = map[metrics.MetricID]string{
metrics.CPULoad15m: "compute.googleapis.com/guest/cpu/load_15m",
metrics.DiskAvgQueueLenID: "compute.googleapis.com/guest/disk/queue_length",
metrics.DiskBytesUsedID: "compute.googleapis.com/guest/disk/bytes_used",
metrics.DiskPercentUsedID: "custom.googleapis.com/guest/disk/percent_used",
metrics.DiskIOTimeID: "compute.googleapis.com/guest/disk/io_time",
metrics.DiskMergedOpsCountID: "compute.googleapis.com/guest/disk/merged_operation_count",
metrics.DiskOpsBytesID: "compute.googleapis.com/guest/disk/operation_bytes_count",
Expand All @@ -66,6 +67,7 @@ var NPDMetricToSDMetric = map[metrics.MetricID]string{
metrics.MemoryDirtyUsedID: "compute.googleapis.com/guest/memory/dirty_used",
metrics.MemoryPageCacheUsedID: "compute.googleapis.com/guest/memory/page_cache_used",
metrics.MemoryUnevictableUsedID: "compute.googleapis.com/guest/memory/unevictable_used",
metrics.MemoryPercentUsedID: "custom.googleapis.com/guest/memory/percent_used",
metrics.ProblemCounterID: "compute.googleapis.com/guest/system/problem_count",
metrics.ProblemGaugeID: "compute.googleapis.com/guest/system/problem_state",
metrics.OSFeatureID: "compute.googleapis.com/guest/system/os_feature_enabled",
Expand Down
15 changes: 15 additions & 0 deletions pkg/systemstatsmonitor/disk_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type diskCollector struct {
mOpsBytes *metrics.Int64Metric
mOpsTime *metrics.Int64Metric
mBytesUsed *metrics.Int64Metric
mPercentUsed *metrics.Float64Metric

config *ssmtypes.DiskStatsConfig

Expand Down Expand Up @@ -150,6 +151,17 @@ func NewDiskCollectorOrDie(diskConfig *ssmtypes.DiskStatsConfig) *diskCollector
klog.Fatalf("Error initializing metric for %q: %v", metrics.DiskBytesUsedID, err)
}

dc.mPercentUsed, err = metrics.NewFloat64Metric(
metrics.DiskPercentUsedID,
diskConfig.MetricsConfigs[string(metrics.DiskPercentUsedID)].DisplayName,
"Disk usage in percentage of total space",
"%",
metrics.LastValue,
[]string{deviceNameLabel})
if err != nil {
klog.Fatalf("Error initializing metric for %q: %v", metrics.DiskPercentUsedID, err)
}

dc.lastIOTime = make(map[string]uint64)
dc.lastWeightedIO = make(map[string]uint64)
dc.lastReadCount = make(map[string]uint64)
Expand Down Expand Up @@ -291,6 +303,9 @@ func (dc *diskCollector) collect() {
opttypes := strings.Join(partition.Opts, ",")
dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "free"}, int64(usageStat.Free))
dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, int64(usageStat.Used))
if dc.mPercentUsed != nil {
dc.mPercentUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, float64(usageStat.UsedPercent))
}
}

}
Expand Down
12 changes: 12 additions & 0 deletions pkg/systemstatsmonitor/memory_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

type memoryCollector struct {
mBytesUsed *metrics.Int64Metric
mPercentUsed *metrics.Float64Metric
mAnonymousUsed *metrics.Int64Metric
mPageCacheUsed *metrics.Int64Metric
mUnevictableUsed *metrics.Int64Metric
Expand All @@ -49,6 +50,17 @@ func NewMemoryCollectorOrDie(memoryConfig *ssmtypes.MemoryStatsConfig) *memoryCo
klog.Fatalf("Error initializing metric for %q: %v", metrics.MemoryBytesUsedID, err)
}

mc.mPercentUsed, err = metrics.NewFloat64Metric(
metrics.MemoryPercentUsedID,
memoryConfig.MetricsConfigs[string(metrics.MemoryPercentUsedID)].DisplayName,
"Memory usage in percentage of total memory.",
"%",
metrics.LastValue,
[]string{stateLabel})
if err != nil {
klog.Fatalf("Error initializing metric for %q: %v", metrics.MemoryPercentUsedID, err)
}

mc.mAnonymousUsed, err = metrics.NewInt64Metric(
metrics.MemoryAnonymousUsedID,
memoryConfig.MetricsConfigs[string(metrics.MemoryAnonymousUsedID)].DisplayName,
Expand Down
6 changes: 6 additions & 0 deletions pkg/systemstatsmonitor/memory_collector_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func (mc *memoryCollector) collect() {
}
}

if mc.mPercentUsed != nil && meminfo.MemTotal != nil && *meminfo.MemTotal > 0 &&
meminfo.MemFree != nil && meminfo.Buffers != nil && meminfo.Cached != nil && meminfo.Slab != nil {
ratio := float64(*meminfo.MemTotal - *meminfo.MemFree - *meminfo.Buffers - *meminfo.Cached - *meminfo.Slab) / float64(*meminfo.MemTotal)
mc.mPercentUsed.Record(map[string]string{stateLabel: "used"}, float64(ratio*100.0))
}

if mc.mDirtyUsed != nil {
if meminfo.Dirty != nil {
mc.mDirtyUsed.Record(map[string]string{stateLabel: "dirty"}, int64(*meminfo.Dirty)*1024)
Expand Down
4 changes: 4 additions & 0 deletions pkg/systemstatsmonitor/memory_collector_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ func (mc *memoryCollector) collect() {
mc.mBytesUsed.Record(map[string]string{stateLabel: "free"}, int64(meminfo.Available)*1024)
mc.mBytesUsed.Record(map[string]string{stateLabel: "used"}, int64(meminfo.Used)*1024)
}

if mc.mPercentUsed != nil {
mc.mPercentUsed.Record(map[string]string{stateLabel: "used"}, float64(meminfo.UsedPercent))
}
}
2 changes: 2 additions & 0 deletions pkg/util/metrics/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const (
DiskOpsBytesID MetricID = "disk/operation_bytes_count"
DiskOpsTimeID MetricID = "disk/operation_time"
DiskBytesUsedID MetricID = "disk/bytes_used"
DiskPercentUsedID MetricID = "disk/percent_used"
HostUptimeID MetricID = "host/uptime"
MemoryBytesUsedID MetricID = "memory/bytes_used"
MemoryAnonymousUsedID MetricID = "memory/anonymous_used"
MemoryPageCacheUsedID MetricID = "memory/page_cache_used"
MemoryUnevictableUsedID MetricID = "memory/unevictable_used"
MemoryDirtyUsedID MetricID = "memory/dirty_used"
MemoryPercentUsedID MetricID = "memory/percent_used"
OSFeatureID MetricID = "system/os_feature"
SystemProcessesTotal MetricID = "system/processes_total"
SystemProcsRunning MetricID = "system/procs_running"
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/metriconly/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ var _ = ginkgo.Describe("NPD should export Prometheus metrics.", func() {
assertMetricExist(gotMetrics, "disk_operation_bytes_count", map[string]string{}, false)
assertMetricExist(gotMetrics, "disk_operation_time", map[string]string{}, false)
assertMetricExist(gotMetrics, "disk_bytes_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "disk_percent_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "disk_io_time", map[string]string{}, false)
assertMetricExist(gotMetrics, "disk_weighted_io", map[string]string{}, false)
assertMetricExist(gotMetrics, "memory_bytes_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "memory_anonymous_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "memory_page_cache_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "memory_unevictable_used", map[string]string{}, true)
assertMetricExist(gotMetrics, "memory_dirty_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "memory_percent_used", map[string]string{}, false)
assertMetricExist(gotMetrics, "host_uptime", map[string]string{}, false)
assertMetricExist(gotMetrics, "system_os_feature", map[string]string{}, false)
})
Expand Down

0 comments on commit bdaa44e

Please sign in to comment.