How to Install Telegraf on FreeNAS

Monitoring FreeNAS

Quick and simple instructions on how to install Telegraf collector agent on FreeNAS.

a. Start by creating a folder for Telegraf on one of your pools. I have mine under /mnt/Volume1/home/system/telegraf

b. Download the Telegraf tar for FreeBSD (here), extract it and copy the binary and the .conf files to the folder we created in the previous step

  • ./telegraf-[version]/usr/bin/telegraf
  • ./telegraf-[version]/etc/telegraf/telegraf.conf

c. Create telegraf.init in the same folder (make sure to modify the 2 lines with your path)

26 : ${telegraf_conf:="/mnt/Volume1/home/system/telegraf/${name}.conf"}

32 command_args="-crP ${pidfile} /mnt/Volume1/home/system/telegraf/${name} ${telegraf_flags} -config=${telegraf_conf} >> /var/log/telegraf.log 2>&1"
#!/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:="/mnt/Volume1/home/system/telegraf/${name}.conf"}

# daemon
start_precmd=telegraf_prestart
pidfile="/var/run/${name}.pid"
command=/usr/sbin/daemon
command_args="-crP ${pidfile} /mnt/Volume1/home/system/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"

d. Edit the configuration file (telegraf.conf) according to your needs

e. Create a link in /usr/local/etc/rc.d for telegraf

ln -s /mnt/Volume1/home/system/telegraf/telegraf.init /usr/local/etc/rc.d/telegraf

f. Start the service

service telegraf start

g. Check the output of the logs to make sure it’s working

# tail /var/log/telegraf.log
2020-10-25T17:34:33Z I! Starting Telegraf 1.16.0

h. Create an “Init/Shutdown Script” (change to reflect your path)

ln -s /mnt/Volume1/home/system/telegraf/telegraf.init /usr/local/etc/rc.d/telegraf ; service telegraf start


Credits:

How to Configure a UPS Between Linux and FreeNAS

Linux Shell Hardware FreeNAS

After having bad memory modules due to power outages at home (on both my FreeNAS and my desktop) I knew I had to invest on a UPS. I will cover the details of the configuration, and as usual, I will try to go straight to the point.

Keep in mind that I’m running Arch on my desktop and FreeNAS-11.3-U1, and that the UPS is connected to my desktop (master) via USB.

Instructions

Hardware

a. Connect the USB cable to your computer

b. Make sure that you can see it with lsusb

➤ lsusb | grep -i ups
Bus 002 Device 008: ID 0764:0501 Cyber Power System, Inc. CP1500 AVR UPS

Desktop

Configuring the UPS

a. Start by installing nut and nut-monitor (if you want a nice GUI to view UPS status)

local/nut 2.7.4-2
    A collection of programs which provide a common interface for monitoring and administering UPS, PDU and SCD
    hardware

local/nut-monitor 2.7.4-3
    GUI to manage devices connected a NUT server

b. Next run nut-scanner to see if identifies your UPS. This will give a start configuration that we can use for the UPS

➤ sudo nut-scanner -U
Scanning USB bus.
[nutdev1]
	driver = "usbhid-ups"
	port = "auto"
	vendorid = "0764"
	productid = "0501"
	product = "SL Series"
	vendor = "CPS"
	bus = "002

c. Edit /etc/nut/ups.conf and add the output from the previous step. You can change the device name (between []) to anything you like

Tip: If you only have one UPS keep the name simple so it’s easy for your to remember and type it in the terminal.

[CyberPowerSL700U]
	driver = "usbhid-ups"
	port = "auto"
	vendorid = "0764"
	productid = "0501"
	product = "SL Series"
	vendor = "CPS"
	bus = "002"

d. Start the USB driver

➤ sudo upsdrvctl start

d.1. If you get an error here you might need to setup a udev rule for your UPS. This can be done by creating the file /etc/udev/rules.d/50-ups.rules with the content below

SUBSYSTEM=="usb", ATTR{idVendor}=="XXXX", ATTR{idProduct}=="YYYY", SYMLINK+="ups0", GROUP="nut"

Make sure to add the vendor ID and product ID for the UPS (both lsusb and nut-scanner -U can provide that info). This what mine looks like:

