Runs once a day to "fix" nodes in various ways
[mom.git] / pl_mop.sh
1 #!/bin/bash
2 #
3 # Runs once a day to "fix" nodes in various ways
4 #
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2005 The Trustees of Princeton University
7 #
8 # $Id$
9 #
10
11 PATH=/sbin:/usr/sbin:$PATH
12
13 PIDFILE=/var/run/pl_mop.pid
14
15 # Record 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"
19         exit 1
20     fi
21 fi
22 echo $$ > $PIDFILE
23
24 # Clean up stale lock files
25 trap "rm -f $PIDFILE" EXIT
26
27 # Run a command and log its output to syslog
28 run() {
29     eval $* 2>&1 | logger -p info -t "pl_mom: $1"
30 }
31
32 # OpenSSH server 3.8 and above refuse login for "locked"
33 # accounts. Replace "!!" with "*" in /etc/shadow for all VServer
34 # accounts.
35 fix_etc_shadow() {
36     echo "* Fixing /etc/shadow"
37
38     shopt -s nullglob
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
43         fi
44     done
45 }
46
47 # keep essential services running
48 restart_services() {
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
55         fi
56     done
57 }
58
59 # keep netflow running
60 restart_netflow() {
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
68     fi
69 }
70
71 # kill all the processes running in slice contexts
72 vkillall() {
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
75     tries=10
76     while grep -q /vservers/ /proc/mounts && [ $tries -gt 0 ] ; do
77         tries=$(($tries -1))
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
82     done
83 }   
84
85 # /vservers gets re-mounted read-only by the kernel if an ext3 journal
86 # transaction aborts
87 fix_vservers() {
88     echo "* Fixing /vservers"
89
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
94         rm -f $tmp
95         return 0
96     fi
97
98     # kill all processes running in slice contexts
99     vkillall
100
101     # stop the key automounter
102     service autofs stop
103
104     # stop vcached
105     pidfile=/var/run/vcached.pid
106     if [ -r "$pidfile" ] ; then
107         kill $(cat $pidfile)
108     fi
109     touch $pidfile
110
111     # unmounts /vservers
112     if umount /vservers ; then
113         # install expect if necessary
114         if ! rpm -q expect ; then
115             yum -y install expect
116         fi
117
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 }'
120
121         # blow away the vserver cache
122         rm -rf /vservers/.vcache/*
123
124         # re-mount /vservers
125         mount /vservers
126     else
127         echo "Unable to unmount /vservers!" >&2
128     fi
129
130     # allow vcached to run again
131     rm -f $pidfile
132
133     # restart the key automounter
134     service autofs start
135 }
136
137 kill_duplicate_ssh() {
138     echo "* Killing stale duplicate SSH instances"
139
140     # count the number of SSH instances started by each slice
141     ps -C sshd -o command= |
142     grep " \[priv\]" |
143     sort | uniq -c |
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"
154                     kill -9 $pid
155                 fi
156             done
157         fi
158     done
159 }
160
161 # XXX kill zombie slices
162
163 # XXX reboot if boot state changes
164
165 run fix_vservers
166
167 run fix_etc_shadow
168
169 run restart_services
170
171 run restart_netflow
172
173 run kill_duplicate_ssh