Adding a note to an old thread in case this is of use to anybody else:
This post at crunchbang http://crunchbang.org/forums/viewtopic.php?id=26447
details a bash script to determine the time in seconds since the last mouse or keyboard action in the system. It doesn't depend on any X session or user being logged in since it's just looking at hardware interrupts. You could modify the script to do a shutdown rather than a suspend.
So for keyword searches: bash script for user idle / inactive time to suspend or lock