目录

安装collectd (server)

collectd server用于收集collectd agent发送过来的数据,是一个被动的过程!

执行./configure之后会输出一张支持插件的列表(如下图)如果需要安装别的插件需要重新编译collectd

安装collectd (agent)

collectd agent定时收集数据并发送到collectd server,是一个主动的过程!

安装influxdb

用于接收collectd 发送过来的数据,并写入数据库。并在此基础上提供了查询功能!


安装Grafana

一个纯web显示程序,可以从influxdb数据源读取数据

collectd.conf(server端)

#----------- global setting ---------------
FQDNLookup   false                # 关闭主机名验证
Interval     10                   # 全局采样时间间隔
Timeout      2                    
ReadThreads  5                    # 开启 5 个读进程
WriteThreads 5                    # 开启 5 个写进程

#------------ simple plugin ----------------
LoadPlugin cpu
LoadPlugin interface
LoadPlugin load
LoadPlugin memory
LoadPlugin disk

#------------ complex plugin ---------------
LoadPlugin snmp
LoadPlugin statsd     # 默认开启8125端口
LoadPlugin rrdtool
LoadPlugin network
LoadPlugin logfile

#------------ detail for complux plugin  ----------------
<Plugin logfile>
        LogLevel info                           # 日志级别 debug|info|notice|warning|err
        File "/usr/local/collectd/collectd.log" # 日志文件位置
</Plugin>
<Plugin network>
        Server "192.168.16.146" "25826"         # 自己作为客户端,将数据发送到某个服务器的ip,port
        <Listen "192.168.16.145" "25826">       # 自己作为服务器,绑定一个接收客户端数据的ip,port
                SecurityLevel Sign              # 验证级别 debug|info|notice|warning|err
                AuthFile "/usr/local/collectd/etc/authfile.txt" # 验证文件,账号密码以键值对的方式存储,如:upyun: 123
        </Listen>
        Forward true
</Plugin>
<Plugin rrdtool>  # 将客户端和本机的数据写入rrd文件
        DataDir "/usr/local/collectd/var/lib/collectd/rrd"
</Plugin>
<Plugin snmp>  # 用snmp插件去主动收集客户端数据
    <Data "hr_users">
      Type "users"                                     # 见/usr/local/collectd/share/collectd/types.db
      Table false
      Instance ""
      Shift -1
      Values "HOST-RESOURCES-MIB::hrSystemNumUsers.0"  # 收集客户端用户登录数
    </Data>
    <Host "192.168.16.144">
      Address "192.168.16.144"                         # 客户端ip
      Version 2                                        # snmp 协议版本
      Community "public"                               # 见/etc/snmp/snmpd.conf中的 community字段
      Collect "hr_users"                               # 收集哪些数据,可以指定多个<Data></Data>
      Interval 10                                      # 采样周期,可以覆盖全局变量 Interval 
    </Host>
</Plugin> 

collectd.conf(agentd端)

#----------- global setting ---------------
FQDNLookup   false
Interval     10
Timeout      2
ReadThreads  5
WriteThreads 5

#------------ simple plugin ----------------
LoadPlugin cpu                 
LoadPlugin interface              
LoadPlugin load
LoadPlugin memory
LoadPlugin disk

##------------ complex plugin ---------------
LoadPlugin statsd
LoadPlugin network
LoadPlugin logfile

##------------ detail for complux plugin  ----------------
<Plugin logfile>
        LogLevel info
        File "/usr/local/collectd/collectd.log"
</Plugin>
<Plugin network>
        <Server "192.168.16.145" "25826">
                SecurityLevel Sign  # 校验级别,和服务器端保持一致
                Username "upyun"    # 账号
                Password "123"      # 密码
        </Server>
</Plugin>

collectd(collectd服务配置脚本)

#!/bin/bash
# chkconfig: 2345 10 90
# description: collectd 

start() {
        /usr/local/collectd/sbin/collectd
}

stop() {
        kill -9 `pidof collectd`
}

restart() {
        stop
        start
}
status(){
        echo $"Usage: {start|stop|restart|status|help}"
}

case "$1" in
  start)
        pidof collectd > /dev/null
        if [ $? == 1 ] ; then
                start
                echo "collectd started"
        else
                echo "collectd is already runing..."
        fi
        ;;
  stop)
        stop
        echo "collectd stop"
        ;;
  restart)
        restart
        ;;
  status)
        pidof collectd > /dev/null
        if [ $? == 0 ] ; then
                echo "collectd is runing..."
        else
                echo "collectd is not runing..."
        fi
        ;;
  *)
        status
        exit 2
esac

exit $?