➤ cat /etc/udev/rules.d/50-ups.rules
SUBSYSTEM=="usb", ATTR{idVendor}=="0764", ATTR{idProduct}=="0501", SYMLINK+="ups0", GROUP="nut"

d.2. After that reload udev rules and try to start the UPS USB driver again

➤ sudo udevadm control --reload-rules && sudo udevadm trigger
➤ sudo upsdrvctl start

e. Make sure that you can view the UPS status with upsc

➤ upsc CyberPowerSL700U
battery.charge: 100
battery.charge.low: 10
battery.charge.warning: 20
battery.mfr.date: CPS
battery.runtime: 599
battery.runtime.low: 300
battery.type: PbAcid
battery.voltage: 14.2
battery.voltage.nominal: 12
device.mfr: CPS
device.model: SL Series
device.type: ups
driver.name: usbhid-ups
driver.parameter.bus: 002
driver.parameter.pollfreq: 30
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.product: SL Series
driver.parameter.productid: 0501
driver.parameter.synchronous: no
driver.parameter.vendor: CPS
driver.parameter.vendorid: 0764
driver.version: 2.7.4
driver.version.data: CyberPower HID 0.4
driver.version.internal: 0.41
input.transfer.high: 140
input.transfer.low: 96
input.voltage: 115.0
input.voltage.nominal: 0
output.voltage: 115.0
ups.beeper.status: enabled
ups.delay.shutdown: 20
ups.delay.start: 30
ups.load: 44
ups.mfr: CPS
ups.model: SL Series
ups.productid: 0501
ups.realpower.nominal: 375
ups.status: OL
ups.timer.shutdown: -60
ups.timer.start: -60
ups.vendorid: 0764

Configuring the NUT Server

We are going to configure it as the master (meaning that it will send the shutdown command to the listening clients when the battery is low).

Info: If you want to know what the shutdown process looks like, take a look at the Official NUT Document: Shutdown design

a. Edit /etc/nut/nut.conf and set the MODE to netserver

MODE=netserver

b. Now let’s configure the listening IP and port. Edit /etc/nut/upsd.conf, edit theLISTEN directive as below:

LISTEN 0.0.0.0 3493

c. Configure the users in /etc/nut/upsd.users

Add passwords under my_master_password and my_slave_password

[upsmaster]
  # Administrative user
  password = my_master_password

  # Allow changing values of certain variables in the UPS
  actions = SET

  # Allow setting the "Forced Shutdown" flag in the UPS
  actions = fsd

  # Allow all instant commands
  upsmon master

[upsremote]
  password = my_slave_password
  upsmon slave

d. Now let’s tell upsmon to monitor the UPS. Edit /etc/nut/upsmon.conf with the lines below

Make sure to change the UPS name (ups_name) and master password (my_master_password)

MONITOR ups_name@localhost 1 [master user] [master password] master

e. Check the status of the NUT server with systemctl status nut-server

f. Enable the NUT server and monitor

➤ sudo systemctl enable --now nut-server.service
➤ sudo systemctl enable --now nut-monitor.service

Timing

It’s important that we understand how the shutdown process works, and how much time each of the slaves take to power down. At the end of the process the master node will send a shutdown command to the UPS, and the last thing we want is to cutoff power to one of our slaves.

I have 3 devices connected to my UPS. I have timed their shutdown so I now how much time I need:

  • Desktop - 30s
  • FreeNAS - 60s (without drives)
  • CentOS - 30s

On top of that, I also need to take in consideration the UPS low battery run time (what the low battery threshold is and how long it can run), and what the shutdown delay should be.

Oversimplified chronogram

UPS       |####################### Low Battery Run Time ######################|
          |                             ########### Shutdown Delay ###########|
          |
Master    |######### Shutdown ##########|
(Desktop) |
          |
Slave1    |####################### Shutdown #########################|
(FreeNAS) |
          |
Slave2    |########## Shutdown ##########|
(Centos)  |
          -------------------------------|---------------------------|--------|
   Time                                 30s                         60s      90s  
Let’s get started

a. Start by timing your device with a normal shutdown

b. Now we can change our configuration, and that will vary with your device

Option 1: Use upsrw

