/
nodeCapacity.go
48 lines (42 loc) · 1.24 KB
/
nodeCapacity.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package containerinsightscommon
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
"log"
"os"
)
type NodeCapacity struct {
MemCapacity int64
CPUCapacity int64
}
func NewNodeCapacity() *NodeCapacity {
if _, err := os.Lstat("/rootfs/proc"); os.IsNotExist(err) {
panic("/rootfs/proc doesn't exists")
}
if err := os.Setenv(GoPSUtilProcDirEnv, "/rootfs/proc"); err != nil {
log.Printf("E! NodeCapacity cannot set goPSUtilProcDirEnv to /rootfs/proc %v", err)
}
nc := &NodeCapacity{}
nc.parseCpu()
nc.parseMemory()
return nc
}
func (n *NodeCapacity) parseMemory() {
if memStats, err := mem.VirtualMemory(); err == nil {
n.MemCapacity = int64(memStats.Total)
} else {
// If any error happen, then there will be no mem utilization metrics
log.Printf("E! NodeCapacity cannot get memStats from psUtil %v", err)
}
}
func (n *NodeCapacity) parseCpu() {
if cpuInfos, err := cpu.Info(); err == nil {
numCores := len(cpuInfos)
n.CPUCapacity = int64(numCores)
} else {
// If any error happen, then there will be no cpu utilization metrics
log.Printf("E! NodeCapacity cannot get cpuInfo from psUtil %v", err)
}
}