一、安装准备

1、系统要求
最低要求
CPU 支持 64 位的 Intel® 或 AMD 处理器*;2 GHz 或速度更快的处理器
内存 2 GB 或更大 RAM(推荐使用 8 GB)
硬盘 需要 50GB 以上的可用磁盘空间
操作系统 CentOS 7 或更高版本
2、软件要求
  • Telegraf v1.25.0

三、服务安装

Ubuntu & Debian 使用 “apt-get” 安装
# influxdata-archive_compat.key GPG Fingerprint: 9D539D90D3328DC7D6C8D3B9D8FF8E1F7DF8B07E
wget -q https://repos.influxdata.com/influxdata-archive_compat.key
echo '393e8779c89ac8d958f81f942f9ad7fb82a25e133faddaf92e15b16e6ac9ce4c influxdata-archive_compat.key' | sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | sudo tee /etc/apt/sources.list.d/influxdata.list

然后,安装并启动 telegraf 服务:

sudo apt-get update && sudo apt-get install telegraf
Red Hat && CentOS 使用 “yum” 安装

RedHat 和 CentOS:yum使用包管理器安装最新的稳定版 Telegraf :

cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxData Repository - Stable
baseurl = https://repos.influxdata.com/stable/\$basearch/main
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive_compat.key
EOF

将存储库添加到yum配置后,通过运行以下命令安装并启动 Telegraf 服务:

sudo yum install telegraf
sudo service telegraf start

或者,如果您的操作系统使用的是 systemd(CentOS 7+、RHEL 7+):

sudo yum install telegraf
sudo systemctl start telegraf
sudo systemctl enable telegraf
sudo systemctl restart telegraf
sudo systemctl status telegraf

查看版本

[root@localhost local]# telegraf -version
Telegraf 1.25.1 (git: HEAD@e1a0d74e)

执行 telegraf 启动报错

$ telegraf
2023-02-09T09:06:53Z I! Using config file: /etc/telegraf/telegraf.conf
2023-02-09T09:06:53Z I! Using config file: /etc/telegraf/telegraf.conf
2023-02-09T09:06:53Z E! [telegraf] Error running agent: no outputs found, did you provide a valid config file?

需要修改配置文件

SLES & openSUSE 使用 “zypper” 安装

openSUSE Build Service 为 SUSE Linux 用户提供了 RPM 包:

# add go repository
zypper ar -f obs://devel:languages:go/ go
# install latest telegraf
zypper in telegraf
FreeBSD/PC-BSD 使用 “pkg” 安装

Telegraf 是 FreeBSD 软件包系统的一部分。它可以通过运行来安装:

sudo pkg install telegraf

配置文件位于 /usr/local/etc/telegraf.conf,基于 /usr/local/etc/telegraf.conf.sample 示例。

使用 Linux Binaries 方式安装
1、选择与你linux系统架构匹配的版本:
  • Linux Binaries (64-bit)
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.25.0_linux_amd64.tar.gz
tar xf telegraf-1.25.0_linux_amd64.tar.gz
  • Linux Binaries (64-bit, static)
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.25.0_static_linux_amd64.tar.gz
tar xf telegraf-1.25.0_static_linux_amd64.tar.gz
  • Linux Binaries (32-bit)
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.25.0_linux_i386.tar.gz
tar xf telegraf-1.25.0_linux_i386.tar.gz
  • Linux Binaries (ARMv8)
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.25.0_linux_arm64.tar.gz
tar xf telegraf-1.25.0_linux_arm64.tar.gz
  • Linux Binaries (ARMv7)
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.25.0_linux_armhf.tar.gz
tar xf telegraf-1.25.0_linux_armhf.tar.gz
2、以 Linux Binaries (64-bit) 为例,下载并安装 Telegraf (下载在线安装中的地址对应安装包)

注意:可先网页下载到本地,上传到 /usr/local/src 目录下,也可以使用 wget 下载;

# 在线安装包
$ cd /usr/local/src && wget https://dl.influxdata.com/telegraf/releases/telegraf-1.25.0_linux_amd64.tar.gz --no-check-certificate
# 本地安装包
$ cd /usr/local/src && tar -zxzf telegraf-1.25.0_linux_amd64.tar.gz -C /usr/local

$ cd .. && mv telegraf-1.25.0 telegraf
$ echo 'export TELEGRAF_HOME=/usr/local/telegraf' >>/etc/profile
$ echo 'export PATH="$PATH:$TELEGRAF_HOME/bin"' >>/etc/profile
$ source /etc/profile

开机自启动

[root@local ~]# vi /lib/systemd/system/telegraf.service

脚本内容:

[Unit]
Description=The plugin-driven server agent for reporting metrics into InfluxDB
Documentation=https://github.com/influxdata/telegraf
After=network.target

[Service]
#EnvironmentFile=-/etc/default/telegraf
User=root
ExecStart=/usr/bin/telegraf -config /etc/telegraf/telegraf.conf -config-directory /etc/telegraf/telegraf.d
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
LimitNOFILE=65536
TimeoutStopSec=20
Restart=on-failure
StartLimitInterval=60
StartLimitBurst=3000

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@local ~]# systemctl daemon-reload
[root@local ~]# systemctl disable telegraf.service
[root@local ~]# systemctl enable telegraf.service
[root@local ~]# systemctl start telegraf.service
[root@local ~]# systemctl status telegraf.service
[root@local ~]# systemctl stop telegraf.service
[root@local ~]# systemctl restart telegraf.service

Telegraf 配置

1、Linux命令修改配置或直接使用SSH工具下载配置,修改后上传

命令:vi /etc/telegraf/telegraf.conf

如图位置,先备份,然后下载修改,覆盖上传:

修改telegraf配置文件添加监控项

#修改监控数据输出位置,本次测试环境本地安装有influxdb, 可以保持默认:urls = ["http://localhost:8086"]
[[outputs.influxdb]]
## The full HTTP or UDP endpoint 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://172.16.7.12:8086"] # required
## The target database for metrics (telegraf will create it if not exists).

参考资料

https://github.com/influxdata/telegraf/blob/master/docs/WINDOWS_SERVICE.md
http://michaelkang.blog.51cto.com/1553154/1759864

作者:Jeebiz  创建时间:2019-10-20 22:08
最后编辑:Jeebiz  更新时间:2024-02-26 11:18