You can use upsrw to set the variable, but that sometimes will not work as it only sets the value for the OS and doesn’t write back to the UPS (which is the same for all 3 cases). The variables we are looking for are:

  • battery.charge.low - Threshold in battery percentage that the UPS should change it’s status to LB (low battery)
  • battery.runtime.low - Threshold in battery run time (seconds) that the UPS should change it’s status to LB (low battery)
  • ups.delay.shutdown - Additional delay after the Master has shutdown and the UPS will wait to cutoff power (the important one we just covered)
  • ups.delay.start - This is how long the UPS should wait to restore power. For home use I usually set this to high

You can set them with upsrw -s [variable=value] -u [UPS admin user] [ups name]. For example:

upsrw -s ups.delay.shutdown=60 -u upsmaster CyberPowerSL700U

⚠️ WARNING: The main problem with this process is that it does not survive restarts.

Option 2: Use the override directive

You can use the override directive in /etc/nut/ups.conf to override levels reported back from the UPS. Similar to the option above, this will only update the outside world (the master device and not the UPS itself). See UPS.CONF(5) for more info.

Option 3: Use offdelay

This is the method that worked the best for my battery model. I have configured the following 3 variables in /etc/nut/ups.conf.

  • offdelay - How long to wait to cut power - overrides ups.delay.shutdown
  • ondelay - How long to wait before turning on devices again - overrides ups.delay.start
  • lowbatt - Overrides battery.charge.low

Here’s my updated /etc/nut/ups.conf

[CyberPowerSL700U]
	driver = "usbhid-ups"
	port = "auto"
	vendorid = "0764"
	productid = "0501"
	product = "SL Series"
	vendor = "CPS"
	bus = "002"
  offdelay = "90" # How long to wait to cut power - overrides ups.delay.shutdown
  ondelay = "50" # How long to wait before turning on devices again - overrides ups.delay.start
  lowbatt = "30" # Overrides battery.charge.low

c. Restart nut-server.service

➤ sudo systemctl restart nut-server.Services

Configuring FreeNAS as the client/slave

a. Login to your FreeNAS and go to Services. Enable the UPS service, set it to start automatically and then click on the edit button

b. Make the following configuration

  • UPS Mode - Slave
  • Identifier - Name for the UPS
  • Remote Host - IP of the master
  • Remote Port - Port
  • Driver - Look for your UPS model. I could not find my model, so I typed in the driver that I’m using on the master host (usbhid-ups) *
  • Port or Hostname - Hostname of the master host

⚠️ WARNING: While this allowed me to save my configuration and the shutdown works, when I access the config page the driver field is blank. This is different than on older versions of FreeNAS where the driver option was only available in Master mode. Beware that it may create problems if you also decide to go this route.

  • Shutdown Mode - I chose ‘UPS reaches low battery’
  • Shutdown Timer - 30 (default)
  • Shutdown Command - I’m using /sbin/shutdown -p now
  • No Communication Warning Time - 60 (default)
  • Monitor User - The slave user you configured in ups.conf on the master
  • Monitor Password - The slave password you configured in ups.conf on the master

Check the ‘Send Email Status Updates’ if you’d like to received emails (also add your email address). Click on save.

c. Check the connection to the UPS with the command below

upsc ups_name@ip:port

d. Check that FreeNAS is actually monitoring the UPS (/var/log/nut/ups.log)

OL - On line (no power failure)

OB - On battery

LB - Low battery

e. Log back to your master machine and confirm that you can see FreeNAS as a connected client (upsc -c)

Assuming that my FreeNAS IP is 192.168.10.20

$ upsc -c ups_name@localhost
192.168.10.20

Tip: You can also check the status of nut-server.service and it will show you the connected clients

Bonus: Setup CentOS as client/slave

As I mentioned before, I also have a CentOS server that connects to this UPS. So here’s how to configure it.

a. Install nut-client

# yum install -y nut-client

b. Add the monitor line to /etc/ups/upsmon.conf

MONITOR CyberPowerSL700U@10.13.15.200 1 [slave user] [slave pass] slave

c. Enable and start the service

# systemctl enable --now nut-monitor.service

d. Check that you can see the new client in the master (upsc -c [ups name])

# upsc -c CyberPowerSL700U
127.0.0.1
192.168.10.20  # FreeNAS
192.168.10.21  # CentOS

Testing

