数据采集

收到数据采集,那我们主要需要哪些数据呢?

  • 机器的状态,cpu,内存
  • 业务自己产生的数据,如qps等
  • mysql,redis等开源软件自身的状态数据

上面的数据大致可以分为两类:

  • 机器指标
  • 业务数据

采集方式也有两种:push和pull 当agent数量非常庞大时,如果采用pull的方式,server端需要保持大量的链接,非常消耗性能,所以我们采用agent push数据的方式

首先总结下机器指标的采集

cpu

  • cpu.busy
  • cpu.cnt
  • cpu.guest
  • cpu.idle
  • cpu.iowait
  • cpu.irq
  • cpu.nice
  • cpu.softirq
  • cpu.steal
  • cpu.system
  • cpu.user

如何计算cpu利用率呢? 计算原理如下:传送门

首先得到每个cpu的状态,封装成一个函数CurrentProcStat() 代码如下 http://pastebin.ubuntu.com/18247784/

然后将得到的数据存到一个数组 procStatHistory 中,定期跟新数组中的cpu的状态值

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    func UpdateCpuStat() error {
        ps, err := CurrentProcStat() 
        if err != nil {
            return err
        }
        psLock.Lock()
        defer psLock.Unlock()
        for i := historyCount - 1; i > 0; i-- {
            procStatHistory[i] = procStatHistory[i-1]
        }
        procStatHistory[0] = ps
        return nil
    }

计算结果

cpuIdle := float64(procStatHistory[0].Cpu.Idle-procStatHistory[1].Cpu.Idle)*100.00/float64(procStatHistory[0].Cpu.Total - procStatHistory[1].Cpu.Total)

内存

一般内存监控一下几个指标即可

  • memfree
  • memtotal
  • memused
  • swapfree
  • swaptotal
  • swapused

这些指标的数据可以从/proc/meminfo中获取,linux下输入 cat /proc/meminfo 得到以下信息 几个主要指标的说明

MemTotal — 总的内存大小,单位kb
MemFree    — 空闲的内存大小,单位kb
SwapTotal — 总swap大小,单位kb
SwapFree — 空闲wsap大小,单位kb

获取内存的具体代码实现 http://pastebin.ubuntu.com/16199860/

磁盘相关采集项

  • df.bytes.free
  • df.bytes.total
  • df.bytes.used
  • df.inodes.free
  • df.inodes.total
  • df.inodes.used

linux下查看命令:df -lh(/ih) 采集方法: 读取/proc/mounts拿到所有挂载点 具体代码:http://pastebin.ubuntu.com/16201041

通过golang syscall包中的 Statfs函数得到磁盘blocks和inode的使用情况。 具体代码如下:http://pastebin.ubuntu.com/18287842

磁盘io采集项

  • disk.io.avgqu-sz 平均每次设备I/O操作的数据大小
  • disk.io.avgrq-sz 平均I/O队列长度
  • disk.io.await 平均每次设备I/O操作的等待时间
  • disk.io.ios_in_progress
  • disk.io.msec_read 读消耗时间
  • disk.io.msec_write 写消耗时间
  • disk.io.msec_total io总消耗时间
  • disk.io.msec_weighted_total 加权io总消耗时间
  • disk.io.read_bytes
  • disk.io.read_merged
  • disk.io.read_requests
  • disk.io.read_sectors
  • disk.io.write_bytes
  • disk.io.write_merged
  • disk.io.write_requests
  • disk.io.write_sectors
  • disk.io.svctm 平均每次io消耗时间
  • disk.io.util 单位时间内(1s)读写占用时间比例

linux下查看命令:iostat

机器负载采集项

  • load.1min
  • load.5min
  • load.15min 采集方法:直接读取/proc/loadavg
    linux下查看命令:top

网络相关采集项

  • net.if.in.compressed
  • net.if.in.dropped
  • net.if.in.errors
  • net.if.in.fifo.errs
  • net.if.in.frame.errs
  • net.if.in.multicast
  • net.if.in.packets
  • net.if.out.bytes
  • net.if.out.carrier.errs
  • net.if.out.collisions
  • net.if.out.compressed
  • net.if.out.dropped
  • net.if.out.errors
  • net.if.out.fifo.errs
  • net.if.out.packets
  • net.if.total.bytes
  • net.if.total.dropped
  • net.if.total.errors
  • net.if.total.packets

采集方法,读取/proc/net/dev的内容 具体实现代码:http://pastebin.ubuntu.com/18293996/

端口信息采集

linux 查看开放端口命令:ss -tln 采集方法:利用golang 调用系统命令的方式获取开放的端口

进程状态采集

由于系统运行的进程非常多,所以这类采集需要用户配置 采集方法:读取 /proc/pid/status 下的内容

总结

机器相关的采集项基本就是这些了,接下来将思考怎样接收业务数据,以及对数据的转发处理


参考:

回顾: