#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# We want a unique ID for each machine, ideally surviving a reinstall.
# Produce the ID using the hardware addresses of the ethernet devices,
# and save it to a file so that it at least survives the duration of
# this install.
idfile=/var/lib/debathena-counterlog/id
if [ -r $idfile ]; then
  id=$(cat $idfile)
else
  id=$(ifconfig -a | sed -nre 's/.*HWaddr ([^ ]+).*/\1/p' | sort -u \
       | md5sum | awk '{print $1}')
  echo "$id" > $idfile
fi

version=$(machtype -L)
loghost=wslogger.mit.edu
host=$(hostname --fqdn)
mtype=$(lsb_release -sc | sed -e 's/\s/_/g')
ctype=$(machtype -c)

# Traditionally, we do counter logging via syslog.  (We could do it
# via HTTP post or something else instead, if we wanted.)  We can't
# rely on central syslogging being configured because that's a
# cluster-only package.  Fortunately, the syslog protocol is dead
# simple, so just fake a packet to the log host.
msg="<13>root: counterlog: $host $mtype $ctype $version $id cron"
printf "%s" "$msg" | socat STDIN UDP:${loghost}:syslog