Now let’s test see if all works. Instead of pulling the cord we can instead run upsmon -c fsd on the master (this will save wear and tear on your battery). Hopefully we have configured our delay time properly and we should see all our devices powering off, and eventually the UPS as well.

Tip: Because I have quick removable drive bays on my FreeNAS server, I have removed all drives before testing (to be extra careful and to avoid any issues).

➤ sudo upsmon -c fsd

How to Quickly Create an Encrypted USB Drive on KDE Plasma

Need to create an encrypted USB drive? Are your running KDE Plasma?

Then great! Here are 6 simple steps on how to quickly and painlessly create an encrypted USB drive with KDE Partition Manager.

Let’s start

a. Start by inserting your USB drive

b. Now launch KDE Parition Manager and type your password

c. Select the USB drive and right click to delete the existing partition

⚠️ WARNING: remember, this will delete all your existing files)

d. Right click to create a new partition

e. Select your “File system” type, check “Encrypt with LUKS”, set the “Password” and “Label” and click on “Ok”

f. Click on apply and you are done. Go grab a coffee because you worked really hard you deserve it

Mounting the Drive

The mounting process should be as painless as setting up the USB drive.

a. Insert the USB drive and click on “Mount” under “Disk & Device” on your system tray

b. Type in your very secure password (mine is “password123”) and profit

Tip: You can set it to remember if you really trust your own machine

How to Install Waveshare 3.5" LCD on Ubuntu 20.04

Linux Hardware Raspberry Pi

Following the steps of an older post (Installing Kuman 3), I find myself trying to configure the same Waveshare 3.5” LCD touch screen on my Raspberry Pi 3B, but now with Ubuntu 20.04 64.bit.

As usual the LCDs from Waveshare are not that easy to configure. While researching online I could not find any working instructions on how to configure the screen with Ubuntu 20.04. I found a few Git repos (Wavesahre and LCD Wiki), but they all failed to get me with a working config.

After spending a lot of time I was able to get it to work using some of the files and instructions from Waveshare’s official Git Repo (as well as LCD Wiki).

If you want to save yourself sometime, just use my Ansible repo to get your RPI3B configured. Otherwise, the manual instructions are bellow.

GitHub: waveshare35-rpi3b-ubuntu-20.04-64

Instructions

a. Download Ubuntu 20.04 64-bit for RPI3 and setup your SD card

https://ubuntu.com/download/raspberry-pi

Make sure to check the downloaded file

b. Power the Pi on, login (SSH or HDMI), change the password and update

# apt-get update apt-get upgrade

c. Install Xubuntu and evdev

Note: you could just install X instead of Xubuntu and there’s a chance the instructions will work

# apt-get install xubuntu-desktop xserver-xorg-input-evdev

d. Backup the files we are going to modify

We are not modifying config.txt, but we are backing it up just in case

# cd /boot/firmware

# cp cmdline.txt cmdline.txt.orig
# cp usercfg.txt usercfg.txt.orig
# cp config.txt config.txt.orig

e. Add the following lines to /boot/firmware/usercfg.txt

dtparam=i2c_arm=on
dtparam=audio=on
dtparam=spi=on
enable_uart=1
dtoverlay=waveshare35a
hdmi_drive=2
disable_overscan=1

f. Make changes to /boot/firmware/cmdline.txt

  • Modify
    • console=ttyAMA0,115200
  • Add
    • fbcon=map:10
    • fbcon=font:ProFont6x11
net.ifnames=0 dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=LABEL=writable rootfstype=ext4 elevator=deadline rootwait fixrtc fbcon=map:10 fbcon=font:ProFont6x11

g. Create the directory /etc/X11/xorg.conf.d

# mkdir -p /etc/X11/xorg.conf.d

h. Create the file /etc/X11/xorg.conf.d/99-calibration.conf

Section "InputClass"
    Identifier "calibration"
    MatchProduct "ADS7846 Touchscreen"
    Option "Calibration" "3932 300 294 3801"
    Option "SwapAxes" "1"
    Option "EmulateThirdButton" "1"
    Option "EmulateThirdButtonTimeout" "750"
    Option "EmulateThirdButtonMoveThreshold" "30"
EndSection

i. Create the file /usr/share/X11/xorg.conf.d/99-fbturbo.conf

