社区亮点:如何构建 InfluxDB 模板

导航至

InfluxDB Community member Ignacio Van Droogenbroeck本文由 InfluxAce Ignacio Van Droogenbroeck 撰写。

Ignacio 是一位常驻乌拉圭的技术营销工程师。他大约十年前开始写 博客,内容关于 IT 基础设施、云、Docker、Linux 和可观测性。

在过去的几个月里,我一直积极为 InfluxDB 社区做贡献,为 InfluxDB 模板 for InfluxDB 2.0 构建 InfluxDB 模板。InfluxDB 是 专门构建的时间序列数据库,用于指标收集和存储。让我们一起构建一个 InfluxDB 模板来监控 PostgreSQL 吧!????????

在本教程中,我将向您展示如何为 InfluxDB 2.0 创建 InfluxDB 模板。“InfluxDB 模板”不仅指仪表板,还包括其他元素,例如“标签”、“存储桶”(您在其中保存信息)和 Telegraf 配置。

您可以使用本教程作为指南来创建其他 InfluxDB 模板,并为这个项目做出贡献。让我们开始吧!

构建 InfluxDB 模板的要求

创建 InfluxDB 模板的首要步骤之一是查看所有可用的 Telegraf 输入插件。使用该列表作为跳板,我可以缩小 InfluxDB 模板候选范围。这次,我选择为 PostgreSQL 构建一个 InfluxDB 模板。您可以在 GitHub 上找到 PostgreSQL 数据库的 Telegraf 插件。

构建 InfluxDB 模板的另一个要求是运行 InfluxDB 2.0 实例。例如,我有一个专门的 Docker 容器用于实验和构建新的 InfluxDB 模板。

设置 InfluxDB 2.0 和 Telegraf

创建 InfluxDB 模板的主要原因之一是了解软件如何公开指标。在本例中,我深入研究了 PostgreSQL 文档。它简单易懂,并且很容易开始监控数据库。正如您从下面的 Telegraf 输入中看到的那样,很容易理解它的工作原理。

接下来,我在 InfluxDB 中创建一个“存储桶”来存储这些 PostgreSQL 指标。我只需转到我的实例并导航到“加载数据/存储桶”,然后创建一个名为 'postgres' 的存储桶。我还为该资源设置了一个同名(postgres)的 标签

这是我的 Telegraf 配置,它使用 Postgres 插件。您可以从 InfluxDB GUI 获取输出配置

[[outputs.influxdb_v2]]
  ## The URLs of the InfluxDB cluster nodes.
  ##
  ## Multiple URLs can be specified for a single cluster, only ONE of the
  ## urls will be written to each interval.
  ## urls exp: http://127.0.0.1:9999
  urls = ["http://localhost:9999"]

  ## Token for authentication.
  token = "$INFLUX_TOKEN"

  ## Organization is the name of the organization you wish to write to; must exist.
  organization = "$INFLUX_ORG"

  ## Destination bucket to write into.
  bucket = "postgres"

  [agent]
  interval = "1m"

 [[inputs.postgresql]]
  address = "postgres://postgres:mysecretpassword@localhost:5432"
  ignored_databases = ["template0", "template1"]

在使用此配置文件运行 Telegraf 之前,我们需要将一些数据作为变量传递——在本例中是 TokenOrganization。您可以使用以下示例来做到这一点

export INFLUX_TOKEN='your-token'

export INFLUX_ORG='your-organization'

完成这些后,我们就可以运行 Telegraf 了!????

使用 InfluxDB 监控 PostgreSQL

让我们通过以调试模式“开启”运行 Telegraf 来开始监控我们的数据库,以了解正在发生的事情。

telegraf --config psql.conf --debug

结果如下所示

