commit b7798739cea8b961cc2c8671f4b948c39361227d Author: 0xFEEDC0DE64 Date: Fri Jan 23 23:57:50 2026 +0100 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..ab7355a --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# telegraf-bcachefs-input + +Telegraf bcachefs monitoring + +Add the following config section to your telegraf.yml: + +``` +[[inputs.exec]] + commands = ["/etc/telegraf/bcachefs.sh"] + data_format = "influx" +``` + +![example disks table](disks-table.png) +![example disks graphs](disks-graphs.png) +![example disks states](disks-states.png) diff --git a/bcachefs.sh b/bcachefs.sh new file mode 100644 index 0000000..db00bd8 --- /dev/null +++ b/bcachefs.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +function convert_to_bytes() { + awk ' + { + val=$0 + num=val + sub(/[KMGT]$/, "", num) + + unit=substr(val, length(val), 1) + mul=1 + if (unit=="K") mul=1024 + else if (unit=="M") mul=1024^2 + else if (unit=="G") mul=1024^3 + else if (unit=="T") mul=1024^4 + + printf "%.0f\n", num * mul + }' <<< "$1" +} + + +FSPATH="/mnt" + +UUID= + +PARSER_STATE=0 +#echo switch to 0 +while IFS= read -r line; do + case $PARSER_STATE in + 0) + if [[ $line =~ ^Filesystem:[[:space:]]+([^[:space:]]+)$ ]] + then + uuid="${BASH_REMATCH[1]}" +# echo "uuid=$UUID" + elif [[ $line == Data\ type* ]] + then + #echo switch to 1 + PARSER_STATE=1 + #continue + elif [[ $line == "Compression:" ]] + then + #echo switch to 2 + PARSER_STATE=2 + #continue + elif [[ $line == "Btree usage:" ]] + then + #echo switch to 3 + PARSER_STATE=3 + #continue + elif [[ $line == "Pending reconcile:" ]] + then + #echo switch to 4 + PARSER_STATE=4 + #continue + elif [[ $line =~ ^([^ ]+)\ \(device\ ([0-9]+)\):[[:space:]]+([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]+([0-9]+)%$ ]] + then + blockdev="${BASH_REMATCH[3]}" + if [[ ! $blockdev == /dev/* ]] + then + blockdev="/dev/$blockdev" + fi + echo -n "bcachefs,fspath=\"$FSPATH\",UUID=\"$UUID\",devlabel=\"${BASH_REMATCH[1]}\",devid=${BASH_REMATCH[2]},blockdev=\"$blockdev\" mode=\"${BASH_REMATCH[4]}\",usage=${BASH_REMATCH[5]}" + + #echo switch to 5 label="$label" devid="$devid" block_device="$block_device" mode="$mode" usage="$usage" + PARSER_STATE=5 + #continue + fi + ;; + 1) + if [[ "$line" == "" ]] + then + #echo switch to 0 + PARSER_STATE=0 + #continue + fi + ;; + 2) + if [[ "$line" == "" ]] + then + #echo switch to 0 + PARSER_STATE=0 + #continue + fi + ;; + 3) + if [[ "$line" == "" ]] + then + #echo switch to 0 + PARSER_STATE=0 + #continue + fi + ;; + 4) + if [[ "$line" == "" ]] + then + #echo switch to 0 + PARSER_STATE=0 + #continue + fi + ;; + 5) + if [[ "$line" == "" ]] + then + echo + #echo switch to 0 + PARSER_STATE=0 + #continue + elif [[ $line =~ ^[[:space:]]*([^:]+):[[:space:]]*(.*)$ ]]; then + field="${BASH_REMATCH[1]// /_}" + rest="${BASH_REMATCH[2]}" + + read -r val1 val2 val3 <<<"$rest" + val1="$(convert_to_bytes "$val1")" + + echo -n ",${field}_data=$val1" + if [[ ! -z "$val2" ]] + then + echo -n ",${field}_buckets=$val2" + fi + if [[ ! -z "$val3" ]] + then + val3="$(convert_to_bytes "$val3")" + echo -n ",${field}_fragmented=$val3" + fi + #else + #echo unknown? + fi + ;; + esac + + #if [[ $PARSER_STATE -ne 1 ]] + #then + # echo "Got line: $line" + #fi +done < <(sudo bcachefs fs usage -a "$FSPATH") diff --git a/disks-graphs.png b/disks-graphs.png new file mode 100644 index 0000000..b16dce2 Binary files /dev/null and b/disks-graphs.png differ diff --git a/disks-states.png b/disks-states.png new file mode 100644 index 0000000..225adc3 Binary files /dev/null and b/disks-states.png differ diff --git a/disks-table.png b/disks-table.png new file mode 100644 index 0000000..69963c2 Binary files /dev/null and b/disks-table.png differ