首页 > 系统相关 >Go语言学习日记【二十三】监控linux系统cpu使用率与空置率

Go语言学习日记【二十三】监控linux系统cpu使用率与空置率

时间:2022-10-13 17:08:06浏览次数:43  
标签:return calculatePct float64 Percentages s1 s0 linux Go cpu


package main


import (
"fmt"
"math"
"time"
sigar "github.com/elastic/gosigar"
)



type Monitor struct {
lastSample *sigar.Cpu
}


type Percentages struct {
User float64
System float64
Idle float64
IOWait float64
IRQ float64
Nice float64
SoftIRQ float64
Steal float64
Total float64
}


type Metrics struct {
previousSample *sigar.Cpu
currentSample *sigar.Cpu
}


func main() {
fmt.Println("Start test")
cpu := new(Monitor)
for {
sample ,_:= cpu.Sample()
pct := sample.Percentages()
fmt.Println("----",pct.User,pct.System,pct.Idle,pct.Total)
time.Sleep(1 * time.Second )
}

}


func (m *Monitor) Sample() (*Metrics, error) {
cpuSample := &sigar.Cpu{}
if err := cpuSample.Get(); err != nil {
return nil, err
}

oldLastSample := m.lastSample
m.lastSample = cpuSample
return &Metrics{oldLastSample, cpuSample}, nil
}


func (m *Metrics) Percentages() Percentages {
return cpuPercentages(m.previousSample, m.currentSample, 1)
}


func cpuPercentages(s0, s1 *sigar.Cpu, numCPU int) Percentages {
if s0 == nil || s1 == nil {
return Percentages{}
}

// timeDelta is the total amount of CPU time available across all CPU cores.
timeDelta := s1.Total() - s0.Total()
if timeDelta <= 0 {
return Percentages{}
}

calculatePct := func(v0, v1 uint64) float64 {
cpuDelta := int64(v1 - v0)
pct := float64(cpuDelta) / float64(timeDelta)
return Round(pct*float64(numCPU), 4)
}

calculateTotalPct := func() float64 {
// IOWait time is excluded from the total as per #7627.
idle := calculatePct(s0.Idle, s1.Idle) + calculatePct(s0.Wait, s1.Wait)
return Round(float64(numCPU)-idle, 4)
}

return Percentages{
User: calculatePct(s0.User, s1.User),
System: calculatePct(s0.Sys, s1.Sys),
Idle: calculatePct(s0.Idle, s1.Idle),
IOWait: calculatePct(s0.Wait, s1.Wait),
IRQ: calculatePct(s0.Irq, s1.Irq),
Nice: calculatePct(s0.Nice, s1.Nice),
SoftIRQ: calculatePct(s0.SoftIrq, s1.SoftIrq),
Steal: calculatePct(s0.Stolen, s1.Stolen),
Total: calculateTotalPct(),
}
}


func Round(val float64, precision int64) (newVal float64) {
var round float64
pow := math.Pow(10, float64(precision))
digit := pow * val
_, div := math.Modf(digit)
if div >= 0.5 {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}

标签:return,calculatePct,float64,Percentages,s1,s0,linux,Go,cpu
From: https://blog.51cto.com/u_15828115/5754002

相关文章

  • 【监控】 linux系统监控的概述
    入门1.识别监控对象2.理解监控对象3.细分监控对象的指标4.确定报警的基准线。 预中级  1工具化和监控分离  2监控对象的分类    2.1硬件监控 (IPMI,SNMP)......
  • Go语言学习日记【二十五】golang格式化输出字符串左右对齐
    packagemainimport"fmt"funcmain(){fmt.Printf("%-40s\t%s\n","aaaaaaaaaaaaaaaaaa","a")fmt.Printf("%-40s\t%s\n","bb","b")} ......
  • Golang Gorm time 时间字段格式化模型类 重写
    问题:在使用GORM中如果我们使用到了CreateAt和UpdateAt就会发现这个时间的类型是time.Time而其数据是"2022-10-13T10:14:02.973528+08:00"这样的,然而这样的数据你......
  • Linux搭建NFS服务
    欢迎来到千汐 博客名称:千汐染琼博客网址:https://www.cnblogs.com/zhangxiaomin/寄语:无数次从睡梦中醒来,忘记了我是谁worldcookieNFS网络文件系统:(1)基于C/S架构......
  • Linux内核漏洞精准检测
    Linux内核结构:Linux内核由七个部分构成,每个不同的部分又有多个内核模块组成,结构框图如下:​Linux裁剪场景分析:通过分析Linux内核源代码可以看到不同目录中存放着不同模块的实......
  • Golang复杂json结构体解析
    1、示例一{"id":"8667597b-bcd9-51de31b655cd","name":"ali-redis-analyse","category":"db","category_display":"数据库","type":"redis","type_display":......
  • golang usps api对接
    golanguspsapi对接简介实现对接uspsapi接口使用gogetgithub.com/wms3001/USPS实例获取跟踪信息usps:=&USPS{}usps.UserId="680CHUAN3141"resp:=usps.Track("930......
  • linux进程调度算法
    一、进程的状态:1、操作系统中:进程是一个动态的实体,所以他是有生命的。从创建到消亡,是一个进程的整个生命周期。在这个周期中,进程可能会经历各种不同的状态。一般来说,所有进......
  • Linux系统NTP配置同步修改硬件时钟
    前言:  硬件时钟:即BIOS时间,就是CMOS设置时看到的时间,存储在主板BIOS里,关机及断电后由主板电池供电维持时间的守时。  系统时钟:linux系统Kernel时间,由CPU守时,关机及......
  • 【Python】关于Django如何处理前后端分离时的POST请求
    在目前的大环境趋势下,前后端分离已经是项目开发的主流,而Django在后端领域也受到众多程序员的青睐,那么在前后端分离开发时,经常会遇到django本身内置的csrf拦截保护机制本文......