使用 Telegraf 和 InfluxDB 监控 PostgreSQL 数据库
作者:Margo Schaedel / 产品, 用例, 开发者
2018 年 7 月 20 日
导航至
概述
本教程将专门介绍如何设置 Telegraf 和 InfluxDB 来监控 PostgreSQL。对于该领域的新手,PostgreSQL (或简称 Postgres) 是一个非常流行的开源对象关系数据库系统,最初由加州大学伯克利分校的开发人员于 1986 年率先开发。它具有多版本并发控制和预写式日志等重要功能,有助于确保数据可靠性。如果您不太熟悉 PostgreSQL,我建议从他们的 初学者教程 开始。
PostgreSQL 的创建者认识到跟踪和监控数据库性能和吞吐量的重要性,添加了一个 统计信息收集器,该收集器自动收集关于其自身数据库活动的信息。您基本上拥有所有这些开箱即用的出色指标。因此,让我们充分利用这一点,将所有这些指标暴露给 Telegraf,并将它们发送到 InfluxDB。
您需要准备什么
在本教程中,我使用的是 InfluxDB、Telegraf 和 Chronograf 的本地安装;这些项目的“入门”指南非常棒且易于理解。您还需要在您的机器上安装 PostgreSQL,如果您碰巧没有任何示例应用程序和数据库,您可以 fork/clone 这个 repo 来跟随学习——这只是一个小型 Node/Express 应用程序,用于在 PostgreSQL 中存储调色板——请务必按照 README 了解如何使应用程序正常工作。
编辑您的 Telegraf 配置文件
首先,Telegraf GitHub 页面 提供了许多输入和输出插件,以适应各种用例——其中一个包括 PostgreSQL 输入插件。如果我们正确地在 Telegraf 配置文件中配置此插件,我们应该会自动开始看到指标被发送到 InfluxDB 中的默认 telegraf.autogen
数据库。
让我们试一下。
导航到您的 Telegraf 配置文件并找到 [[inputs.postgresql]]
部分。如果您使用的是 Mac OS 并使用 Homebrew 安装 InfluxDB 和 Telegraf,则路径 /usr/local/etc/telegraf.conf
应该可以引导您找到默认配置文件。否则,请参阅 Telegraf 文档 以获取进一步参考。
# # Read metrics from one or many postgresql servers
# [[inputs.postgresql]]
# ## specify address via a url matching:
# ## postgres://[pqgotest[:password]]@localhost[/dbname]\
# ## ?sslmode=[disable|verify-ca|verify-full]
# ## or a simple string:
# ## host=localhost user=pqotest password=... sslmode=... dbname=app_production
# ##
# ## All connection parameters are optional.
# ##
# ## Without the dbname parameter, the driver will default to a database
# ## with the same name as the user. This dbname is just for instantiating a
# ## connection with the server and doesn't restrict the databases we are trying
# ## to grab metrics for.
# ##
# address = "host=localhost user=postgres sslmode=disable"
# ## A custom name for the database that will be used as the "server" tag in the
# ## measurement output. If not specified, a default one generated from
# ## the connection address is used.
# # outputaddress = "db01"
#
# ## connection configuration.
# ## maxlifetime - specify the maximum lifetime of a connection.
# ## default is forever (0s)
# max_lifetime = "0s"
#
# ## A list of databases to explicitly ignore. If not specified, metrics for all
# ## databases are gathered. Do NOT use with the 'databases' option.
# # ignored_databases = ["postgres", "template0", "template1"]
#
# ## A list of databases to pull metrics about. If not specified, metrics for all
# ## databases are gathered. Do NOT use with the 'ignored_databases' option.
# # databases = ["app_production", "testing"]
这是配置文件的开箱即用外观。正如您所见,要遵循的说明非常简单。您肯定需要指定要连接的地址,以便 Telegraf 可以与您的 PostgreSQL 服务器通信。您可以选择性地指定其他参数,例如 username
、password
、启用或禁用 ssl-mode
,以及连接到特定数据库(如果您希望这样做)。
如果您想为 InfluxDB 数据库中的服务器标签创建自定义名称,您可以在 outputaddress
中指定它。连接生命周期决定了您希望连接保持打开状态的持续时间。最后,您可以列出要忽略或专门收集这些列表中数据库指标的数据库数组。对于此选项,您只能执行其中一项,而不能同时执行两项。
此插件可以轻松地从 PostgreSQL 中已内置的 pg_stat_database
和 pg_stat_bgwriter
视图中提取指标。查看 文档 以准确查看提取了哪些指标。让我们将地址值更改为将我们的主机列为 localhost 的字符串,如下所示
address = "host=localhost"
唯一需要确保的另一件事是,您的数据输出将发送到 InfluxDB。如果您向下滚动到 outputs.influxdb
部分,您可以编辑 url 以包含 InfluxDB 的默认端口 8086
# Configuration for influxdb server to send metrics to
[[outputs.influxdb]]
## The full HTTP or UDP URL for your InfluxDB instance.
##
## Multiple urls can be specified as part of the same cluster,
## this means that only ONE of the urls will be written to each interval.
# urls = ["udp://localhost:8089"] # UDP endpoint example
urls = ["http://localhost:8086"] # required
## The target database for metrics (telegraf will create it if not exists).
database = "telegraf" # required
## Name of existing retention policy to write to. Empty string writes to
## the default retention policy.
retention_policy = ""
## Write consistency (clusters only), can be: "any", "one", "quorum", "all"
write_consistency = "any"
## Write timeout (for the InfluxDB client), formatted as a string.
## If not provided, will default to 5s. 0s means no timeout (not recommended).
timeout = "5s"
# username = "telegraf"
# password = "metricsmetricsmetricsmetrics"
## Set the user agent for HTTP POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
## Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
# udp_payload = 512
## Optional SSL Config
# ssl_ca = "/etc/telegraf/ca.pem"
# ssl_cert = "/etc/telegraf/cert.pem"
# ssl_key = "/etc/telegraf/key.pem"
## Use SSL but skip chain & host verification
# insecure_skip_verify = false
## HTTP Proxy Config
# http_proxy = "http://corporate.proxy:3128"
## Optional HTTP headers
# http_headers = {"X-Special-Header" = "Special-Value"}
## Compress each HTTP request payload using GZIP.
# content_encoding = "gzip"
重启 Telegraf 和 Chronograf,导航到 Chronograf 的默认端口 (8888),在菜单的数据浏览器部分,您应该会在默认 telegraf.autogen
数据库下看到一个名为 postgresql
的 measurement。您还应该在字段列中看到大量指标,包括 blk_read_time
、blk_write_time
、buffers_clean
、datid
、deadlocks
、tup_inserted
和 tup_deleted
,仅举几例。要了解每个字段的确切含义,请查看 此参考页面。
或者,您可以使用 CLI 从 InfluxDB 查询数据。在您的终端中,键入 influx
以访问 Influx shell。命令 SHOW DATABASES
将为您列出数据库,USE [databasename]
,然后 SHOW MEASUREMENTS
将列出与该特定数据库关联的 measurement 名称。然后,您可以运行各种查询语句,例如
SELECT mean("xact_commit") AS "mean_xact_commit" FROM "telegraf"."autogen"."postgresql" WHERE time > now() - 5m AND "db"='palette_picker'
或
SELECT * FROM "telegraf"."autogen"."postgresql" WHERE time > now() - 1m AND "db"='palette_picker'
试用一下,亲眼看看!如果您查询过多并且需要在任何时候终止查询,只需运行 KILL QUERY [qid]
,这可以使用 SHOW QUERIES
命令找到。
在生产环境中监控 PostgreSQL
如果您想在生产环境中密切关注您的 PostgreSQL 数据库,这很容易。只需使用正确的地址信息更新 telegraf 配置文件。我已更新了下面的 telegraf 配置文件中的地址,以从我的 Heroku 实例监控此相同的示例应用程序 (Palette Picker) 中的 Postgresql。我能够在我的 Heroku 仪表板页面上找到所有这些凭据。看看这个
address = "host=ec2-204-236-239-225.compute-1.amazonaws.com user=username password=password dbname=databasename"
(为了安全起见,此处已更改用户名、密码和 dbname)
非常简单,对吧?
下一步
希望本指南已帮助您了解使用 Telegraf 和 InfluxDB 监控 PostgreSQL 数据库是多么容易。在下一篇文章中,我们将讨论在评估 Postgres 数据库的健康状况时需要关注的一些关键指标。如有任何问题或意见,请随时通过 Twitter @influxDB 和 @mschae16 与我们联系!