3 # Runs once a day to "fix" nodes in various ways
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
11 PATH=/sbin:/usr/sbin:$PATH
13 PIDFILE=/var/run/pl_mop.pid
16 if [ -f $PIDFILE ] ; then
17 if kill -0 `cat $PIDFILE` >/dev/null 2>&1 ; then
18 logger -p info -t pl_mom "$0 (`cat $PIDFILE`) already running"
24 # Clean up stale lock files
25 trap "rm -f $PIDFILE" EXIT
27 # Run a command and log its output to syslog
29 eval $* 2>&1 | logger -p info -t "pl_mom: $1"
32 # OpenSSH server 3.8 and above refuse login for "locked"
33 # accounts. Replace "!!" with "*" in /etc/shadow for all VServer
36 echo "* Fixing /etc/shadow"
39 for file in /etc/vservers/*.conf pl_admin.conf site_admin.conf ; do
40 slice=$(basename ${file%*.conf})
41 if grep -q "$slice:\!\!" /etc/shadow ; then
42 sed -i -e "s/$slice:\!\!:\(.*\)/$slice:*:\1/" /etc/shadow
47 # keep essential services running
49 for service in autofs sshd pl_sshd pl_mom pl_nm pl_conf proper ; do
50 echo "* Checking $service"
51 status=$(service $service status)
52 if [ $? -ne 0 ] || echo $status 2>&1 | grep -q stopped ; then
53 echo "* Restarting $service"
54 service $service start
59 # keep netflow running
61 echo "* Checking netflow"
62 echo "sudo /sbin/service netflow restart" | su - pl_netflow
63 if [ $? -ne 0 ] ; then
64 echo "* Restarting netflow"
65 service netflow-init start
66 vserver pl_netflow start
67 echo "sudo /sbin/service netflow restart" | su - pl_netflow
71 # kill all the processes running in slice contexts
73 vps -A | awk '(int($2) > 1) { system("vkill -c " $2 " -s 9 " $1); }'
74 # unmounts all the /proc and /dev/pts mounts in each vserver
76 while grep -q /vservers/ /proc/mounts && [ $tries -gt 0 ] ; do
78 # arizona_stork seems to generate some weird mount points of the form
79 # /vservers/arizona_stork/tmp/0.886421543959\040(deleted) that should be
80 # /vservers/arizona_stork/tmp/0.886421543959
81 awk '(/vservers\//) { sub(/\\040.*$/, ""); print "Unmounting " $2; system("umount " $2); }' /proc/mounts
85 # /vservers gets re-mounted read-only by the kernel if an ext3 journal
88 echo "* Fixing /vservers"
90 # test to see if /vservers is mounted read-only
91 mkdir -p /vservers/.vtmp
92 tmp=$(mktemp /vservers/.vtmp/fixit.XXXXXX)
93 if [ $? -eq 0 ] ; then
98 # kill all processes running in slice contexts
101 # stop the key automounter
105 pidfile=/var/run/vcached.pid
106 if [ -r "$pidfile" ] ; then
112 if umount /vservers ; then
113 # install expect if necessary
114 if ! rpm -q expect ; then
115 yum -y install expect
118 # tell expect to hit the 'y' key every time fsck asks
119 expect -c 'set timeout 3600; spawn fsck /dev/mapper/planetlab-vservers; expect "<y>?" { send "y\r"; exp_continue }'
121 # blow away the vserver cache
122 rm -rf /vservers/.vcache/*
127 echo "Unable to unmount /vservers!" >&2
130 # allow vcached to run again
133 # restart the key automounter
137 kill_duplicate_ssh() {
138 echo "* Killing stale duplicate SSH instances"
140 # count the number of SSH instances started by each slice
141 ps -C sshd -o command= |
144 while read instances sshd slice priv ; do
145 # kill all old instances
146 if [ $instances -gt 10 ] ; then
147 ps -C sshd -o pid=,start_time=,command= |
148 grep "$slice \[priv\]" |
149 while read pid start_time command ; do
150 start_time=$(date -d "$start_time" +%s)
151 min=$(date -d "6 hours ago" +%s)
152 if [ $start_time -lt $min ] ; then
153 echo "* Killing $slice sshd pid $pid"
161 # XXX kill zombie slices
163 # XXX reboot if boot state changes
173 run kill_duplicate_ssh