使用Grafana + Influxdb + Telegraf搭建监控平台

需求

现在大部分的服务器都会选择云服务器,一般云服务器提供商的后台控制面板也都可以看到服务器的性能状况,包括内存、CPU、网络、IO等资料。那么如果是本地自建的服务器呢?经过一番搜索和了解,最终选定了使用Grafana + Influxdb + Telegraf来自己搭建一个监控平台。

Influxdb

Influxdb是一个数据库,这里用于记录我们需要监控的数据(CPU使用率等)。

InfluxDB是一个由InfluxData开发的开源时序型数据库。它由Go写成,着力于高性能地查询与存储时序型数据。InfluxDB被广泛应用于存储系统的监控数据,IoT行业的实时数据等场景。

安装

有两种安装方式:

1、https://docs.influxdata.com/influxdb/v1.2/introduction/installation/#installation 选择对应的系统按照说明执行。

2、https://portal.influxdata.com/downloads 页面选择InfluxDB后在弹窗里选择对应的系统按照说明执行。

安装成功后,按照第一种方式说明文档里的方法启动influxdb服务。建议同时设置服务开机启动。

配置

Influxdb的配置文件在 /etc/influxdb/influxdb.conf,有需要可以修改这个配置,然后重启influxdb服务。

创建一个数据库,详见:https://docs.influxdata.com/influxdb/v1.2/introduction/getting_started/#creating-a-database

1
2
3
4
5
$ influx -precision rfc3339
Connected to http://localhost:8086 version 1.2.x
InfluxDB shell 1.2.x
>CREATE DATABASE grafana_db
>

Telegraf

Telegraf用于收集系统数据,然后插入Influxdb。

Telegraf is an agent written in Go for collecting metrics and writing them into InfluxDB or other possible outputs. This guide will get you up and running with Telegraf. It walks you through the download, installation, and configuration processes, and it shows how to use Telegraf to get data into InfluxDB.

安装

有两种安装方式:

1、https://docs.influxdata.com/telegraf/v1.3/introduction/installation/#installation 选择对应的系统按照说明执行。

2、https://portal.influxdata.com/downloads 页面选择Telegraf后在弹窗里选择对应的系统按照说明执行。

安装成功后,按照第一种方式说明文档里的方法启动telegraf服务。建议同时设置服务开机启动。

配置

Telegraf的配置文件在 /etc/telegraf/telegraf.conf,有需要可以修改这个配置,然后重启telegraf服务。

这里我们需要修改几个地方。

1、[[outputs.influxdb]]节点,修改数据库信息。

1
2
urls = ["http://localhost:8086"]
database = "grafana_db"

2、[[inputs.xxx]]等节点,根据需要打开或注释对应的信息,可以配置是否收集这些信息。比如net节点默认没打开,这里给他打开。

1
2
[[inputs.net]]
interfaces = ["eth0", "eth1"]

Grafana

Grafana是一个强大且美观的可视化界面系统。

Grafana is an open source metric analytics & visualization suite. It is most commonly used for visualizing time series data for infrastructure and application analytics but many use it in other domains including industrial sensors, home automation, weather, and process control.

安装

按照http://docs.grafana.org/installation/rpm/的说明(左侧菜单选择对应的系统)安装Grafana,启动并设置开机启动grafana-server服务。

配置

Grafana的配置文件在 /etc/grafana/grafana.ini,有需要可以修改这个配置,然后重启grafana-server服务。

使用

至此,需要的软件都已安装完毕。

这个时候我们访问 http://localhost:3000,输入账号密码(可以在/etc/grafana/grafana.ini[security]节点找到默认的账号密码),就可以进入控制面板了。

Data Sources

在试用Grafana查看数据前,我们需要先设置数据库连接。在左上角的菜单中选择Data Sources,添加新的data source。Type中选择InfluxDB,Url里填http://localhost:8086,数据库填写刚才我们建立的grafana_db,账号密码根据InfluxDB有设置的话就填写。然后保存,保存的时候系统会顺便进行检查配置是否正确,是否可以连接的上,如果报错,检查一下配置。

Dashboards

Grafana的dashboard非常强大,用户可以自己定义各种条件生成曲线图。但是还有更轻松的方法:直接用别人做好的面板!

在这里可以找到各式各样的Dashboards: https://grafana.com/dashboards。 因为我们只安装了Influxdb,这里的Data Source请选择Influxdb筛选。

这里我选择了Dynamic Dashboard。可以看到他的ID是79,我们记下来。然后到我们的控制面板,在左上角的菜单中选择Dashboards => Import,弹窗中输入79,点击Load按钮。在下面的Options => telegraf的选项中选择我们刚才建立的Data Source。点击Import完成导入。

这个时候我们再到左上角的Dashboards切换到Dynamic Dashboard,就可以看到服务器的数据了。