使用 Telegraf、InfluxDB、Grafana 搭建简单监控步骤

发布时间: 更新时间: 总字数:494 阅读时间:1m 作者: 分享 复制网址

使用 Telegraf(采集数据)、InfluxDB(存储数据)、Grafana(Grafana) 搭建简单监控步骤

Influxdb 安装

influxdb 作为时间序列数据库使用,本文采用 Docker 启动 influxdb:

docker run -d \
  -p 2003:2003 \
  -p 8086:8086 \
  -v /data/influxdb:/var/lib/influxdb \
  -e INFLUXDB_DB=db0 \
  -e INFLUXDB_ADMIN_USER=admin \
  -e INFLUXDB_ADMIN_PASSWORD=admin \
  --restart=always \
  --name influxdb \
  influxdb:1.7.11

访问 influxdb 数据库:

influx -host 127.0.0.1 -port 8086

更多使用参考:Influxdb 数据库使用介绍

Telegraf

Telegraf 作为采集器使用,其中:

/etc/telegraf/telegraf.conf

[agent]
  ## Default data collection interval for all inputs
  interval = "10m"

[[outputs.influxdb]]  # 配置数据库
  urls = ["http://127.0.0.1:8086"]
  database = "telegraf"
  username = "telegraf"
  password = "metricsmetricsmetricsmetrics"
  • 通过 Telegraf 的 inputs.exec 插件执行,配置如下:
# Read metrics from one or more commands that can output to stdout
[[inputs.exec]]
  ## Commands array
  commands = [
    "/tmp/test.sh",
  ]

  ## Timeout for each command to complete.
  timeout = "5s"

  ## measurement name suffix (for separating different commands)
  name_suffix = "_mycollector"

  ## Data format to consume.
  ## Each data format has its own unique set of configuration options, read
  ## more about them here:
  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
  data_format = "influx"

参考:https://github.com/influxdata/telegraf/blob/release-1.8/plugins/inputs/exec/README.md

  • 其中,采集脚本 /tmp/test.sh,也可以使用 python 等封装为 InfluxDB Line Protocol 格式
#!/bin/sh
echo 'example,tag1=a,tag2=b i=42i,j=43i,k=44i'
# echo 'example,tag1=a,tag2=b i=42i,j=43i,k=44i to_timestap("2021-05-30")'
  • 启动 telegraf

systemctl start telegraf.service

Grafana

Grafana 作为展示工具,参考 Grafana 安装,配置数据和配置图即可。

Nginx 方向代理配置

  • nginx.conf
location /grafana/ {
     proxy_pass         http://<grafana>:3000/;
     proxy_set_header   Host $host;
}
  • /etc/grafana/grafana.ini
root_url = http://localhost:3000/grafana/

重启 nginxgrafana 既可以通过 http://<nignx>/grafana/ 访问

最新评论
加载中...
Home Archives Categories Tags Statistics