【linux】查看系统cpu信息及使用telegraf监控系统负载状况

一、cpu负载和利用率的区别

查看系统cpu状况常用的两个指标时负载和利用率,这两个指标是不一样的,负载高不一定利用率高,利用率高负载也不一定高,两者无必然关系,下面是一个通俗易懂的例子:
拿打电话来说明两者的区别,我按自己的理解阐述一下。 某公用电话亭,有一个人在打电话,四个人在等待,每人限定使用电话一分钟,若有人一分钟之内没有打完电话,只能挂掉电话去排队,等待下一轮。电话在这里就相当于CPU,而正在或等待打电话的人就相当于任务数。 在电话亭使用过程中,肯定会有人打完电话走掉,有人没有打完电话而选择重新排队,更会有新增的人在这儿排队,这个人数的变化就相当于任务数的增减。为了统计平均负载情况,我们5秒钟统计一次人数,并在第1、5、15分钟的时候对统计情况取平均值,从而形成第1、5、15分钟的平均负载。 有的人拿起电话就打,一直打完1分钟,而有的人可能前三十秒在找电话号码,或者在犹豫要不要打,后三十秒才真正在打电话。如果把电话看作CPU,人数看作任务,我们就说前一个人(任务)的CPU利用率高,后一个人(任务)的CPU利用率低。 当然, CPU并不会在前三十秒工作,后三十秒歇着,只是说,有的程序涉及到大量的计算,所以CPU利用率就高,而有的程序牵涉到计算的部分很少,CPU利用率自然就低。但无论CPU的利用率是高是低,跟后面有多少任务在排队没有必然关系。

二、查看系统cpu信息的方式

1、知识点
  1. cpu信息记录在/proc/cpuinfo中
  2. linux中的top命令可以查询cpu信息,相当于windows的任务管理器
  3. cpu总核数 = 物理cpu个数 * 每颗物理cpu的核数
  4. 总逻辑cpu数 = 物理cpu个数 * 每颗物理cpu的核数 * 超线程数
2、查询方式

(1)通过/proc/cpuinfo 中记录的内容查询

cat /proc/cpuinfo | grep name | sort | uniq //查看cpu型号
cat /proc/cpuinfo | grep "physical id"  //查看物理cpu数目,有多少各不同的physical id就代表有几个物理cpu
cat /proc/cpuinfo| grep "cpu cores"| uniq //查看每个物理cpu的核数
cat /proc/cpuinfo| grep "processor"| wc -l // 查看逻辑cpu数目

(2)通过lscpu直接查看,cpus表示逻辑cpu数量,threads代表超线程数,core代表核数,Socket代表物理cpu数。
在这里插入图片描述
(3)top查看cpu负载和利用率情况
cpu负载要结合逻辑cpu数量来分析
待完善

三、监控系统cpu负载状况

监控系统cpu负载情况可以使用telegraf 的cpu插件.
The CPU plugin collects standard CPU metrics as defined in man proc. All architectures do not support all of these metrics.
CPU插件收集man proc中定义的标准CPU指标。并非所有的体系结构都支持所有这些度量。

cpu  3357 0 4313 1362393
    The amount of time, measured in units of USER_HZ (1/100ths of a second on
    most architectures, use sysconf(_SC_CLK_TCK) to obtain the right value),
    that the system spent in various states:

    user   (1) Time spent in user mode.

    nice   (2) Time spent in user mode with low priority (nice).

    system (3) Time spent in system mode.

    idle   (4) Time spent in the idle task.  This value should be USER_HZ times
    the second entry in the /proc/uptime pseudo-file.

    iowait (since Linux 2.5.41)
           (5) Time waiting for I/O to complete.

    irq (since Linux 2.6.0-test4)
           (6) Time servicing interrupts.

    softirq (since Linux 2.6.0-test4)
           (7) Time servicing softirqs.

    steal (since Linux 2.6.11)
           (8) Stolen time, which is the time spent in other operating systems
           when running in a virtualized environment

    guest (since Linux 2.6.24)
           (9) Time spent running a virtual CPU for guest operating systems
           under the control of the Linux kernel.

    guest_nice (since Linux 2.6.33)
           (10) Time spent running a niced guest (virtual CPU for guest operating systems under the control of the Linux kernel).

配置文件

[[inputs.cpu]]
  ## Whether to report per-cpu stats or not
  percpu = true
  ## Whether to report total system cpu stats or not
  totalcpu = true
  ## If true, collect raw CPU time metrics.
  collect_cpu_time = false
  ## If true, compute and report the sum of all non-idle CPU states.
  report_active = false
测量指标如下:
CPU Time measurements:

Meta:

  • units: CPU Time
  • tags: cpu=<cpuN> or <cpu-total>

Measurement names:

  • cpu_time_user
  • cpu_time_system
  • cpu_time_idle
  • cpu_time_active (must be explicitly enabled by setting report_active = true)
  • cpu_time_nice
  • cpu_time_iowait
  • cpu_time_irq
  • cpu_time_softirq
  • cpu_time_steal
  • cpu_time_guest
  • cpu_time_guest_nice
CPU Usage Percent Measurements:

Meta:

  • units: percent (out of 100)
  • tags: cpu=<cpuN> or <cpu-total>

Measurement names:

  • cpu_usage_user
  • cpu_usage_system
  • cpu_usage_idle
  • cpu_usage_active (must be explicitly enabled by setting report_active = true)
  • cpu_usage_nice
  • cpu_usage_iowait
  • cpu_usage_irq
  • cpu_usage_softirq
  • cpu_usage_steal
  • cpu_usage_guest
  • cpu_usage_guest_nice
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值