pleroma.debian.social

pleroma.debian.social

Is there a better way to get values out of Linux /proc than grep e.g.:

# grep size /proc/spl/kstat/zfs/arcstats

@mwl I'm not sure if this is what you're looking for, but I use these, which I found on a blog post somewhere (I can't recall atm):

# Get ZFS ARC size
function get-arc-size() {
awk '/^size/ { printf "ARC size: %.2f GiB\n", $3/1024/1024/1024; exit }' /proc/spl/kstat/zfs/arcstats
}

# Get ZFS ARC hit rate
function get-hit-rate() {
awk '/^hits/ { hits=$3 } /^misses/ { misses=$3 } \
END { printf "Hit rate: %.2f%%\n", hits*100/(hits+misses) }' /proc/spl/kstat/zfs/arcstats
}

@mwl I mean I usually awk for stuff like this, like
# awk '$1=="arc_raw_size"{print $2}' /proc/spl/kstat/zfs/arcstats

(if there's a specific value I want)

@hyperreal @mwl I was going to pithily suggest awk but here you’ve provided an actual useful use of it
replies
0
announces
0
likes
1

@mwl

I don't have a Linux+ZFS box at hand to test that particular endpoint, but some values get exposed via sysctl, so you might be able extract things that way.

If you're stuck with pseudo-files in /proc, sadly each one seems to have its own representation.

If a grep does the job, and you only expect one result, you can bail early with --max-count=1 to save parsing the rest of the file. Or you can use if you only want a value from a key/value pair, something like

$ awk -F: '$1 ~ /size/{print $2; exit}' /proc/spl/kstat/zfs/arcstats

@gumnos @mwl One reason I look forward to the book being done is so I can see "OK, just how does a BSD solve this instead?"

@trini @gumnos

Alcohol, usually

@mwl @gumnos So it's one of those "We're not so dissimilar, you and I" things? Except wait, one of them is always the villain..

@trini @gumnos

It's us. We're the villains.

@mwl awk?

@mwl Are you asking to use something different to grep, which in that case the answer is plenty of alternatives.

Or is it more about the kernel interface? Don't read a file, but use a.. say an ioctl()?

The answer to the latter is most stuff is available out of /proc or /sys and not exposed elsewhere by the kernel.

@smallsees the latter, thank you.

BSD sysctl has spoiled me

@mwl If you want something cross-platform I'm pretty sure `arcstat` comes with openzfs and it just does the thing in both FreeBSD and Linux (reading sysctls on FreeBSD and the file you're grepping on Linux) and generates the same output on both.

@fwaggle FreeBSD doesn't have arcstat

@mwl Oh bummer, I thought because there was code in there for it that it would 😭