2020-06-12T14:38:35Z I! Starting Telegraf 1.14.3
2020-06-12T14:38:35Z I! Loaded inputs: postgresql
2020-06-12T14:38:35Z I! Loaded aggregators: 
2020-06-12T14:38:35Z I! Loaded processors: 
2020-06-12T14:38:35Z I! Loaded outputs: influxdb_v2
2020-06-12T14:38:35Z I! Tags enabled: host=thelab
2020-06-12T14:38:35Z I! [agent] Config: Interval:1m0s, Quiet:false, Hostname:"thelab", Flush Interval:10s
2020-06-12T14:38:35Z D! [agent] Initializing plugins
2020-06-12T14:38:35Z D! [agent] Connecting outputs
2020-06-12T14:38:35Z D! [agent] Attempting connection to [outputs.influxdb_v2]
2020-06-12T14:38:35Z D! [agent] Successfully connected to outputs.influxdb_v2
2020-06-12T14:38:35Z D! [agent] Starting service inputs
2020-06-12T14:38:50Z D! [outputs.influxdb_v2] Buffer fullness: 0 / 10000 metrics
2020-06-12T14:39:00Z D! [outputs.influxdb_v2] Buffer fullness: 0 / 10000 metrics
2020-06-12T14:39:10Z D! [outputs.influxdb_v2] Wrote batch of 2 metrics in 145.099172ms

正如您所看到的,Telegraf 正在运行并且已开始将指标发送到 InfluxDB。几分钟后,您可以转到 InfluxDB GUI,特别是数据浏览器,选择“postgres”存储桶,然后查看测量指标和字段都在那里。

它奏效了——一切进展顺利,数据正在被收集。????????

可视化数据

我们准备好创建仪表板并使用数据了。首先,我转到仪表板,然后单击创建仪表板。之后,我命名并分配一个标签。最好使用我们之前使用的名称——在本例中是 postgres 标签。

接下来,我开始使用我已经存储在 InfluxDB 中的数据。我还查看了其他已创建的仪表板。例如,我查看 Grafana 仪表板并从中获取灵感。完成的仪表板应该看起来像这样

InfluxAce Ignacio Van Droogenbroeck shows how to build an InfluxDB Template

这一步非常重要,因为我们需要创建一个包含对社区有帮助的数据的仪表板。如果您创建一个图表,其中包含任意顺序的“随机”数据,则该仪表板将不会被使用。

现在,到了下一个有趣的部分,让我们打包这个模板以发布!

打包 InfluxDB 模板

当我谈论打包或导出模板时,我的意思不仅是指仪表板,还包括存储桶、Telegraf 配置和标签。

现在我们在 InfluxDB 中有了仪表板、标签和存储桶,但我们的 Telegraf 配置在一个单独的文件中。我们需要将所有内容合并到一个很棒的 YAML 文件中。

由于我在 Docker 容器中运行 InfluxDB,因此我需要运行如下所示的导出命令。(记住:在此过程中,我们分配给资源的标签非常有意义。)

docker exec -it 4d410b0f82ba influx pkg export all --filter labelName=postgres -f postgres.yml -o $INFLUX_ORG -t $INFLUX_TOKEN

接下来,我需要从我的容器中拉出文件到我的机器,以便开始编辑和合并 Telegraf 配置

docker cp 4d410b0f82ba:/postgres.yml

我刚刚导出的文件看起来像这样(这是一个摘录)

apiVersion: influxdata.com/v2alpha1
kind: Label
metadata:
    name: flamboyant-dubinsky-332001
spec:
    color: '#F95F53'
    name: postgres
---
apiVersion: influxdata.com/v2alpha1
kind: Bucket
metadata:
    name: vivid-heisenberg-732003
spec:
    associations:
      - kind: Label
        name: flamboyant-dubinsky-332001
    name: postgres
---
apiVersion: influxdata.com/v2alpha1
kind: Dashboard
metadata:
    name: jovial-montalcini-f32001
spec:
    associations:
      - kind: Label
        name: flamboyant-dubinsky-332001
    charts:
      - colors:
          - hex: '#00C9FF'
            name: laser
            type: min
          - hex: '#9394FF'
            name: comet
            type: max
            value: 100
        decimalPlaces: 2
        height: 3
        kind: Gauge
        name: Current IOwait
        queries:
          - query: |-
                from(bucket: "postgres")
                  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
                  |> filter(fn: (r) => r["_measurement"] == "cpu")
                  |> filter(fn: (r) => r["_field"] == "usage_iowait")

