Telegraf监控介绍

Telegraf是一个开源的代理程序,用于在不同的服务之间传输指标数据。它支持很多不同的数据源,并提供了很多插件,需要什么样的数据,就选择相应的插件,非常方便易用。本文将从以下几个方面介绍Telegraf监控,帮助大家更好地利用Telegraf监控。
一、Telegraf被监控
当你把Telegraf部署到你的系统时,它会逐步地监视整个系统。Telegraf支持多种输入插件来从各种数据源,比如系统,网络设备和Web服务中采集数据。此外,Telegraf还支持灵活的输出插件,可以把采集到的指标数据发送到不同的位置,比如InfluxDB、Kafka和Elasticsearch等。
下面是一个监控本地CPU和内存的Telegraf示例:
[[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
## Collect CPU time metrics for each individual CPU if collected.
report_active = false
[[inputs.mem]]
## Whether to report memory usage metrics or not
## Deprecated in 1.4; use: fielddrop or fieldpass to filter metrics.
report_memory_metrics = true
## no configuration
这个配置文件定义了两个输入插件,CPU和内存,Telegraf会定期获取本地的CPU和内存使用状态数据,以供进一步处理。
二、Telegraf监控WMI数据源
Windows 管理工具命名空间用于访问多种计算机资源,如进程、服务、网络性能以及操作系统的设置和状态。Windows 管理工具提供了一种简单的方式,通过 WMI 数据源,采集这些信息。Telegraf提供了一个 WMI 插件,用于将 Windows 管理工具的数据作为资源来管理。
下面是一个监控本地 Windows 系统信息的Telegraf示例:
[[inputs.wmi]]
# Print debug information about DCOM connection.
# debug = false
## Gather metrics from the Win32_OperatingSystem table.
[[inputs.wmi.field]]
measurement = "win_os"
table = "Win32_OperatingSystem"
tagexclude = ["Namespace"]
tagexcludevalue = ["MSFT_WmiSelfEvent"]
## Gather metrics from the Win32_ComputerSystem table.
[[inputs.wmi.field]]
measurement = "win_cpu"
table = "Win32_Processor"
tagexclude = ["Name", "SocketDesignation", "Version"]
tagexcludevalue = ["CPU à processeur x86"]
这个配置文件定义了 WMI 插件的两个字段,指定了 Telegraf 从 Win32_OperatingSystem 和 Win32_ComputerSystem 表中收集操作系统和 CPU 信息。
三、Telegraf监控SNMP数据源
SNMP (Simple Network Management Protocol)是一个基于Internet的协议,用于网络管理。SNMP协议主要用于网络设备监控方面,可以支持网络拓扑图、网络负载等功能的实现。Telegraf提供了一个 SNMP 插件,可以监视和获取网络设备的指标
下面是一个监控路由器负载的 Telegraf 示例:
[[inputs.snmp]]
agents = [ "my.router.example.com" ]
## SNMP version; can be 1, 2, or 3.
version = 2
## community string for v1 & v2
community = "public"
## v3 auth parameters
# security_name = "telegraf"
## security level can be either noAuthNoPriv|authNoPriv|authPriv
# security_level = "authNoPriv"
# auth_protocol = "md5"
# auth_password = "password"
# priv_protocol = "des"
# priv_password = "password"
## timeout applies to the amount of time waiting for the device to respond
timeout = "5s"
## retries configures the number of times an SNMP query is retried
retries = 3
[[inputs.snmp.field]]
is_tag = true # mark this OID as a tag
oid = "IF-MIB::ifDescr"
[[inputs.snmp.field]]
is_tag = true # mark this OID as a tag
oid = "IF-MIB::ifAlias"
[[inputs.snmp.field]]
is_tag = true # mark this OID as a tag
oid = "IF-MIB::ifType"
[[inputs.snmp.field]]
is_tag = true # mark this OID as a tag
oid = "IF-MIB::ifOperStatus"
[[inputs.snmp.field]]
is_tag = true # mark this OID as a tag
oid = "IF-MIB::ifAdminStatus"
[[inputs.snmp.field]]
oid = "IF-MIB::ifInOctets"
[[inputs.snmp.field]]
oid = "IF-MIB::ifOutOctets"
这个配置文件定义了 SNMP 插件的6个字段,指定了 Telegraf 从 IF-MIB 表中收集网络接口指标。
四、Telegraf监控Docker数据源
Docker 让应用程序便携性更好、更快,并在不同的环境中更容易部署。Telegraf提供了一个 Docker 插件,它可以让用户监视和收集有关容器以及它们所运行的应用程序的详细信息。
下面是一个监控本地 Docker 容器的 Telegraf 示例:
[[inputs.docker]]
## Whether to gather metrics about containers. Default is true.
container_stats = true
## Whether to gather metrics about Images. Default is true.
image_stats = true
## Timeout for Docker API requests. Default is no timeout.
timeout = "5s"
## endpoint for the Dockerd daemon. Default is "unix:///var/run/docker.sock".
endpoint = "unix:///var/run/docker.sock"
## Whether to report per-cpu stats or not
perdevice = true
## Whether to report containers status or not
container_status = true
## Whether to report image metrics or not
image_size = true
## Whether to compute the in and out network stats. Default is false.
# collect_network = false
# Whether to compute per-core CPU usage on Linux. Default is true.
# cpu_per_cpu = true
这个配置文件定义了 Docker 插件的7个字段,指定了 Telegraf 从 Docker 中收集容器和镜像的指标。
五、Telegraf监控收集到的数据的处理
与 Telegraf 捕获相应数据源数据并将其发送到 InfluxDB 中的其他组件相比,Telegraf 中的处理逻辑要简单得多。
当 Telegraf 捕获指标时,它会按照以下流程进行处理:
根据其定义的标记和字段与测量相关联,将新数据添加到流中。 通过执行任何定义的过滤器来转换新数据。 将新的标记、字段和值更新到流中。 通过执行任何定义的输出插件,将新数据发送到您指定的目标位置。使用 filter 插件,可以对输入的数据流进行更高级的操作,例如对标记进行重写,以便在输出之后更轻松地查询。
下面是一个把 Telegraf 采集的日志发送给 syslog 的 Telegraf 示例:
[[outputs.syslog]]
## Use the following code to specify the syslog server. The preset for Facility is local0.
## The preset for Severity is notice.
## The default level for Wireshark is info.
server = "tcp://syslog-server:514"
facility = "daemon"
severity = "info"
[[processors.regex]]
## General tag/field matching:
## Field corresponding to substitution
fields = ["message"]
## Regular expression to match against the field
## Must compile to a valid regex with optional usage of ?P for named capturing
regex = '^(?P[0-9]{1,2})%$'
## Use of ?P<> syntax to name fields is allowed and encouraged
replacement = "${1}"
这个配置文件定义了 Syslog 输出插件和一个正则表达式过滤器,指定了 Telegraf 将日志发送到 Syslog 服务器,并对日志信息中的CPU百分比数据进行过滤。