procfs虚拟文件系统

/proc虚拟文件系统(也叫procfs)是Unix操作系统所使用的虚拟文件系统的Linux实现,包括Sun Solaris、LinuxBSD。在/proc开始时,它以一个标准文件系统出现,并包含与正在运行的进程IDs同样名字的文件。

然而,在/proc中的文件不占用磁盘空间,它们存在于工作存储器(内存)中。/proc最初的目的是便于进程信息的存取,但是现在,在Linux中,它可被内核的每一部分使用来报告某些事情。

在/proc文件系统提供的成百上千的值当中,我们将集中考虑集群监控所需的最小集,它们包括:

每个文件提供的值的数量是不同的。这些文件的完整有效值列表如下。

/proc/loadavg:负载

/proc/meminfo:存储器信息

/proc/net/dev:网卡数据

/proc/stat:系统状态

/proc/uptime:工作时间

值得注意的是,每次某个/proc被读时,一个句柄函数都被内核或特有模块调用,来产生数据。数据在运行中产生,不管是读一个字符还是一个大的字块,整个文件都将被重建。这对效率是至关重要的一点,因为使用/proc的任何系统监控器将吞下整个文件,而不是一点一点地处理它。

tsdb采集loadavg采集脚本

#!/bin/bash
# loadavg-collector.sh

# /proc/loadavg
# 0.00 0.00 0.00 1/173 30545
# The first three fields in this file are load average figures giving the number of jobs in the run queue (state R)  or  waiting
# for  disk  I/O (state D) averaged over 1, 5, and 15 minutes.  They are the same as the load average numbers given by uptime(1)
# and other programs.
# The fourth field consists of two numbers separated by a slash (/).  The first of these is the  number  of currently  executing
# kernel  scheduling entities (processes, threads); this will be less than or equal to the number of CPUs.
# The value after the slash is the number of kernel scheduling entities that currently exist on the system. 
# The fifth field  is the PID of the process that was most recently created on the system.

# 该脚本每15秒输出如下格式内容,并直接通过nc提交给open-tsdb:
# put proc.loadavg.1m 1398245238 0.00 host=tyunwei2.tsptest
# put proc.loadavg.5m 1398245238 0.00 host=tyunwei2.tsptest
set -e 
while true;do
    awk -v now=`date +%s` -v host=`hostname` \
    '{ print "put proc.loadavg.1m "now " "$1" host="host;print "put proc.loadavg.5m "now " "$2" host="host }' /proc/loadavg
    sleep 15
done | nc -w 30 192.168.0.75 4242