系统指标监控 collectd + influxDB + grafana

Collcted

collectd 是一个守护程序,它定期收集系统和应用程序性能指标,并提供以各种方式存储值的机制。

collectd 从各种来源收集指标,例如 操作系统,应用程序,日志文件和外部设备,并存储这些信息或通过网络传输。 这些统计数据可用于监视系统,发现性能瓶颈(即性能分析)和预测未来的系统负载(即容量规划)

它是用C语言编写的,具有高性能和可移植性,允许它在没有脚本语言或cron守护程序的系统上运行,例如嵌入式系统。 它还包括处理数十万个指标的优化和功能。 该守护进程包含100多个插件,从标准案例到非常专业和高级的主题。 它提供强大的网络功能,并且可以通过多种方式进行扩展。 最后但同样重要的是:collectd得到了积极的开发和支持,并且有详细记录。 提供了更完整的功能列表。

安装

  • ubuntu
apt update
apt install -y collectd collectd-utils
  • centos
yum install epel-release
yum install -y collectd

配置

  • vi /etc/collectd/collectd.conf

最主要的加载你所需的丰富的 collectd 插件

#LoadPlugin logfile
LoadPlugin syslog

<Plugin syslog>
        LogLevel info
</Plugin>

##############################################################################
# LoadPlugin section                                                         #
#----------------------------------------------------------------------------#
# Specify what features to activate.                                         #
##############################################################################

LoadPlugin battery
LoadPlugin cpu

LoadPlugin df
LoadPlugin disk
#LoadPlugin dns
#LoadPlugin email
LoadPlugin entropy
LoadPlugin interface
LoadPlugin irq
#LoadPlugin java
#LoadPlugin libvirt
LoadPlugin load
LoadPlugin memory
<Plugin rrdtool>
        DataDir "/var/lib/collectd/rrd"
#       CacheTimeout 120
#       CacheFlush 900
#       WritesPerSecond 30
#       CreateFilesAsync false
#       RandomTimeout 0
#
# The following settings are rather advanced
# and should usually not be touched:
#       StepSize 10
#       HeartBeat 20
#       RRARows 1200
#       RRATimespan 158112000
#       XFF 0.1
</Plugin>

启动

service collectd start
service collectd status
tail /var/log/syslog

插件

CPU

一图胜千言, 看看这张 CPU 使用率的图表

1598924-fa10ec05c673611c.png
cpu metrics

CPU的基本架构如下

1598924-291de9d747277f3d.png
cpu architecture

CPU 是以一个特定的时钟频率执行的, 比如 5GHz 的 CPU 每秒运行 50 亿个时钟周期, 一般来说时钟频率越快, CPU 性能越好, 对应的指标是 CPI (Cycles Per Instruction) 每指令时钟周期数.

CPU 指令的执行包括如下步骤

  1. 指令预取
  2. 指令解码
  3. 执行
  4. 内存访问
  5. 寄存器写回

CPU 使用率

CPU 的忙闲比即使用率, 即 busy_time/total_time, CPU 会忙于运行用户态应用程序线程, 或者其他的内核线程, 或者在处理中断

用户时间与内核时间

CPU 使用率分为内核时间和用户时间

  • 用户时间: CPU 花在用户态应用程序代码的时间
  • 内核时间: CPU 花在系统调用, 内核线程和中断的时间

用户时间和内核时间之比揭示了应用程序运行的负载类型

  • 计算密集型应用程序: 用户/内核时间比接近 99/1
  • I/O 密集型的系统调用频率较高, 它要通过执行内核代码进行 I/O 操作, 如一般 web 服务器的用户/内核时间比约为 70/30

常用的 CPU 检查命令有 uptime, vmstat, mpstat, top, pidstat, perf, 等等

比如 top 命令的输出

top - 05:01:09 up 107 days, 19:09,  1 user,  load average: 0.00, 0.01, 0.05
Tasks: 111 total,   1 running, 110 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.0 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   4047288 total,  3825860 used,   221428 free,   241544 buffers
KiB Swap:  4190204 total,   632600 used,  3557604 free.  1656504 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 3459 jenkins   20   0 3580416 831472  11752 S   0.0 20.5 134:10.70 java
 1727 999       20   0 2755632 527880  18976 S   0.0 13.0 271:58.04 java
 1200 mysql     20   0 1136664  32288   3484 S   0.3  0.8  65:25.30 mysqld
24300 root      20   0  138644  22488   5952 S   1.0  0.6 159:03.78 python

vmstat 的输出

# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 632600 220636 241544 1656512    0    0     0    22    1    1  1  0 99  0  0
  • r 运行队伍长度
  • us 用户态时间
  • sy 系统态时间
  • id 空闲时间
  • wa 等待 I/O 时间
  • st 偷取(未显示)时间, 虚拟化环境下其他租户的开销

Memory

内存一般是指主存 DRAM, 也就是常驻内存, 广义的内存还包括虚拟内存, 虚拟内存是操作系统向进程所提供的一个线性的私有地址空间, 映射到主存和磁盘上的交换空间。

页是指操作系统所使用的基本内存单位, 当主存不够用时, 操作系统会将页面换入和换出
一个页 page 可能处于如下一个状态
a. 未分配
b. 已分配,未映射(未填充且未缺页)
c. 已分配,已映射到主内存(RAM)
d. 已分配,已映射到物理交换空间 (磁盘)

从状态 b 到 c 的转换就是缺页, 如果需要读写磁盘, 则是严重缺页, 否则就是轻微缺页
RSS(常驻集合大小): 已分配的主存页 c
虚拟内存大小: 所有已分配的区域 (b + c + d)

内存指标最主要看

  1. 使用率: 内存使用的百分比
  2. 饱和度: 换页,交换, "OOM: Kill process"
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 1875060 162564 1757308    0    0     0     1    2    3  0  0 100  0  0

如下指标的单位为 KB

  • swpd: 交换出的内存量
  • free 空闲的可用内存
  • buff 用于缓冲缓存的内存
  • cache 用于页缓存的内存
  • si 换入的内存(换页)
  • so 换出的内存(换页)

使用 free 命令是最方便查看内存使用率的工具

# free -t -m
             total       used       free     shared    buffers     cached
Mem:          3952       3735        217          0        235       1616
-/+ buffers/cache:       1883       2069
Swap:         4091        617       3474
Total:        8044       4353       3691

一般是buffers/cache 的内存可以被程序使用, 所以 free 空间不仅是217m, 而是第二行的 2069m, 把 buffers/cache 也算成可用内存, 已使用内存为 1883m

内存使用率 = 已用内存/总内存 = 1883/3952 = 0.48

1598924-ce45e1972b73cafc.png
memory usage

与 InfluxDB 集成

  • 安装 InfluxDB
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/lsb-release
echo "deb https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

sudo apt-get update 
sudo apt-get install influxdb
service influxdb start
service influxdb status
tail /var/log/influxdb/influxd.log

详细的介绍参见 度量驱动开发之 InfluxDB

  • 添加 collectd 支持
    $ vi /etc/influxdb/influxdb.conf

[[collectd]]
  enabled = true
  bind-address = ":25826"
  database = "collectd"
  # retention-policy = ""
  #
  # The collectd service supports either scanning a directory for multiple types
  # db files, or specifying a single db file.
  typesdb = "/usr/share/collectd/types.db"
  #
  # security-level = "none"
  # auth-file = "/etc/collectd/auth_file"

  • 配置 collectd
    vi /etc/collectd/collectd.conf
LoadPlugin network

<Plugin network>
        Server "10.224.76.178" "25826"
</Plugin>

$ influx -execute 'show series' -database collectd

$ influx -execute 'select * from memory_value limit 10' -database collectd

name: memory_value
time                host type   type_instance value
----                ---- ----   ------------- -----
1546672069335186832 ub14 memory used          1987960832
1546672069335187797 ub14 memory buffered      224665600
1546672069335188176 ub14 memory cached        1642242048
1546672069335188551 ub14 memory free          289554432
1546672079335258226 ub14 memory used          1988050944
1546672079335259194 ub14 memory buffered      224669696
1546672079335260020 ub14 memory cached        1642242048
1546672079335260681 ub14 memory free          289460224
1546672089335281171 ub14 memory used          1988050944
1546672089335282242 ub14 memory buffered      224669696

$ influx -execute 'select * from cpu_value limit 10' -database collectd

name: cpu_value
time                host instance type type_instance value
----                ---- -------- ---- ------------- -----
1546672069334501460 ub14 0        cpu  user          8922189
1546672069334509003 ub14 0        cpu  nice          91148
1546672069334527235 ub14 0        cpu  system        1981116
1546672069334530939 ub14 0        cpu  idle          918718139
1546672069334533948 ub14 0        cpu  wait          105511
1546672069334536611 ub14 0        cpu  interrupt     0
1546672069334537537 ub14 0        cpu  softirq       33480
1546672069334538435 ub14 0        cpu  steal         0

与 Grafana 集成

wget [https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb](https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb) 
dpkg -i grafana_5.4.2_amd64.deb
service grafana-server start

打开 http://10.224.76.178:3000/login, 默认帐号是 admin/admin
修改下默认默认帐号, 设置 datasource

1598924-6b867d9b5f4e80fa.png

然后添加一个仪表盘 dashboard , 面板 Panel 和 图表 Graph

1598924-3aa7453f45245049.png
image.png

总结一下, 整个数据流如下:

1598924-053d1619457a3678.png
flow

参考文档

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值