centos7.9 安装telegraf

下载解压软件

# 系统环境: centos7.9
# 软件名:telegraf-1.28.2_linux_amd64.tar.gz

useradd -M -s /sbin/nologin telegraf
cd /opt
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.28.2_linux_amd64.tar.gz
tar -zxvf telegraf-1.28.2_linux_amd64.tar.gz
ln -s /opt/telegraf-1.28.2 /opt/telegraf
ln -s /opt/telegraf/usr/bin/telegraf /usr/bin/

生成配置文件

# 备份原始配置文件
mv /opt/telegraf/etc/telegraf/telegraf.conf /opt/telegraf/etc/telegraf/telegraf.conf.bak

# 生成自定义配置文件
# 开启input插件:cpu:disk:diskio:kernel:mem:net:processes:swap:system
# 开启output插件:prometheus_client
telegraf --input-filter cpu:disk:diskio:kernel:mem:net:processes:swap:system --output-filter prometheus_client config | grep -Ev '#|^$' > /opt/telegraf/etc/telegraf/telegraf.conf

修改为最终配置文件

cat /opt/telegraf/etc/telegraf/telegraf.conf
[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = "0s"
  hostname = ""
  omit_hostname = false
[[inputs.cpu]]
  percpu = true
  totalcpu = true
  collect_cpu_time = false
  report_active = false
  core_tags = false
[[inputs.disk]]
  # 设置不收及指标数据的文件系统
  ignore_fs = ["tmpfs", "devtmpfs", "devfs", "iso9660", "overlay", "aufs", "squashfs"]
[[inputs.diskio]]
[[inputs.kernel]]
[[inputs.mem]]
[[inputs.processes]]
[[inputs.swap]]
[[inputs.system]]
[[inputs.net]]
  ignore_protocol_stats = true
#[[inputs.nstat]]
#  proc_net_netstat = "/proc/net/netstat"
#  proc_net_snmp = "/proc/net/snmp"
#  proc_net_snmp6 = "/proc/net/snmp6"
#  dump_zeros       = true


[[outputs.prometheus_client]]
  listen = ":9273"
  # 排除插件自身的指标数据
  collectors_exclude = ["gocollector", "process"]

设置开机自启

cat /etc/systemd/system/telegraf.service
[Unit]
Description=Telegraf
Documentation=https://github.com/influxdata/telegraf
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
#EnvironmentFile=-/etc/default/telegraf
WorkingDirectory=/opt/telegraf
User=telegraf
ExecStart=/usr/bin/telegraf -config /opt/telegraf/etc/telegraf/telegraf.conf -config-directory /opt/telegraf/etc/telegraf/telegraf.d
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=mixed
TimeoutStopSec=5
LimitMEMLOCK=8M:8M

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl enable telegraf
systemctl start telegraf

自定义日志文件路径

cat /etc/rsyslog.d/telegraf.conf 
### telegraf Rules ###
if $programname == 'telegraf' then {
   action(type="omfile" file="/var/log/telegraf/telegraf.log")
   stop
}

# 创建日志目录
mkdir /var/log/telegraf/
systemctl restart rsyslog.service
systemctl restart telegraf

你可能感兴趣的:(shell,监控,运维,数据库,服务器)