Telegraf 是一个基于服务器的代理,用于从数据库、系统和 IoT 传感器收集和发送指标和事件。 Telegraf 是一个开源的、插件驱动的工具,用于收集和输出数据。

要查看由 Telegraf 收集并存储在 Timescale Cloud 服务中的超表中的指标。

最佳实践是使用与您的 Timescale Cloud 服务位于同一区域的 Ubuntu EC2 实例 作为迁移机器。 也就是说,您在其上运行命令以将数据从源数据库移动到目标 Timescale Cloud 服务的机器。

在迁移数据之前

  • 创建一个目标 Timescale Cloud 服务

    每个 Timescale Cloud 服务都有一个支持最流行的扩展的单个数据库。 Timescale Cloud 服务不支持表空间,并且没有与服务关联的超级用户。 最佳实践是创建至少具有 8 个 CPU 的 Timescale Cloud 服务,以获得更流畅的体验。 较高规格的实例可以显着缩短整体迁移窗口。

  • 为确保维护不会在过程中运行,请调整维护窗口

要创建将数据导出到您的服务中的超表的 Telegraf 配置

  1. 设置您的服务连接字符串

    此变量保存目标 Timescale Cloud 服务的连接信息。

    在源机器上的终端中,设置以下内容

    export TARGET=postgres://tsdbadmin:<PASSWORD>@<HOST>:<PORT>/tsdb?sslmode=require

    您可以在创建服务时下载的配置文件中找到您的 Timescale Cloud 服务的连接信息。

  2. 生成 Telegraf 配置文件

    在终端中,运行以下命令

    telegraf --input-filter=cpu --output-filter=postgresql config > telegraf.conf

    telegraf.conf 配置了一个 CPU 输入插件,该插件对有关 CPU 使用率的各种指标进行采样,以及 PostgreSQL 输出插件。 telegraf.conf 还包括所有可用的输入、输出、处理器和聚合器插件。 这些默认情况下被注释掉。

  3. 测试配置

    telegraf --config telegraf.conf --test

    您会看到类似于以下的输出

    2022-11-28T12:53:44Z I! Starting Telegraf 1.24.3
    2022-11-28T12:53:44Z I! Available plugins: 208 inputs, 9 aggregators, 26 processors, 20 parsers, 57 outputs
    2022-11-28T12:53:44Z I! Loaded inputs: cpu
    2022-11-28T12:53:44Z I! Loaded aggregators:
    2022-11-28T12:53:44Z I! Loaded processors:
    2022-11-28T12:53:44Z W! Outputs are not used in testing mode!
    2022-11-28T12:53:44Z I! Tags enabled: host=localhost
    > cpu,cpu=cpu0,host=localhost usage_guest=0,usage_guest_nice=0,usage_idle=90.00000000087311,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=6.000000000040018,usage_user=3.999999999996362 1669640025000000000
    > cpu,cpu=cpu1,host=localhost usage_guest=0,usage_guest_nice=0,usage_idle=92.15686274495818,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=5.882352941192206,usage_user=1.9607843136712912 1669640025000000000
    > cpu,cpu=cpu2,host=localhost usage_guest=0,usage_guest_nice=0,usage_idle=91.99999999982538,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=3.999999999996362,usage_user=3.999999999996362 1669640025000000000
  4. 配置 PostgreSQL 输出插件

    1. telegraf.conf 中,在 [[outputs.postgresql]] 部分中,将 connection 设置为 $TARGET 的值。

      connection = "<VALUE OF $TARGET>"
    2. 当 Telegraf 创建新表时使用超表

      在以注释 ## Templated statements to execute when creating a new table 开头的部分中,添加以下模板

      ## Templated statements to execute when creating a new table.
      # create_templates = [
      # '''CREATE TABLE {{ .table }} ({{ .columns }})''',
      # ]
      # table_template=`CREATE TABLE IF NOT EXISTS {TABLE}({COLUMNS}); SELECT create_hypertable({TABLELITERAL},by_range('time', INTERVAL '1 week'),if_not_exists := true);`

      by_range 维度构建器已添加到 TimescaleDB 2.13。

本节向您展示如何使用 Telegraf 生成系统指标,然后连接到您的服务并查询指标超表

  1. 使用 Telegraf 收集系统指标

    运行以下命令 30 秒

    telegraf --config telegraf.conf

    Telegraf 使用加载的输入 cpu 和输出 postgresql 以及 global tags、代理从输入收集数据的时间间隔以及刷新到输出的时间间隔。

  2. 查看指标

    1. 连接到您的 Timescale Cloud 服务

      psql $TARGET
    2. 查看在 tsdbcpu 表中收集的指标

      SELECT*FROM cpu;

      您会看到类似以下内容

      time | cpu | host | usage_guest | usage_guest_nice | usage_idle | usage_iowait | usage_irq | usage_nice | usage_softirq | usage_steal | usage_system | usage_user
      ---------------------+-----------+----------------------------------+-------------+------------------+-------------------+--------------+-----------+------------+---------------+-------------+---------------------+---------------------
      2022-12-05 12:25:20 | cpu0 | hostname | 0 | 0 | 83.08605341237833 | 0 | 0 | 0 | 0 | 0 | 6.824925815961274 | 10.089020771444481
      2022-12-05 12:25:20 | cpu1 | hostname | 0 | 0 | 84.27299703278959 | 0 | 0 | 0 | 0 | 0 | 5.934718100814769 | 9.792284866395647
      2022-12-05 12:25:20 | cpu2 | hostname | 0 | 0 | 87.53709198848934 | 0 | 0 | 0 | 0 | 0 | 4.747774480755411 | 7.715133531241037
      2022-12-05 12:25:20 | cpu3 | hostname| 0 | 0 | 86.68639053296472 | 0 | 0 | 0 | 0 | 0 | 4.43786982253345 | 8.875739645039992
      2022-12-05 12:25:20 | cpu4 | hostname | 0 | 0 | 96.15384615371369 | 0 | 0 | 0 | 0 | 0 | 1.1834319526667423 | 2.6627218934917614

      要查看每个 CPU 核心的平均使用率,请使用 SELECT cpu, avg(usage_user) FROM cpu GROUP BY cpu;

有关您可以在 Telegraf 中配置的选项的更多信息,请参阅PostgreSQL 输出插件

关键词

在此页面上发现问题?报告问题 或 在 GitHub 上编辑此页面