在文件末尾,我们将添加经过一些调整的 Telegraf 配置。请记住,这是一个 YAML 文件,因此缩进非常重要。

Telegraf 配置部分如下所示

apiVersion: influxdata.com/v2alpha1
kind: Telegraf
metadata:
    name: postgres-config
spec:
    config: |

        [[outputs.influxdb_v2]]
          ## The URLs of the InfluxDB cluster nodes.
          ##
          ## Multiple URLs can be specified for a single cluster, only ONE of the
          ## urls will be written to each interval.
          ## urls exp: http://127.0.0.1:9999
          urls = ["$INFLUX_HOST"]
        
          ## Token for authentication.
          token = "$INFLUX_TOKEN"
        
          ## Organization is the name of the organization you wish to write to; must exist.
          organization = "$INFLUX_ORG"
        
          ## Destination bucket to write into.
          bucket = "postgres"
              
          [agent]
          interval = "1m"
          
        [[inputs.postgresql]]
          # address = "postgres://postgres:mysecretpassword@localhost:5432"
          address = "$PSQL_STRING_CONNECTION"
          ignored_databases = ["template0", "template1"]
        
        [[inputs.cpu]]
          ## Whether to report per-cpu stats or not
          percpu = true
          ## Whether to report total system cpu stats or not
          totalcpu = true
          ## If true, collect raw CPU time metrics.
          collect_cpu_time = false
          ## If true, compute and report the sum of all non-idle CPU states.
          report_active = false
        
        [[inputs.disk]]
          ## By default stats will be gathered for all mount points.
          ## Set mount_points will restrict the stats to only the specified mount points.
          # mount_points = ["/"]
        
          ## Ignore mount points by filesystem type.
          ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
        
        # Read metrics about disk IO by device
        [[inputs.diskio]]
          ## By default, telegraf will gather stats for all devices including
          ## disk partitions.
          ## Setting devices will restrict the stats to the specified devices.
          # devices = ["sda", "sdb"]
          ## Uncomment the following line if you need disk serial numbers.
          # skip_serial_number = false
          #
          ## On systems which support it, device metadata can be added in the form of
          ## tags.
          ## Currently only Linux is supported via udev properties. You can view
          ## available properties for a device by running:
          ## 'udevadm info -q property -n /dev/sda'
          ## Note: Most, but not all, udev properties can be accessed this way. Properties
          ## that are currently inaccessible include DEVTYPE, DEVNAME, and DEVPATH.
          # device_tags = ["ID_FS_TYPE", "ID_FS_USAGE"]
          #
          ## Using the same metadata source as device_tags, you can also customize the
          ## name of the device via templates.
          ## The 'name_templates' parameter is a list of templates to try and apply to
          ## the device. The template may contain variables in the form of '$PROPERTY' or
          ## '${PROPERTY}'. The first template which does not contain any variables not
          ## present for the device is used as the device name tag.
          ## The typical use case is for LVM volumes, to get the VG/LV name instead of
          ## the near-meaningless DM-0 name.
          # name_templates = ["$ID_FS_LABEL","$DM_VG_NAME/$DM_LV_NAME"]
        
        # Read metrics about memory usage
        [[inputs.mem]]
          # no configuration

注意:我将 PostgreSQL 字符串连接转换为变量。凭据最初是硬编码在文件中的,但现在可以通过环境变量设置。

$ export PSQL_STRING_CONNECTION=postgres://postgres:mysecretpassword@localhost:5432

保存文件后,我们准备好进行测试,看看导入过程是否顺利,以及 InfluxDB 模板是否可以发布。首先,我将新的 YAML 文件复制到容器中。

docker cp postgres.yml 4d410b0f82ba:/

然后我运行导入过程

docker exec -it 4d410b0f82ba influx pkg -f postgres.yml -o $INFLUX_ORG -t $INFLUX_TOKEN

一切设置完成后,我终端的响应应该如下所示

