#!/bin/sh VERSION='Sun Mar 13 21:45:11 EST 2005' ###################################### # Defaults ###################################### IDLE_TIME=30 # How long in seconds we consider idle SLEEP_INTERVAL=20 # How long in seconds we sleep between tests # Date formats - pick which one you like the look of # #DATE_FORMAT='%Y/%m/%d %T' # 2005/03/13 21:31:13 #DATE_FORMAT='%v %T' # 13-Mar-2005 21:34:32 DATE_FORMAT='%a %v %T' # Sun 13-Mar-2005 21:35:23 ###################################### # HELP ###################################### help () { cat < Append output to filename instead of writing to STDOUT -i|-idle How long we consider idle [default: $IDLE_TIME] -s|-sleep How long we sleep between tests [default: $SLEEP_INTERVAL] -d|-date Format for date output (strftime style) [default: $DATE_FORMAT] $0 version $VERSION END } ###################################### # Handle Options ###################################### while [ -n "$1" ]; do case $1 in -h*) help; exit 3;; -o*) OUTFILE=$2 ; shift 2;; -i*) IDLE_TIME=$2 ; shift 2;; -s*) SLEEP_INTERVAL=$2 ; shift 2;; -d*) DATE_FORMAT=$2 ; shift 2;; --) shift;break;; # end of options -*) echo "error: no such option $1. -h for help";exit 2;; *) break;; esac done ###################################### # Subroutines ###################################### # I lifted this from someone else # getidle () { /usr/sbin/ioreg -c IOHIDSystem | \ /usr/bin/perl -ane 'if (/Idle/) {$idle=int((pop @F)/1e9); print $idle,"\n"; last}' } # Called with previous state and new state # output() { if [ "$1" != "$2" ] ; then if [ "$OUTFILE" ] ; then date +"$DATE_FORMAT $2" >> $OUTFILE else date +"$DATE_FORMAT $2" fi fi } ###################################### # main() ###################################### STATE='none' while [ 1 ] ; do IDLETIME=`getidle` if test "$IDLETIME" -lt "$IDLE_TIME" ; then output $STATE notidle ; STATE='notidle' else output $STATE idle ; STATE='idle' fi sleep $SLEEP_INTERVAL done