===== 安装collectd (server)=====
collectd server用于收集collectd agent发送过来的数据,是一个被动的过程!
- 安装collectd需要支持的插件
yum install -y net-snmp-devel(snmp插件依赖这个库)
yum install -y rrdtool-devel(rrdtool插件依赖这个库)
yum install -y libgcrypt-devel(network插件加密依赖这个库)
yum install -y python-devel(依赖这个库)
yum install -y perl-devel(编译依赖perl库)
yum install -y python-requests(python influxdb plugin 的依赖库)
yum install -y liboping-devel (ping 依赖的库)
- 安装collectd
wget http://collectd.org/files/collectd-5.4.2.tar.gz
tar zxvf collectd-5.4.2.tar.gz
cd collectd-5.4.2
./configure --prefix=/usr/local/collectd --enable-all-plugins
make ; make install
- 修改collectd配置文件
cd /usr/local/collectd/etc/
cp collectd.conf collectd.conf.bak(collectd.conf文件中具体内容见文章末尾collectd.conf.server)
- collectd配置成服务
vim /etc/rc.d/init.d/collectd (collectd 文件中具体内容见文章末尾collectd.txt)
chmod u+x /etc/rc.d/init.d/collectd
chkconfig --add collectd
chkconfig collectd on
server collectd {start|stop|restart|status|help}
执行./configure之后会输出一张支持插件的列表(如下图)如果需要安装别的插件需要重新编译collectd
{{:pasted:20150411-214212.png}}
===== 安装collectd (agent)=====
collectd agent定时收集数据并发送到collectd server,是一个主动的过程!
* yum install -y net-snmp
* chkconfig snmpd on # snmp设置成开机启动
* echo "view systemview included .1">> /etc/snmp/snmpd.conf # 让snmp监控所有字段
* service snmpd start
* yum install -y net-snmp-utils # snmp客户端工具,用于调试。
* 常见调试命令
* snmpwalk -v 2c -c public 192.168.16.144 # 查看所有字段
* snmpwalk -v 2c -c public 192.168.16.144 system # 查看系统字段
其他步骤和安装collectd server一样,但是不需要额外安装插件
----
===== 安装influxdb =====
用于接收collectd 发送过来的数据,并写入数据库。并在此基础上提供了查询功能!
- wget https://s3.amazonaws.com/influxdb/influxdb-latest-1.x86_64.rpm
- rpm -ivh influxdb-latest-1.x86_64.rpm
- chkconfig influxdb on
- service influxdb start
- 登录influxdb管理后台 http://192.168.16.146:8083/
- 默认账号:root 密码:root
- 创建一个数据库
- curl https://raw.githubusercontent.com/collectd/collectd/master/src/types.db -o /opt/influxdb/shared/types.db # 下载collectd插件支持的数据类型映射表
- vim /opt/influxdb/shared/config.toml(配置influxdb的collectd插件,选择数据库引擎,并重启influxdb) [input_plugins.collectd]
enabled = true
address = "0.0.0.0" # If not set, is actually set to bind-address.
port = 25826
database = "collectd"
# types.db can be found in a collectd installation or on github:
# https://github.com/collectd/collectd/blob/master/src/types.db
typesdb = "/opt/influxdb/shared/types.db"
default-engine = "rocksdb" # 选择数据库引擎
=== influx常用命令 ===
* list series
* select * from /.*/
* select * from “10.0.2.220/interface-eth0/if_octets” where dsname = 'tx'
* 表明用双引号,列值用单引号
----
===== 安装Grafana =====
一个纯web显示程序,可以从influxdb数据源读取数据
- wget http://grafanarel.s3.amazonaws.com/grafana-1.9.1.tar.gz
- tar xzvf grafana-1.9.1.tar.gz
- cd grafana-1.9.1.tar.gz
- cp config.sample.js config.js
- 最后将整个目录放到nginx或apache下即可
- 登录grafana http://192.168.16.146
config.js
# 取消如下代码的注释
datasources: {
influxdb: {
type: 'influxdb',
url: "http://192.168.16.146:8086/db/collectd",
username: 'root',
password: 'root',
},
},
===== 配置文件 =====
==== 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 ----------------
LogLevel info # 日志级别 debug|info|notice|warning|err
File "/usr/local/collectd/collectd.log" # 日志文件位置
Server "192.168.16.146" "25826" # 自己作为客户端,将数据发送到某个服务器的ip,port
# 自己作为服务器,绑定一个接收客户端数据的ip,port
SecurityLevel Sign # 验证级别 debug|info|notice|warning|err
AuthFile "/usr/local/collectd/etc/authfile.txt" # 验证文件,账号密码以键值对的方式存储,如:upyun: 123
Forward true
# 将客户端和本机的数据写入rrd文件
DataDir "/usr/local/collectd/var/lib/collectd/rrd"
# 用snmp插件去主动收集客户端数据
Type "users" # 见/usr/local/collectd/share/collectd/types.db
Table false
Instance ""
Shift -1
Values "HOST-RESOURCES-MIB::hrSystemNumUsers.0" # 收集客户端用户登录数
Address "192.168.16.144" # 客户端ip
Version 2 # snmp 协议版本
Community "public" # 见/etc/snmp/snmpd.conf中的 community字段
Collect "hr_users" # 收集哪些数据,可以指定多个
Interval 10 # 采样周期,可以覆盖全局变量 Interval
==== 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 ----------------
LogLevel info
File "/usr/local/collectd/collectd.log"
SecurityLevel Sign # 校验级别,和服务器端保持一致
Username "upyun" # 账号
Password "123" # 密码
==== 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 $?