[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#714477: killer: Is no longer able to figure out when X users last were active



I asked on #debian-devel for ideas how to fix killer, and Mithrandir
suggested I look at the xprintidle package, while weasel pointed me to
the attached script he use to suspend unused machines.

Both can provide inspiration for a fix for killer.

-- 
Happy hacking
Petter Reinholdtsen
#!/bin/bash

# Copyright (c) 2012 Peter Palfrader <peter@palfrader.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

PATH="$PATH:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"


MYLOGNAME="`basename "$0"`[$$]"
HOSTNAME="$(hostname)"
FLAGFILE="/var/run/busy-host"
BACULA_FD=9102
X_IDLE_TIME=90 # minutes



usage()
{
cat << EOF
usage: $0

OPTIONS:
   -n      no-do - do not really suspend.
   -t <x>  touch - do not suspend for <x> minutes
   -h      show this help.
EOF
}

info() {
  logger -p daemon.info -t "$MYLOGNAME" "$1"
}



VERBOSE=0
NODO=""
max=0

while getopts "hnvt:" OPTION
do
  case $OPTION in
         h)
             usage
             exit
             ;;
         n)
             NODO=":"
             ;;
         t)
             max="$OPTARG"
             ;;
         v)
             VERBOSE=$(( VERBOSE + 1 ))
             ;;
         *)
             usage >&2
             exit 1
             ;;
     esac
done
shift $(($OPTIND - 1))


# chk_net <port>
# returns true (0) if there are connections to that port.
chk_net() {
  local port="$1"; shift
  # ignore connections from munin
  local con="$(ss -nt "sport = :$port" | egrep -v '2001:628:2120:500:4000:feff:27:159|141.201.27.159' | wc -l)"
  if [ "$con" -gt 1 ]; then
    return 0
  else
    return 1
  fi
}

# checks echo the number of minutes in the future when suspending would be ok.
# 0 means it's ok to suspend now.

check_sge() {
  local jcnt="$(qstat -u '*' -q "*@$HOSTNAME" -s r | wc -l)"
  if [ "$jcnt" = 0 ]; then
    echo 0
  else
    echo 1
  fi
}

check_bacula() {
  if chk_net $BACULA_FD; then
    echo 1
  else
    echo 0
  fi
}

check_ssh() {
  if chk_net 22; then
    echo 10
  else
    echo 0
  fi
}

check_puppet() {
  if pgrep puppet > /dev/null; then
    echo 1
  else
    echo 0
  fi
}

check_xsession() {
  for s in /var/run/gdm3/auth-for-Debian-gdm-*/database /run/xauth/*; do
    if [ -e "$s" ] ; then
      i="$(XAUTHORITY="$s" DISPLAY=:0 xprintidle)"
      i=$(( i / 1000 / 60 ))
      if [ "$i" -lt "$X_IDLE_TIME" ] ; then
        echo $(( $X_IDLE_TIME - $i ))
        return
      fi
    fi
  done
  echo 0
}

suspend_host() {
  info "System is suspending"
  echo "System is suspending." | wall
  cat /dev/null > "$FLAGFILE"
  sync
  sleep 1
  susp="pm-suspend-hybrid"
  case "$HOSTNAME" in
    erna|judith|irina|clara|sabine|evi|gitti|inge|emma|gabi)
      susp="pm-suspend"
      ;;
    *)
      if ! nagios-check-running-kernel > /dev/null; then
        susp="pm-suspend"
      fi
  esac
  $NODO $susp || info "Calling $susp failed!"
}

for check in sge bacula ssh puppet xsession; do
  result=$(check_$check)
  [ "$VERBOSE" -ge 1 ] && info "Check $check: $result"
  [ "$result" -gt "$max" ] && max="$result"
done

ref="$max minutes"
if [ "$max" -gt 0 ]; then
  info "Will suspend no sooner than $max minutes from now"
  if [ -e "$FLAGFILE" ] && [ "$( find "$FLAGFILE" -newermt "$ref" | wc -l )" -gt 0 ] ; then
    info "flag file is in the future, no update needed"
  else
    touch -d "$max minutes" "$FLAGFILE"
  fi
elif [ -e "$FLAGFILE" ] && [ "$( find "$FLAGFILE" -newermt "$ref" | wc -l )" -gt 0 ] ; then
  info "flag file is in the future, no update needed"
else
  suspend_host
  touch -d "30 minutes" "$FLAGFILE"
fi

# vim:set et:
# vim:set ts=2:
# vim:set shiftwidth=2:

Reply to: