Suppose you’re hosting remote Linux desktops in your cloud environment and want to discover which ones could be able to shutdown to save on valuable resources like money, RAM, or CPU.
Most Linux remote desktop protocols still utilize Xorg (as opposed to Wayland) for their display server. Prime examples would be tigervnc, tightvnc, or X2go. Because of this, the utility xprintidle
is still useful for determining how long an X session has been idle, as its name suggests. With it, we can automate the discovery process with a simple script, querying each desktop to see when it was last used. This assumes you have permissions on the host to run commands as the user actually running the X server (or have access to their .Xauthority file).
Depending on your infrastructure you might choose to run something like the below script via SSH, Ansible, Salt Stack, Puppet, or something else.
This is a rough example and assumes the username is the same on all hosts. You’ll likely have different usernames on each host so you’d need to adjust the script to filter out the users and corresponding display number.
#!/usr/bin/env bash
# A contrived example of checking for idle X sesssions on remote systems.
HOSTS="host1 host2 host3"
USER=shaner # the user running the X session
DISPLAY=:1 # typical/default display for most VNC servers
XPIPATH=./xprintidle # path to 'xprintidle' binary.
for h in ${HOSTS}; do
echo "put ${XPIPATH} /usr/local/bin/" | sftp -b- root@$h >/dev/null
IDLE=$(ssh root@$h sudo -u ${USER} DISPLAY=${DISPLAY} /usr/local/bin/xprintidle)
IDLE=$(echo $IDLE/1000/60 | bc)
printf "[*] ${USER}@${h}:${DISPLAY} idle for ${IDLE} minutes\n"
done
Here’s what it looks like in practice. From the output, we could probably shutdown host1
for the time being.
$ ./check_idles.sh
[*] shaner@host1:1 idle for 18564 minutes
[*] shaner@host2:1 idle for 20 minutes
[*] shaner@host3:1 idle for 108 minutes
$
Leave a Reply
You must be logged in to post a comment.