前言

Telegraf 是一个用 Go 编写的用于收集机器数据的代理程序。它可部署到机器上收集系统和服务的统计数据,并通过配置写入到 InfluxDB 数据库,然后在通过Grafana或者Chrongraf用于数据展示。

核心功能

  • 完全由GO编写,编译产物仅为一个二进制程序,安装运行无需外部依赖

  • 运行时内存占用很小

  • 对于当前大部分流行的服务来说,telegraf已经有现成的插件支持

  • 插件系统可以支持对其他服务轻松扩展

安装&配置

1. MacOs(默认安装最新版本telegraf)

brew update
brew install telegraf

2. Ubuntu & Debian

wget https://dl.influxdata.com/telegraf/releases/telegraf_1.15.2-1_amd64.deb sudo dpkg -i telegraf_1.15.2-1_amd64.deb

3. RedHat & CentOS

wget https://dl.influxdata.com/telegraf/releases/telegraf-1.15.2-1.x86_64.rpm sudo yum localinstall telegraf-1.15.2-1.x86_64.rpm

4. Windows Binaries (64-bit)

wget https://dl.influxdata.com/telegraf/releases/telegraf-1.15.2_windows_amd64.zip unzip telegraf-1.15.2_windows_amd64.zip

5. 二进制方式

1)Linux Binaries (64-bit)

wget https://dl.influxdata.com/telegraf/releases/telegraf-1.15.2_linux_amd64.tar.gz tar xf telegraf-1.15.2_linux_amd64.tar.gz

2)Linux Binaries (32-bit)

wget https://dl.influxdata.com/telegraf/releases/telegraf-1.15.2_linux_i386.tar.gz tar xf telegraf-1.15.2_linux_i386.tar.gz

配置文件地址

MacOS: /usr/local/etc/telegraf.conf

Linux debian and RPM package: /etc/telegraf/telegraf.conf

如果你是二进制包的安装方式,可以生成一个telegraf.conf

#配置生成一个telegraf.conf,该配置收集系统的cpu和内存信息,然后输出到influxdb
telegraf -sample-config -input-filter cpu:mem -output-filter influxdb > telegraf.conf

配置文件内容


[[outputs.influxdb]]

urls = ["http://localhost:8086"]

#influxdb中的数据库名,不存在则自动创建

database = "telegraf"

#写influxDB超时时间

timeout = "5s"

# 收集cpu的配置

[[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


# 收集disk的信息

[[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 = ["/data"]


## Ignore mount points by filesystem type.

ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]



# 收集硬盘的io

[[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", "vd*"]

## 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"]

# 收集内存信息

[[inputs.mem]]

interval = "10m"


# 收集进程状态

[[inputs.processes]]

interval = "10m"

fielddrop = ["wait","idle","unknown"]


# 收集swap分区信息

[[inputs.swap]]

interval = "1h"

fieldpass = ["used_percent"]


# 收集系统运行信息

[[inputs.system]]

## Uncomment to remove deprecated metrics.

interval = "2m"

fielddrop = ["uptime_format"]

启动方式

1. MacOS启动方式

brew services start telegraf

2. Linux debian and RPM package

sudo systemctl start telegraf 或者 sudo service telegraf start

3.二进制启动方式

nohup telegraf -config /usr/local/etc/telegraf.conf &

启动后,查看日志没有报错即可


博主:测试生财

座右铭:专注测试与自动化,致力提高研发效能;通过测试精进完成原始积累,通过读书理财奔向财务自由。

csdn:https://blog.csdn.net/ccgshigao

博客园:https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374