Fixed indenting.
[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 # Parse PLC configuration
14 if [ -r /etc/planetlab/plc_config ] ; then
15     . /etc/planetlab/plc_config
16 else
17     PLC_SLICE_PREFIX="pl"
18 fi
19
20 PIDFILE=/var/run/pl_mop.pid
21
22 # Record PID
23 if [ -f $PIDFILE ] ; then
24     if kill -0 `cat $PIDFILE` >/dev/null 2>&1 ; then
25         logger -p info -t pl_mom "$0 (`cat $PIDFILE`) already running"
26         exit 1
27     fi
28 fi
29 echo $$ > $PIDFILE
30
31 # Clean up stale lock files
32 trap "rm -f $PIDFILE" EXIT
33
34 # Run a command and log its output to syslog
35 run() {
36     eval $* 2>&1 | logger -p info -t "pl_mom: $1"
37 }
38
39 # OpenSSH server 3.8 and above refuse login for "locked"
40 # accounts. Replace "!!" with "*" in /etc/shadow for all VServer
41 # accounts.
42 fix_etc_shadow() {
43     echo "* Fixing /etc/shadow"
44
45     shopt -s nullglob
46     for file in /etc/vservers/*.conf pl_admin.conf site_admin.conf ; do
47         slice=$(basename ${file%*.conf})
48         if grep -q "$slice:\!\!" /etc/shadow ; then
49             sed -i -e "s/$slice:\!\!:\(.*\)/$slice:*:\1/" /etc/shadow
50         fi
51     done
52 }
53
54 # keep essential services running
55 restart_services() {
56     for service in sshd pl_sshd swapmon nm proper ; do
57         echo "* Checking $service"
58         status=$(service $service status)
59         if [ $? -ne 0 ] || echo $status 2>&1 | grep -q stopped ; then
60             echo "* Restarting $service"
61             service $service start
62         fi
63     done
64 }
65
66 # keep netflow running
67 restart_netflow() {
68     echo "* Checking netflow"
69     echo "sudo /sbin/service netflow restart" | su - pl_netflow
70     if [ $? -ne 0 ] ; then
71         echo "* Restarting netflow"
72         service netflow-init start
73         vserver pl_netflow start
74         echo "sudo /sbin/service netflow restart" | su - pl_netflow
75     fi
76 }
77
78 # GPG keys are installed in /etc/pki/rpm-gpg by both the Boot Manager
79 # during initial installation, and by PlanetLabConf during daily
80 # updates. NodeUpdate imports the keys into the RPM database before
81 # running yum daily. vserver-reference copies and imports the keys
82 # into the reference images and system slices daily. The only parts of
83 # this process that are actually necessary, are the Boot Manager and
84 # vserver-reference. However, we do not want to force a re-install of
85 # all nodes, and we do not want to force an update of
86 # vserver-reference, so in the meantime, PlanetLabConf and NodeUpdate
87 # take care of getting the keys installed and imported in /, and this
88 # script takes care of getting them installed in the reference images
89 # and system slices, until we can get a new vserver-reference image
90 # pushed out.
91 update_vserver_reference() {
92     echo "* Updating VServer reference"
93
94     shopt -s nullglob
95
96     VROOTS="/vservers/vserver-reference /vservers/.vcache/* /vservers/${PLC_SLICE_PREFIX}_*"
97
98     # Copy configuration files from host to slices
99     for file in \
100     /etc/hosts /etc/resolv.conf /etc/yum.conf /etc/planetlab/node_id \
101     /etc/planetlab/plc_config* /etc/planetlab/php/* \
102     /etc/pki/rpm-gpg/* ; do
103       if [ -r $file ] ; then
104           for vroot in $VROOTS ; do
105               install -D -m 644 $file $vroot/$file
106           done
107       fi
108     done
109
110     # (Re)install GPG signing keys
111     if [ -d /etc/pki/rpm-gpg ] ; then
112         for vroot in $VROOTS ; do
113             chroot $vroot rpm --allmatches -e gpg-pubkey || :
114             chroot $vroot rpm --import /etc/pki/rpm-gpg/* || :
115         done
116     fi
117 }    
118
119 # kill all the processes running in slice contexts
120 vkillall() {
121     vps -A | awk '(int($2) > 1) { system("vkill -c " $2 " -s 9 " $1); }'
122     # unmounts all the /proc and /dev/pts mounts in each vserver
123     tries=10
124     while grep -q /vservers/ /proc/mounts && [ $tries -gt 0 ] ; do
125         tries=$(($tries -1))
126         awk '(/vservers\//) { sub(/\\040.*$/, ""); print "Unmounting " $2; system("umount " $2); }' /proc/mounts
127     done
128 }   
129
130 # /vservers gets re-mounted read-only by the kernel if an ext3 journal
131 # transaction aborts
132 fix_vservers() {
133     echo "* Fixing /vservers"
134
135     # test to see if /vservers is mounted read-only
136     mkdir -p /vservers/.vtmp
137     tmp=$(mktemp /vservers/.vtmp/fixit.XXXXXX)
138     if [ $? -eq 0 ] ; then
139         rm -f $tmp
140         return 0
141     fi
142
143     # kill all processes running in slice contexts
144     vkillall
145
146     # stop vcached
147     pidfile=/var/run/vcached.pid
148     if [ -r "$pidfile" ] ; then
149         kill $(cat $pidfile)
150     fi
151     touch $pidfile
152
153     # unmounts /vservers
154     if umount /vservers ; then
155         # install expect if necessary
156         if ! rpm -q expect ; then
157             yum -y install expect
158         fi
159
160         # tell expect to hit the 'y' key every time fsck asks
161         expect -c 'set timeout 3600; spawn fsck /dev/mapper/planetlab-vservers; expect "<y>?" { send "y\r"; exp_continue }'
162
163         # blow away the vserver cache
164         rm -rf /vservers/.vcache/*
165
166         # XXX re-mount /vservers
167         # mount /vservers
168
169         # shutdown instead to avoid clearing disk quotas
170         shutdown -r now "/vservers filesystem repaired, rebooting"
171     else
172         echo "Unable to unmount /vservers!" >&2
173     fi
174
175     # allow vcached to run again
176     rm -f $pidfile
177 }
178
179 kill_duplicate_ssh() {
180     echo "* Killing stale duplicate SSH instances"
181
182     # count the number of SSH instances started by each slice
183     ps -C sshd -o command= |
184     grep " \[priv\]" |
185     sort | uniq -c |
186     while read instances sshd slice priv ; do
187     # kill all old instances
188     if [ $instances -gt 10 ] ; then
189         ps -C sshd -o pid=,start_time=,command= |
190         grep "$slice \[priv\]" |
191         while read pid start_time command ; do
192             start_time=$(date -d "$start_time" +%s)
193             min=$(date -d "6 hours ago" +%s)
194             if [ $start_time -lt $min ] ; then
195                 echo "* Killing $slice sshd pid $pid"
196                 kill -9 $pid
197             fi
198         done
199     fi
200     done
201 }
202
203 kill_nm_inslice(){
204     pids=$(vps aux | awk '$1 != "root" && $14 == "/usr/share/NodeManager/nm.py" {print $2}')
205     for pid in $pids ; do
206         line=$(vps aux | grep $pid)
207         echo NM found in slice. Killing PID $pid
208         echo $line
209         kill -9 $pid
210     done
211 }
212
213 kill_nonroot_nm(){
214     # For whatever reason, Some NM's, after fork and chcontext...don't chcontext.  Kill them.
215     pids=$(ps aux | awk '$1 != "root" && $12 == "/usr/share/NodeManager/nm.py" {print $2}')
216     for pid in $pids ; do
217         line=$(ps aux | grep $pid)
218         echo NM found not belonging to root. Killing PID $pid
219         echo $line
220         kill -9 $pid
221     done
222 }
223
224 kill_multi_nm(){
225     # if there is more than one nm running around, kill them, then nm restart
226     pids=$(ps aux | awk '$1 == "root" && $12 == "/usr/share/NodeManager/nm.py" {print $2}')
227     i=0
228     for pid in $pids ; do
229         i=$[$i+1]
230     done
231     if [ $i -gt 1 ] ; then
232         # stop nm
233         echo "More than 1 NM found belonging to root.  Restarting NM."
234         /etc/init.d/nm stop 
235         pids=$(ps aux | awk '$1 == "root" && $12 == "/usr/share/NodeManager/nm.py" {print $2}')
236         for pid in $pids ; do
237             kill -9 $pid
238         done
239         /etc/init.d/nm start
240     fi
241 }
242 # XXX kill zombie slices
243
244 # XXX reboot if boot state changes
245 run kill_nonroot_nm
246
247 run kill_nm_inslice
248
249 run kill_multi_nm
250
251 run fix_vservers
252
253 run fix_etc_shadow
254
255 run restart_services
256
257 run restart_netflow
258
259 run kill_duplicate_ssh
260
261 run update_vserver_reference
262