Running Telegraf on OpenBSD

       398 words, 2 minutes

I tried to run InfluxData Telegraf on OpenBSD 6.2 but it wasn’t available in the Ports nor was I able to compile a binary from sources. But the latter has changed since I have an OpenBSD 6.3 instance running. Here’s how to compile and run Telegraf on OpenBSD.

Get an OpenBSD 6.3/amd64. It may run on other architectures and versions.

Install the required dev tools:

# pkg_add gmake git go
(...)
gmake-4.2.1: ok
git-2.16.2: ok
go-1.10: ok

Setup the compile environnement:

# export GOPATH=~/go
# export PATH=$PATH:~/go/bin

Start building the Go dependency management tool:

# go get github.com/golang/dep
# cd $GOPATH/src/github.com/golang/dep
# go install ./...

Then build Telegraf itself:

# go get github.com/influxdata/telegraf
# cd $GOPATH/src/github.com/influxdata/telegraf
# gmake
dep ensure -vendor-only
go build -ldflags " -X main.commit=c7e2945a -X main.branch=master" ./cmd/telegraf

This will compile the master branch. One should be able to compile a stable branch using checkout and tags stuff from Git. I couldn’t, trying to build 1.7.4.

When the binary’s ready, simply install it:

# gmake install
go build -ldflags " -X main.commit=c7e2945a -X main.branch=master" ./cmd/telegraf
mkdir -p /usr/local/bin/
cp telegraf /usr/local/bin/

I’m using User and Class features of OpenBSD so that Telegraf doesn’t run as root:

# vim /etc/login.conf
(...)
telegraf:\
  :tc=daemon:

# cap_mkdb /etc/login.conf
# groupadd -g xxxx _telegraf
# useradd -u xxxx -g xxxx -c "Telegraf agent" -d /var/telegraf -s /sbin/nologin -L telegraf _telegraf

The default configuration file will go in “/etc”:

# mkdir /etc/telegraf
# chown _telegraf:_telegraf /etc/telegraf
# chmod 0750 /etc/telegraf
# doas -u _telegraf telegraf config > /etc/telegraf/telegraf.conf

Finally, create an rc.d script (inspired by sysutils/prometheus) for easy management:

# cat > /etc/rc.d/telegraf
#!/bin/ksh
#
# Start InfluxData Telegraf agent

daemon="/usr/local/bin/telegraf"
daemon_flags="--config /etc/telegraf/telegraf.conf"
daemon_user="_telegraf"

. /etc/rc.d/rc.subr

pexp="${daemon}.*"
rc_bg=YES
rc_reload=NO

rc_pre() {
    /usr/bin/install -d -o _telegraf -g _telegraf -m 0750 /var/telegraf
}

rc_start() {
    ${rcexec} "${daemon} ${daemon_flags} < /dev/null 2>&1 | \
        logger -p daemon.info -t telegraf"
}

rc_cmd $1
#EOF

# chmod 0755 /etc/rc.d/telegraf
# chown root:wheel /etc/rc.d/telegraf

# rcctl enable telegraf
# rcctl start telegraf

So far, Telegraf runs the Apache, MySQL, PHP-FPM and SNMP input filters and the InfluxDB output filter. And it doesn’t seem to use more resources than Collectd.

Some plugins will expect the sudo command to exist. To be able to use those, I fooled them by creating an alias that would call doas(1). Seems to work so far.

# cat /var/telegraf/.profile
#!/bin/sh

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

alias sudo="/usr/bin/doas"