|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package systemstatsmonitor |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/golang/glog" |
| 23 | + "github.com/prometheus/procfs" |
| 24 | + "github.com/shirou/gopsutil/load" |
| 25 | +) |
| 26 | + |
| 27 | +func (cc *cpuCollector) recordLoad() { |
| 28 | + if cc.mRunnableTaskCount == nil { |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + loadAvg, err := load.Avg() |
| 33 | + if err != nil { |
| 34 | + glog.Errorf("Failed to retrieve average CPU load: %v", err) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1) |
| 39 | + |
| 40 | + cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1) |
| 41 | + cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5) |
| 42 | + cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15) |
| 43 | +} |
| 44 | + |
| 45 | +func (cc *cpuCollector) recordSystemStats() { |
| 46 | + fs, err := procfs.NewFS("/proc") |
| 47 | + stats, err := fs.Stat() |
| 48 | + if err != nil { |
| 49 | + glog.Errorf("Failed to retrieve cpu/process stats: %v", err) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated)) |
| 54 | + cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning)) |
| 55 | + cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked)) |
| 56 | + cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal)) |
| 57 | + |
| 58 | + for i, c := range stats.CPU { |
| 59 | + tags := map[string]string{} |
| 60 | + tags[cpuLabel] = fmt.Sprintf("cpu%d", i) |
| 61 | + |
| 62 | + tags[stageLabel] = "user" |
| 63 | + cc.mSystemCPUStat.Record(tags, c.User) |
| 64 | + tags[stageLabel] = "nice" |
| 65 | + cc.mSystemCPUStat.Record(tags, c.Nice) |
| 66 | + tags[stageLabel] = "system" |
| 67 | + cc.mSystemCPUStat.Record(tags, c.System) |
| 68 | + tags[stageLabel] = "idle" |
| 69 | + cc.mSystemCPUStat.Record(tags, c.Idle) |
| 70 | + tags[stageLabel] = "iowait" |
| 71 | + cc.mSystemCPUStat.Record(tags, c.Iowait) |
| 72 | + tags[stageLabel] = "iRQ" |
| 73 | + cc.mSystemCPUStat.Record(tags, c.IRQ) |
| 74 | + tags[stageLabel] = "softIRQ" |
| 75 | + cc.mSystemCPUStat.Record(tags, c.SoftIRQ) |
| 76 | + tags[stageLabel] = "steal" |
| 77 | + cc.mSystemCPUStat.Record(tags, c.Steal) |
| 78 | + tags[stageLabel] = "guest" |
| 79 | + cc.mSystemCPUStat.Record(tags, c.Guest) |
| 80 | + tags[stageLabel] = "guestNice" |
| 81 | + cc.mSystemCPUStat.Record(tags, c.GuestNice) |
| 82 | + } |
| 83 | +} |
0 commit comments