Telegraf monitoring

Mouftik

Dabbler
Joined
May 12, 2014
Messages
41
Hi all,

I am currently trying to get telegraph working on my FreeNAS but I get a little bit upset. I configured it well and every metric I want came fine (CPU, disks, du, net, ...) but when I want to use the inputs.exec to handle my UPS info, things get annoying.

The binary is installed by hand in /opt/telegraf because the telegraf pkg is not available. With this binary is the config file, an init file and the script I want to use.
The init script looks like (Taken from a reddit thread) :
!/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:="/opt/telegraf/${name}.conf"}

# daemon
start_precmd=telegraf_prestart
pidfile="/var/run/${name}.pid"
command=/usr/sbin/daemon
command_args="-crP ${pidfile} /opt/telegraf/${name} ${telegraf_flags} -config=${telegraf_conf} >> /var/log/telegraf.log 2>&1"

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

run_rc_command "$1"

And I have a Shutdown/Init script in the UI like :
ln -s /opt/telegraf/telegraf.init /usr/local/etc/rc.d/telegraf; service telegraf start

Lastly the script I run for gathering metrics :
#!/bin/bash
#
# Query NUT UPS status and output in InfluxDB Line Protocol
#
# Usage: ups.sh upsname[@hostname[:port]]
# (same as `upsc`, see https://networkupstools.org/docs/man/upsc.html)

# Sample telegraf.conf config snippet.
#[[inputs.exec]]
# commands = [
# "/usr/local/bin/nut-telegraf.sh ups@127.0.0.1"
# ]

set -euo pipefail
IFS=$'\n\t'

function join { local IFS="$1"; shift; echo "$*"; }

declare -A vars=();

# read UPS status into `vars`
while IFS=": " read -r k v; do
vars[$k]="$v"
done < <(bash -c "upsc $* 2>/dev/null")

# exit if empty/unsuccessful
[[ -z "${vars-}" ]] || exit 1

# print a metric in InfluxDB line format
tags=(
ups=${1%%@*}
)
fields=(
status=\"${vars["ups.status"]}\"
load=${vars["ups.load"]}
battery_charge=${vars["battery.charge"]}
battery_runtime=${vars["battery.runtime"]}
battery_voltage=${vars["battery.voltage"]}
battery_voltage_nominal=${vars["battery.voltage.nominal"]}
input_voltage=${vars["input.voltage"]}
input_voltage_nominal=${vars["input.voltage.nominal"]}
output_voltage=${vars["output.voltage"]}
ups_realpower_nominal=${vars["ups.realpower.nominal"]}
timer_reboot=${vars["ups.timer.reboot"]:--1}
timer_shutdown=${vars["ups.timer.shutdown"]:--1}
)
time=$(date +%s)

echo "ups,$(join , ${tags[@]}) $(join , ${fields[@]})"

Ok now where I am, when I test my script on command line, the formatted line is OK, when I launch the telegraf binary directly by hand in the terminal everything gets fine. But when I use the same command line in the inputs.exec part ... The script failed apparently because he can not execute upsc binary. What did I did wrong ?
 

sretalla

Powered by Neutrality
Moderator
Joined
Jan 1, 2016
Messages
9,703
What user is telegraf running as?
 

Mouftik

Dabbler
Joined
May 12, 2014
Messages
41
Ok, so I found the solution, but not the answer ...
It seems that upsc is in /usr/local/bin which is in the PATH when launched by hand or whatever. But starting with the daemon stuff, even if prefixing my command with an export of local PATH didn't worked. So I launched the upsc from it's full path, and it worked ... Any idea on why the PATH is not accessible via daemon launching ?
 
Top