forked from prometheus/node_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move stat_linux to cpu_linux and add cpufreq stats (prometheus#548)
- Loading branch information
1 parent
798950d
commit 2e9f191
Showing
15 changed files
with
210 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
// Copyright 2015 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// +build !nocpu | ||
|
||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/procfs" | ||
) | ||
|
||
const ( | ||
cpuCollectorNamespace = "cpu" | ||
) | ||
|
||
type cpuCollector struct { | ||
cpu *prometheus.Desc | ||
cpuFreq *prometheus.Desc | ||
cpuFreqMin *prometheus.Desc | ||
cpuFreqMax *prometheus.Desc | ||
cpuCoreThrottle *prometheus.Desc | ||
cpuPackageThrottle *prometheus.Desc | ||
} | ||
|
||
func init() { | ||
Factories["cpu"] = NewCPUCollector | ||
} | ||
|
||
// NewCPUCollector returns a new Collector exposing kernel/system statistics. | ||
func NewCPUCollector() (Collector, error) { | ||
return &cpuCollector{ | ||
cpu: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, "", cpuCollectorNamespace), | ||
"Seconds the cpus spent in each mode.", | ||
[]string{"cpu", "mode"}, nil, | ||
), | ||
cpuFreq: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, cpuCollectorNamespace, "frequency_hertz"), | ||
"Current cpu thread frequency in hertz.", | ||
[]string{"cpu"}, nil, | ||
), | ||
cpuFreqMin: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, cpuCollectorNamespace, "frequency_min_hertz"), | ||
"Minimum cpu thread frequency in hertz.", | ||
[]string{"cpu"}, nil, | ||
), | ||
cpuFreqMax: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, cpuCollectorNamespace, "frequency_max_hertz"), | ||
"Maximum cpu thread frequency in hertz.", | ||
[]string{"cpu"}, nil, | ||
), | ||
cpuCoreThrottle: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, cpuCollectorNamespace, "core_throttles_total"), | ||
"Number of times this cpu core has been throttled.", | ||
[]string{"cpu"}, nil, | ||
), | ||
cpuPackageThrottle: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, cpuCollectorNamespace, "package_throttles_total"), | ||
"Number of times this cpu package has been throttled.", | ||
[]string{"cpu"}, nil, | ||
), | ||
}, nil | ||
} | ||
|
||
// Update implements Collector and exposes cpu related metrics from /proc/stat and /sys/.../cpu/. | ||
func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error { | ||
if err := c.updateStat(ch); err != nil { | ||
return err | ||
} | ||
if err := c.updateCPUfreq(ch); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// updateCPUfreq reads /sys/bus/cpu/devices/cpu* and expose cpu frequency statistics. | ||
func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error { | ||
cpus, err := filepath.Glob(sysFilePath("bus/cpu/devices/cpu[0-9]*")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var value uint64 | ||
|
||
for _, cpu := range cpus { | ||
_, cpuname := filepath.Split(cpu) | ||
|
||
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq/scaling_cur_freq")); err != nil { | ||
return err | ||
} | ||
ch <- prometheus.MustNewConstMetric(c.cpuFreq, prometheus.GaugeValue, float64(value), cpuname) | ||
|
||
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq/scaling_min_freq")); err != nil { | ||
return err | ||
} | ||
ch <- prometheus.MustNewConstMetric(c.cpuFreqMin, prometheus.GaugeValue, float64(value), cpuname) | ||
|
||
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq/scaling_max_freq")); err != nil { | ||
return err | ||
} | ||
ch <- prometheus.MustNewConstMetric(c.cpuFreqMax, prometheus.GaugeValue, float64(value), cpuname) | ||
|
||
if value, err = readUintFromFile(filepath.Join(cpu, "thermal_throttle/core_throttle_count")); err != nil { | ||
return err | ||
} | ||
ch <- prometheus.MustNewConstMetric(c.cpuCoreThrottle, prometheus.CounterValue, float64(value), cpuname) | ||
|
||
if value, err = readUintFromFile(filepath.Join(cpu, "thermal_throttle/package_throttle_count")); err != nil { | ||
return err | ||
} | ||
ch <- prometheus.MustNewConstMetric(c.cpuPackageThrottle, prometheus.CounterValue, float64(value), cpuname) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// updateStat reads /proc/stat through procfs and exports cpu related metrics. | ||
func (c *cpuCollector) updateStat(ch chan<- prometheus.Metric) error { | ||
fs, err := procfs.NewFS(*procPath) | ||
if err != nil { | ||
return fmt.Errorf("failed to open procfs: %v", err) | ||
} | ||
stats, err := fs.NewStat() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for cpuID, cpuStat := range stats.CPU { | ||
cpuName := fmt.Sprintf("cpu%d", cpuID) | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.User, cpuName, "user") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Nice, cpuName, "nice") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.System, cpuName, "system") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Idle, cpuName, "idle") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Iowait, cpuName, "iowait") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.IRQ, cpuName, "irq") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.SoftIRQ, cpuName, "softirq") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Steal, cpuName, "steal") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Guest, cpuName, "guest") | ||
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.GuestNice, cpuName, "guest_nice") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu0/cpufreq/scaling_cur_freq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1699981 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu0/cpufreq/scaling_max_freq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3700000 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu0/cpufreq/scaling_min_freq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
800000 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu0/thermal_throttle/core_throttle_count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu0/thermal_throttle/package_throttle_count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
30 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu1/cpufreq/scaling_cur_freq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1699981 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu1/cpufreq/scaling_max_freq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3700000 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu1/cpufreq/scaling_min_freq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
800000 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu1/thermal_throttle/core_throttle_count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
1 change: 1 addition & 0 deletions
1
collector/fixtures/sys/bus/cpu/devices/cpu1/thermal_throttle/package_throttle_count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ collectors=$(cat << COLLECTORS | |
arp | ||
buddyinfo | ||
conntrack | ||
cpu | ||
diskstats | ||
drbd | ||
edac | ||
|