When was the machine last touched

monitoringx11

It is occasionally useful for me to know when my machine (Debian wheezy) was last touched. To be precise, I mean the more recent of the times I typed on the keyboard or moved the mouse. When I currently try to do this, I adopt ad-hoc means, like checking the last modification times of files that I was editing. However, a most systematic way would be useful. If not the precise time, approaches to get a reasonably close estimate would be welcome. I would prefer methods that used information that was not easily destroyed, like file modification times.

I suppose the ultimate approach would be to install some kind of program that monitors my computers activity. I don't know if I would go so far, but would be willing to hear about it, at least.

Any software installed for this purpose must be free, and available in Debian, for preference. Having said that, solutions don't have to be Debian-specific, or even Linux specific. In fact, it is better if they are not.

It's ok if only activity in an X11 session is taken into account.

Best Answer

There is an xprintidle utility (available as a package, at least in Debian and Ubuntu) that will do this. It gives you the number of milliseconds since last keyboard or mouse activity. Of course, if you type that in a terminal and run it, the result will be near-0.

Alternatively, there is a Perl module.

C code (apparently borrowed from that Perl module) can be found on Stack Overflow.

edit: You mentioned on chat possibly wanting it to be like a munin graph. Actually, you should be able to hook it into munin, but you'll need to get it access to your X display. The minimal requirement to do that is to set the DISPLAY=:0 environment variable (or whatever display you log in on) and also get it access to the magic-cookie, which will come from ~/.Xauthority or $XAUTHORITY. xauth is the command to manipulate xauthority files. See also Open a window on a remote X display (why "Cannot open display")? for some approaches on getting access to the X display.

Related Question