Idle is defined as when no foreground program is running and the terminal is waiting for user input, but the user does not make any input.
Actually, my ultimate goal is: shutdown when all SSH sessions are idle and x-window is also idle.
The idle time of x-window can be determined with xprintidle. So for now I just need to determine if all SSH sessions are idle or not.
I've looked for many ways to determine if a machine is idle in Linux, but most of them are based on x-window to determine if the desktop is idle.