LABELS    +add | -remove | unchanged
+-----+----------------------------+------------------+---------------+---------+-------------+
| +/- |        PACKAGE NAME        |        ID        | RESOURCE NAME |  COLOR  | DESCRIPTION |
+-----+----------------------------+------------------+---------------+---------+-------------+
|     | flamboyant-dubinsky-332001 | 05d639e9629a9000 | postgres      | #F95F53 |             |
+-----+----------------------------+------------------+---------------+---------+-------------+
|                                                                        TOTAL  |      1      |
+-----+----------------------------+------------------+---------------+---------+-------------+

BUCKETS    +add | -remove | unchanged
+-----+-------------------------+------------------+---------------+------------------+-------------+
| +/- |      PACKAGE NAME       |        ID        | RESOURCE NAME | RETENTION PERIOD | DESCRIPTION |
+-----+-------------------------+------------------+---------------+------------------+-------------+
|     | vivid-heisenberg-732003 | 05d639b5fdf32000 | postgres      | 0s               |             |
+-----+-------------------------+------------------+---------------+------------------+-------------+
|                                                                         TOTAL       |      1      |
+-----+-------------------------+------------------+---------------+------------------+-------------+

DASHBOARDS    +add | -remove | unchanged
+-----+--------------------------+----+---------------+-------------+------------+
| +/- |       PACKAGE NAME       | ID | RESOURCE NAME | DESCRIPTION | NUM CHARTS |
+-----+--------------------------+----+---------------+-------------+------------+
| +   | jovial-montalcini-f32001 |    | Postgres      |             | 17         |
+-----+--------------------------+----+---------------+-------------+------------+
|                                                          TOTAL    |     1      |
+-----+--------------------------+----+---------------+-------------+------------+

TELEGRAF CONFIGURATIONS    +add | -remove | unchanged
+-----+-----------------+----+-----------------+-------------+
| +/- |  PACKAGE NAME   | ID |  RESOURCE NAME  | DESCRIPTION |
+-----+-----------------+----+-----------------+-------------+
| +   | postgres-config |    | postgres-config |             |
+-----+-----------------+----+-----------------+-------------+
|                                   TOTAL      |      1      |
+-----+-----------------+----+-----------------+-------------+

LABEL ASSOCIATIONS    +add | -remove | unchanged
+-----+---------------+--------------------------+---------------+------------------+----------------------------+------------+------------------+
| +/- | RESOURCE TYPE |  RESOURCE PACKAGE NAME   | RESOURCE NAME |   RESOURCE ID    |     LABEL PACKAGE NAME     | LABEL NAME |     LABEL ID     |
+-----+---------------+--------------------------+---------------+------------------+----------------------------+------------+------------------+
|     | buckets       | vivid-heisenberg-732003  | postgres      | 05d639b5fdf32000 | flamboyant-dubinsky-332001 | postgres   | 05d639e9629a9000 |
+-----+---------------+--------------------------+---------------+------------------+----------------------------+------------+------------------+
+-----+---------------+--------------------------+---------------+------------------+----------------------------+------------+------------------+
| +   | dashboards    | jovial-montalcini-f32001 | Postgres      |                  | flamboyant-dubinsky-332001 | postgres   | 05d639e9629a9000 |
+-----+---------------+--------------------------+---------------+------------------+----------------------------+------------+------------------+
|                                                                                                                    TOTAL    |        2         |
+-----+---------------+--------------------------+---------------+------------------+----------------------------+------------+------------------+

正如您所看到的,导入过程进行了一些检查并识别了我们的模板——我们的 PostgreSQL InfluxDB 模板已准备就绪!????

准备好构建您自己的 InfluxDB 模板了吗?

我发现构建 InfluxDB 模板非常有趣,我很高兴分享我制作模板的过程。我希望本文对您有所帮助——我迫不及待地想看看社区能够构建什么!

想要为 InfluxDB 模板做贡献吗?请查看这个 GitHub 仓库 并阅读贡献指南。单击 此处 发现其他已经开发的 InfluxDB 模板。

如果您有任何问题,我很乐意提供帮助。在 InfluxDB 社区 Slack 工作区 @Ignacio Van Droogenbroeck 中找到我。务必查看 #Templates 频道——来打个招呼!或者在 Twitter 上告诉我您是否正在使用此仪表板以及如何使用。

附加资源