TrueNas/telegraf

From r00tedvw.com wiki
Jump to: navigation, search

Contents

Overview

reference: https://blog.victormendonca.com/2020/10/28/how-to-install-telegraf-on-freenas/
Setup and configure telegraf for TrueNAS (and probably any other FreeBSD OS)

Dependencies

Download and extract telegraf tar.gz

Find the latest telegraf release here
https://github.com/influxdata/telegraf/releases
Download and extract it on a vm with GNUtar (ubuntu, centos, etc) as tar included with FreeBSD will not unpack it.

Copy files over

You only need (2) files, the binary and conf file. Copy them over to TrueNAS.

~$ ./telegraf-[version]/usr/bin/telegraf
./telegraf-[version]/etc/telegraf/telegraf.conf

TrueNAS telegraf setup

Create dir and copy files

Create the directory your files will live in. I chose /usr/local/telegraf-[version]_freebsd_amd64 because /usr/local/ is the closest thing to /opt/ in freebsd

~$ sudo mkdir /usr/local/telegraf-[version]_freebsd_amd64
~$ cp /tmp/telegraf* /usr/local/telegraf-[version]_freebsd_amd64

Modify telegraf.conf

update the conf file to use prometheus instead of influxdb

~$ vim /tmp/telegraf-1.23.4_freebsd_amd64/etc/telegraf/telegraf.conf

comment

# [[outputs.influxdb]]

uncomment

[[outputs.prometheus_client]]
  listen = ":9100"
  path = "/metrics"

Create the init script

Modify the telegraf conf and bin dir vars.

~$ sudo vim /usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.init
#!/bin/sh
# $FreeBSD$

# PROVIDE: telegraf
# REQUIRE: DAEMON NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf to enable telegrafb:
# telegraf_enable="YES"
#
# telegraf_enable (bool): Set to YES to enable telegraf
# Default: NO
# telegraf_conf (str): telegraf configuration file
# Default: ${PREFIX}/etc/telegraf.conf
# telegraf_flags (str): Extra flags passed to telegraf

. /etc/rc.subr

name="telegraf"
rcvar=telegraf_enable

load_rc_config "${name}"
: ${telegraf_enable:="YES"}
: ${telegraf_flags:="-quiet"}
: ${telegraf_conf:="/usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.conf"}
: ${telegraf_bindir:="/usr/local/telegraf-1.23.4_freebsd_amd64"}

pidfile="/var/run/${name}.pid"
command=/usr/sbin/daemon
command_args="-crP ${pidfile} ${telegraf_bindir}/${name} ${telegraf_flags} -config=${telegraf_conf}"

telegraf_prestart() {
# Have to empty rc_flags so they don't get passed to daemon(8)
  touch $pidfile
  rc_flags=""
}

telegraf_start() {
  ${command} ${command_args} ${rc_arg} >> /var/log/telegraf.log 2>&1
}

start_precmd="${name}_prestart"
start_cmd="${name}_start"

run_rc_command "$1"

Final steps

make the init executable

~$ chmod +x /usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.init

create a symlink for telegraf.init

~$ ln -s /usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.init /usr/local/etc/rc.d/telegraf

test the service

~$ service telegraf start

Add the following lines to /etc/rc.conf to enable telegrafb:

telegraf_enable="YES"

Create post init startup task

In the GUI go to:
Tasks > Init/Shutdown Scripts
and create a Command type with the following:

ln -s /usr/local/telegraf-1.23.4_freebsd_amd64/telegraf.init /usr/local/etc/rc.d/telegraf; service telegraf start

Select Post Init and Enable it.

Personal tools
Namespaces

Variants
Actions
Navigation
Mediawiki
Confluence
DevOps Tools
Ubuntu
Ubuntu 22
Mac OSX
Oracle Linux
AWS
Windows
OpenVPN
Grafana
InfluxDB2
TrueNas
OwnCloud
Pivotal
osTicket
OTRS
phpBB
WordPress
VmWare ESXI 5.1
Crypto currencies
HTML
CSS
Python
Java Script
PHP
Raspberry Pi
Canvas LMS
Kaltura Media Server
Plex Media Server
MetaSploit
Zoneminder
ShinobiCE
Photoshop CS2
Fortinet
Uploaded
Certifications
General Info
Games
Meal Plans
NC Statutes
2020 Election
Volkswagen
Covid
NCDMV
Toolbox