1. 简介
1.1 Collectd
Collectd 是一个守护进程,可以定期收集系统和应用程序的性能指标,同时提供了以不同方式来存储这些指标的机制。
Collectd 可以从操作系统、应用程序、日志文件和外部设备等不同来源收集指标。这些指标可以用来监控系统、发现性能瓶颈、并预测未来的系统负载等。
1.2 InfluxDB
InfluxDB 是一个开源的时序数据库,适合存储指标、事件、分析等数据。
1.3 Grafana
Grafana 是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知。
2. 搭建环境
本次搭建需要两台 Server:
- Server1:安装 Collectd,收集的数据存入 Server2 的 InfluxDB 中
- Server2:安装 InfluxDB 和Grafana,Grafana 将 InfluxDB 中存储的数据通过图表方式展现出来
3. 搭建过程
3.1 InfluxDB 安装
设置 yum 源:
1 2 3 4 5 6 7 8 9 |
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo > [influxdb] > name = InfluxDB Repository - RHEL \$releasever > baseurl = https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable > enabled = 1 > gpgcheck = 1 > gpgkey = https://repos.influxdata.com/influxdb.key > EOF |
安装:
1 2 |
yum install influxdb |
修改 InfluxDB 的配置文件使其支持从 Collectd 接收数据:
1 2 3 4 5 6 7 8 9 10 11 |
[[collectd]] enabled = true bind-address = ":25826" database = "collectd" retention-policy = "" typesdb = "/usr/local/share/collectd/types.db" batch-size = 5000 batch-pending = 10 batch-timeout = "10s" read-buffer = 0 |
下载 types.db:
1 2 3 |
mkdir /usr/local/share/collectd wget -P /usr/local/share/collectd https://raw.githubusercontent.com/collectd/collectd/master/src/types.db |
创建一个名为 Collectd 的数据库:
1 2 |
curl -i -XPOST http://server2:8086/query -u dorothy:DorothyStack@2018 --data-urlencode "q=CREATE DATABASE collectd" |
启动 InfluxDB:
1 2 |
service influxdb start |
3.2 Collectd 安装
下载源码:
1 2 |
wget https://storage.googleapis.com/collectd-tarballs/collectd-5.8.1.tar.bz2 |
解压:
1 2 |
tar xf collectd-5.8.1.tar.bz2 |
编译:
1 2 3 4 |
cd collectd-5.8.1 ./configure make all install |
配置 /opt/collectd/etc/collectd.conf:
1 2 3 4 5 6 7 8 9 |
# 启动这些 LoadPlugin cpu LoadPlugin disk LoadPlugin load LoadPlugin memory LoadPlugin processes LoadPlugin swap LoadPlugin users |
配置 InfluxDB 服务地址:
1 2 3 4 |
<Plugin "network"> Server "server2" "25826" </Plugin> |
启动 Collectd:
1 2 |
/opt/collectd/sbin/collectd |
3.3 Grafana 安装
下载并安装:
1 2 3 |
wget https://dl.grafana.com/oss/release/grafana-6.0.0-1.x86_64.rpm yum localinstall grafana-6.0.0-1.x86_64.rpm |
修改配置文件,设置访问的 URL 地址和端口等:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[server] # Protocol (http, https, socket) ;protocol = http # The ip address to bind to, empty will bind to all interfaces ;http_addr = # The http port to use http_port = 8000 # The public facing domain name used to access grafana from a browser domain = server2 # The full public facing url you use in browser, used for redirects and emails # If you use reverse proxy and sub path specify full url (with sub path) root_url = http://server:8000 |
启动 Grafana:
1 2 |
systemctl start grafana-server |
配置 Grafana:
在浏览器中输入 http://IP:8000 ,访问 Grafana。
点击 config datatsource,选择 InfluxDB:
在 InfluxDB Details 中输入 Collectd 中创建的 DatatBase 名称以及用户名和密码。
点击左侧的+号,创建一个 Dashboard:
然后点击 add panel 创建一个新的查询:
这样 Grafana 就配置完了。
本文作者:张迪