Skip to content

Instantly share code, notes, and snippets.

@backerman
Created June 29, 2025 04:22
Show Gist options
  • Select an option

  • Save backerman/ca1393fce95eca5794647066203e5dfd to your computer and use it in GitHub Desktop.

Select an option

Save backerman/ca1393fce95eca5794647066203e5dfd to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# add to /etc/rc.local
# [ -x /usr/local/bin/xe-update-stats ] && /usr/local/bin/xe-update-stats init
# add to `crontab -e`
# 1-59 * * * * /bin/sh /usr/local/bin/xe-update-stats
# Get actual pvbus driver - sometimes there are two.
pvdev=""
for pvbus in $(echo /dev/pvbus*); do
type="$(hostctl -qtf "$pvbus" | cut -d: -f2 | tr -d ' ')"
if [ "$type" = "Xen" ]; then
pvdev=$pvbus
fi
done
if [ "$pvdev" = "" ]; then
echo "Unable to communicate with Xen dom0: no appropriate pvbus device found."
exit 1
fi
invocation="hostctl -f $pvdev"
if [ "$1" = "init" ]; then
ostype=$(sysctl -n kern.ostype)
osrelease=$(sysctl -n kern.osrelease)
# XenServer Tools version
$invocation attr/PVAddons/MajorVersion 6
$invocation attr/PVAddons/MinorVersion 2
$invocation attr/PVAddons/MicroVersion 0
$invocation attr/PVAddons/BuildVersion 76888
$invocation attr/PVAddons/Installed 1
for ifline in $(dmesg | grep address | sed -E -e 's/^(xnf.+)\ at.*address\ ([a-fA-F0-9:]+)/\1=\2/' | sort); do
mac="$(echo "$ifline" | cut -d= -f2)"
if="$(echo "$ifline" | cut -d= -f1)"
for vif in $($invocation device/vif); do
if [ "$mac" = "$($invocation device/vif/"$vif"/mac)" ]; then
ip4="$(ifconfig "$if" | grep 'inet[^6]' | cut -d ' ' -f2)"
ip6="$(ifconfig "$if" | grep 'inet6' | cut -d ' ' -f2 | cut -d% -f1)"
if [ -n "$ip4" ]; then
$invocation attr/eth"$vif"/ip "$ip4"
fi
if [ -n "$ip6" ]; then
$invocation attr/eth"$vif"/ipv6/0/addr "$ip6"
fi
fi
done
done
# OS version
$invocation data/os_name "$ostype $osrelease"
$invocation data/os_uname "$osrelease"
$invocation data/os_distro "$ostype"
$invocation data/os_majorver "$(echo "$osrelease" | sed -e 's/\..*$//')"
$invocation data/os_minorver "$(echo "$osrelease" | sed -e 's/^.*\.//')"
# Update XenStore
$invocation data/updated 1
fi
$invocation data/meminfo_total "$(vmstat -s | awk -v pagesize="$(/sbin/sysctl -n hw.pagesize)" '/pages managed$/ { print $1 * pagesize / 1024 }')"
$invocation data/meminfo_free "$(vmstat -s | awk -v pagesize="$(/sbin/sysctl -n hw.pagesize)" '/pages free$/ { print $1 * pagesize / 1024 }')"
@backerman
Copy link
Author

This script has been modified to pass all shellcheck warnings (even though as far as I can tell there's zero chance of one being relevant) and actually look for the correct pvbus device because I've got a VM that also has a HyperV one for some reason on 7.7. A nice extra is that it will now bounce out with a meaningful error if run on something that's not OpenBSD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment