#!/bin/sh
# $FreeBSD: ports/Tools/portbuild/scripts/reportload,v 1.16 2010/12/01 02:23:14 linimon Exp $

# client script to report load to the server.

pb=/var/portbuild
# note: uname is not being overridden
arch=$(uname -m)
osver=$(sysctl -n kern.osreldate)

. ${pb}/${arch}/client.conf
. ${pb}/${arch}/portbuild.conf
if [ -f ${pb}/${arch}/portbuild.$(hostname) ]; then
    . ${pb}/${arch}/portbuild.$(hostname)
fi

# Look for exceptional conditions
error=
for i in squid disk; do
  if [ -f ${scratchdir}/.${i} ]; then
    error="${i} ${error}"
  fi
done

# XXX MCL DEBUG 20100727
# on occasion, a machine winds up with a huge number (> 300) of chroots.
# When this happens, the following command takes more than a minute, and
# pointyhat will pound it to death, resulting in 'kern.ipc.maxpipekva
# exceeded; see tuning(7)'.  So, as a workaround until the reason for the
# stale chroots is understood, limit the time we allow this to run.
ulimit -t 10
num=$(echo $(ls -1d ${scratchdir}/*/*/*/used ${scratchdir}/*/*/chroot/*/used 2>/dev/null| wc -l))

echo "arch=${arch}"
echo "osversion=${osver}"
echo "jobs=${num}"

cd /var/portbuild/${arch}
for i in */builds/*; do
    buildenv=${arch}/${i%%/*}/${i##*/}
    if [ -f ${i}/.ready ]; then
	buildenvs="${buildenv} ${buildenvs}"
    fi
done
echo "buildenvs=${buildenvs}"
echo -n "load="
uptime
echo "error=${error}"