Section "Device"
    Identifier "Allwinner A10/A13 FBDEV"
    Driver "fbturbo"
    Option "fbdev" "/dev/fb2"

    Option "SwapbuffersWait" "true"
EndSection

j. Copy 10-evdev.conf to 45-evdev.conf

# cd /usr/share/X11/xorg.conf.d/
# cp 10-evdev.conf 45-evdev.conf

k. Clone https://github.com/waveshare/LCD-show.git

# mkdir /root/Git
# cd /roo/Git

# git clone https://github.com/waveshare/LCD-show.git

# cd LCD-show

l. Copy waveshare35a-overlay.dtb to /boot/firmware/overlays as both waveshare35a-overlay.dtb and waveshare35a-overlay.dtbo

# cp waveshare35a-overlay.dtb /boot/firmware/overlays/waveshare35a-overlay.dtb
# cp waveshare35a-overlay.dtb /boot/firmware/overlays/waveshare35a-overlay.dtbo

m. Reboot and enjoy

How to Show Progress for dd

Bash Linux Hardware Storage

If you have reached this page I expect that you already know what dd is. But you if you don’t, dd is a command-line utility that is used to convert and copy files. It’s commonly found on various (Linux) distro help pages that show new users how to setup USB (or Micro SD) drives to install or run (like Raspberry Pi) the OS.

One of the main problems is that when running the dd utility, it does not provide any information on the current status of the copy process. This can make users anxious not knowing if it’s done or not.

The Solutions

1. Running dd with the status option

This requires you to be running dd version 8.24 and above. If you are running a modern Linux distro you are most likely covered.

You can check your version with dd --version:

➤ dd --version
dd (coreutils) 8.32
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin, David MacKenzie, and Stuart Kemp.

We will also need to use oflag=sync to make sure that we are really syncing as data is being copied so we get a better sense of when it’s done. Otherwise dd goes blank at the end as it’s trying to sync.

Note: while using oflag=sync makes the copy slower, using a higher block size will help speed up the process (bs=4M instead of bs=1M)

The command:

Structure:

dd bs=4M if=/path/to/input of=/path/to/output status=progress oflag=sync

Example: here are backing up the contents of /dev/sdf to a file (ubuntu-server-20.04.1-updated.iso)

sudo dd bs=4M if=/dev/sdf of=ubuntu-server-20.04.1-updated.iso status=progress oflag=sync

The output:

1. Running dd with pv

In reality this should be option 1 as it gives a nicer output with a progress bar, ETA and other data. pv is a terminal-based tool for monitoring the progress of data through a pipeline. It can potentially be used with any pipe.

The command:

Structure:

pv -tpreb /path/to/input | sudo dd of=/path/to/output bs=4M oflag=sync

Options used:

  • -t - “Turn the timer on”
  • -p - “Turn the progress bar on”
  • -r - “Turn the rate counter on”
  • -e - “Turn the ETA timer on”
  • -b - “Turn the total byte counter on”

Example: Here we are copying the image file to /dev/sdf

pv -tpreb ubuntu-20.04.1-preinstalled-server-arm64+raspi.img | sudo dd of=/dev/sdf bs=4M oflag=sync

The output:


Putting all in a Bash alias for re-use

Add the function below to your ~/.bash_aliases and it will be available whenever you need. Call it with dd_iso [image] [device] and it will use pv if it’s already installed in your syste.

dd_iso ()
{
    usage="usage: dd_iso [image] [device]";
    if [[ $# -lt 2 ]]; then
        echo "$usage";
        return 0;
    else
        if [[ $# -eq 2 ]]; then
            iso="$1";
            device="$2";
        fi;
    fi;
    if [[ ${iso##*.} != iso && ${iso##*.} != img ]]; then
        echo "The first parameter should be an iso";
        return 1;
    else
        if [[ ! -b "$device" ]]; then
            echo "The second parameter needs to be a device";
            return 1;
        fi;
    fi;
    device_type="$(basename "$(readlink -f "/sys/class/block/${device##*/}/..")")";
    if [[ "$device_type" != "block" ]]; then
        echo "Do not specify a parition as the device";
        return 1;
    fi;
    sudo dd bs=4M if="$iso" of="$device" status=progress oflag=sync
}
code with