FreeBSD Grafana/Prometheus Chrony Exporter

Help with operating systems, apps, and software-related issues.
User avatar
ccb056
Site Administrator
Posts: 1003
Joined: January 14th, 2004, 11:36 pm
Location: Texas

FreeBSD Grafana/Prometheus Chrony Exporter

Post by ccb056 »

https://grafana.com/grafana/dashboards/19186-chrony/
https://github.com/SuperQ/chrony_exporter

Code: Select all

fetch https://github.com/SuperQ/chrony_exporter/releases/download/v0.12.1/chrony_exporter-0.12.1.freebsd-amd64.tar.gz

Code: Select all

tar -xzf chrony_exporter-0.12.1.freebsd-amd64.tar.gz

Code: Select all

cd chrony_exporter-0.12.1.freebsd-amd64

Code: Select all

install -m 755 chrony_exporter /usr/local/bin/
create file /usr/local/etc/rc.d/chrony_exporter with contents:

Code: Select all

#!/bin/sh
# PROVIDE: chrony_exporter
# REQUIRE: NETWORKING
# KEYWORD: shutdown

. /etc/rc.subr

name="chrony_exporter"
rcvar="${name}_enable"

# The actual exporter binary
procname="/usr/local/bin/chrony_exporter"

# daemon(8) will be the command invoked by rc.subr
command="/usr/sbin/daemon"

# pidfiles (parent and child)
pidfile="/var/run/${name}.pid"
child_pidfile="/var/run/${name}.child.pid"

# Default flags (override in /etc/rc.conf via chrony_exporter_flags="...")
: ${chrony_exporter_flags:="--chrony.address=unix:///var/run/chrony/chronyd.sock --web.listen-address=:9123 --collector.sources --collector.serverstats --collector.chmod-socket"}

# Arguments passed to daemon(8):
#  -r            restart child if it exits
#  -p <file>     write daemon pid
#  -P <file>     write child pid
#  -o <file>     capture stdout/stderr
command_args="-p ${pidfile} -P ${child_pidfile} -o /var/log/${name}.log ${procname} ${chrony_exporter_flags}"

start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

chrony_exporter_start()
{
    echo "Starting ${name}."
    ${command} ${command_args}
}

chrony_exporter_stop()
{
    if [ -r "${pidfile}" ]; then
        kill "$(cat "${pidfile}")" 2>/dev/null || true
        rm -f "${pidfile}" "${child_pidfile}"
        echo "Stopped ${name}."
    else
        echo "${name} not running (no pidfile)."
    fi
}

chrony_exporter_status()
{
    if [ -r "${child_pidfile}" ] && kill -0 "$(cat "${child_pidfile}")" 2>/dev/null; then
        echo "${name} child is running as pid $(cat "${child_pidfile}")"
        return 0
    elif [ -r "${pidfile}" ] && kill -0 "$(cat "${pidfile}")" 2>/dev/null; then
        echo "${name} daemon is running as pid $(cat "${pidfile}") (child may have exited)"
        return 0
    else
        echo "${name} is not running"
        return 1
    fi
}

load_rc_config "${name}"
: ${chrony_exporter_enable:="NO"}

run_rc_command "$1"

Code: Select all

chmod +x /usr/local/etc/rc.d/chrony_exporter

Code: Select all

sysrc chrony_exporter_enable="YES"

Code: Select all

service chrony_exporter start
add to the bottom of /usr/local/etc/prometheus.yml

Code: Select all

  - job_name: 'chrony_exporter'
    static_configs:
      - targets: ['localhost:9123']   # or use the host IP/hostname
create new directories and file: /usr/local/etc/prometheus/rules/chrony.yml

Code: Select all

groups:
  - name: Chrony
    rules:
      - record: instance:chrony_clock_error_seconds:abs
        expr: >
          abs(chrony_tracking_last_offset_seconds)
          +
          chrony_tracking_root_dispersion_seconds
          +
          (0.5 * chrony_tracking_root_delay_seconds)
edit /usr/local/etc/prometheus.yml to add

Code: Select all

rule_files:
  - "/usr/local/etc/prometheus/rules/*.yml"

Code: Select all

service prometheus restart
log rotation:

Code: Select all

vi /etc/newsyslog.conf

Code: Select all

/var/log/chrony_exporter.log   root:wheel  644  7  *  @T00  -
User avatar
ccb056
Site Administrator
Posts: 1003
Joined: January 14th, 2004, 11:36 pm
Location: Texas

How to update Chrony Exporter and add sourcestats

Post by ccb056 »

Code: Select all

fetch https://github.com/SuperQ/chrony_exporter/releases/download/v0.13.1/chrony_exporter-0.13.1.freebsd-amd64.tar.gz

Code: Select all

tar -xzf chrony_exporter-0.13.1.freebsd-amd64.tar.gz

Code: Select all

cd chrony_exporter-0.13.1.freebsd-amd64

Code: Select all

service chrony_exporter stop

Code: Select all

cp chrony_exporter /usr/local/bin/chrony_exporter

Code: Select all

chmod 755 /usr/local/bin/chrony_exporter

Code: Select all

vi /usr/local/etc/rc.d/chrony_exporter
add:

Code: Select all

--collector.sourcestats

Code: Select all

service chrony_exporter start

Code: Select all

chrony_exporter --version