部署环境:
两台机器(机器1安装collectd采集此机器数据;机器2部署nfluxDB+Grafana,存储数据及图表形式展示数据);注:机器网络互通,并关闭对应的防火墙
采集数据(collectd)-> 持久化(influxDB) -> 显示数据(Grafana)
安装版本:
collectd 5.4.0 下载:https://collectd.org/download.shtml
influxDB 1.6.0 下载:https://portal.influxdata.com/downloads
Grafana 5.2.2 下载:https://grafana.com/grafana/download
collectd部署
- 安装
apt-get install collectd
- 修改配置
root@ubuntu:~# vi /etc/collectd/collectd.conf
FQDNLookup true
BaseDir "/var/lib/collectd"
PIDFile "/var/run/collectd.pid"
PluginDir "/usr/lib64/collectd"
TypesDB "/usr/share/collectd/types.db"
#根据需要配置监控内容
LoadPlugin syslog
LoadPlugin rrdtool
LoadPlugin disk
LoadPlugin interface
LoadPlugin load
LoadPlugin memory
LoadPlugin network
LoadPlugin processes
LoadPlugin users
#(注意成对的注释符)
<Plugin interface>
Interface "eth0" #用ifconfig查看Interface对应名称
IgnoreSelected false
</Plugin>
<Plugin network>
Server "192.168.216.132" "25826" #这里填写的是influxDB安装的服务器ip
</Plugin>
<Plugin rrdtool>
DataDir "/usr/var/lib/collectd/rrd"
</Plugin>
- 安装依赖包
root@ubuntu:/etc/collectd# apt-get install collectd-rrdtool rrdtool rrdtool-devel
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package collectd-rrdtool
E: Unable to locate package rrdtool-devel
- 启动服务
root@ubuntu:/etc/collectd# sudo /etc/init.d/collectd start
* Starting statistics collection and monitoring daemon collectd [ OK ]
- 确认配置是否成功
root@ubuntu:~# cd /var/lib/collectd
root@ubuntu:/var/lib/collectd# ls
rrd
如果/var/lib/collectd目录下生成rrd文件,说明有数据了。
influxDB部署
- 安装influxDB
[root@localhost monitor]# yum install -y http://dl.influxdata.com/influxdb/releases/influxdb-1.6.0.x86_64.rpm
- 启动InfluxDB
[root@localhost monitor]# systemctl start influxdb.service #启动influxdb服务
[root@localhost monitor]# systemctl enable influxdb.service #配置influxdb开机自启
- 配置InfluxDB数据库
进入influxDB
[root@localhost monitor]# influx
Connected to http://localhost:8086 version 1.6.0
InfluxDB shell version: 1.6.0
> create database collectd #创建数据库
> show databases
name: databases
name
----
_internal
collectd
> create user "root" with password \'root\' with all privileges #创建一个管理员权限用户root和密码为root
> quit #退出
- 修改配置文件启用认证
[root@localhost monitor]# sed -i \'s#auth-enabled = false#auth-enabled = true#g\' /etc/influxdb/influxdb.conf
[root@localhost monitor]# systemctl restart influxdb.service #重启influxdb
- 配置InfluxDB支持Collectd
[root@localhost monitor]# vim /etc/influxdb/influxdb.conf
[[collectd]]
enabled = true
bind-address = "192.168.216.132:25826"
database = "collectd"
batch-size = 5000
batch-pending = 10
batch-timeout = "10s"
read-buffer = 0
[root@localhost monitor]# systemctl restart influxdb.service #配置修改后重启服务
查看25826这个端口是否已经监听
[root@localhost ~]# netstat -anp| grep 25826
udp 0 0 192.168.216.132:25826 0.0.0.0:* 5078/influxd
- 确认数据
[root@localhost ~]# influx
Connected to http://localhost:8086 version 1.6.0
InfluxDB shell version: 1.6.0
> show databases
name: databases
name
----
_internal
collectd
> use collectd
Using database collectd
> show field keys #显示所有的keys
name: cpu_value
fieldKey fieldType
-------- ---------
value float
name: df_value
fieldKey fieldType
-------- ---------
value float
name: disk_read
fieldKey fieldType
-------- ---------
value float
name: disk_write
fieldKey fieldType
-------- ---------
value float
name: entropy_value
fieldKey fieldType
-------- ---------
value float
name: interface_rx
fieldKey fieldType
-------- ---------
value float
name: interface_tx
fieldKey fieldType
-------- ---------
value float
name: irq_value
fieldKey fieldType
-------- ---------
value float
name: load_longterm
fieldKey fieldType
-------- ---------
value float
name: load_midterm
fieldKey fieldType
-------- ---------
value float
name: load_shortterm
fieldKey fieldType
-------- ---------
value float
name: memory_value
fieldKey fieldType
-------- ---------
value float
name: processes_value
fieldKey fieldType
-------- ---------
value float
name: swap_value
fieldKey fieldType
-------- ---------
value float
name: users_value
fieldKey fieldType
-------- ---------
value float
> select * from cpu_value limit 15;
name: cpu_value
time host instance type type_instance value
---- ---- -------- ---- ------------- -----
1533706051222270586 localhost 0 cpu user 29165
1533706051222284473 localhost 0 cpu system 15743
1533706051222289059 localhost 0 cpu wait 21161
1533706051222293123 localhost 0 cpu nice 5307
1533706051222297874 localhost 0 cpu interrupt 0
1533706051222301889 localhost 0 cpu softirq 756
1533706051222303497 localhost 0 cpu steal 0
1533706051222305026 localhost 0 cpu idle 1421907
1533706061222793419 localhost 0 cpu user 29233
1533706061222808289 localhost 0 cpu system 15759
1533706061222813120 localhost 0 cpu wait 21161
1533706061222817450 localhost 0 cpu nice 5307
1533706061222822436 localhost 0 cpu interrupt 0
1533706061222826526 localhost 0 cpu softirq 757
1533706061222828185 localhost 0 cpu steal 0
Grafana部署
- 安装Grafana
[root@localhost monitor]# wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.2-1.x86_64.rpm
[root@localhost monitor]# sudo yum localinstall grafana-5.2.2-1.x86_64.rpm
- 启动Grafana服务
[root@localhost monitor]# service grafana-server start #启动
- 配置Grafana
访问地址:http://IP:3000 默认账号为:admin 密码:admin;初始登录后设置密码;
添加Data Sources
保存并测试时报错:Network Error: Bad Gateway(502) ,把URL配置为http://127.0.0.1:8086可保存成功(后面再修改对应具体IP就不报520错误了,-_-||)
添加监控
参考资料:
http://www.simlinux.com/2016/04/28/grafana-influxdb-collectd.html
https://yq.aliyun.com/articles/227006