- fix how pl_conf is checked and restarted
[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: pl_mop.sh,v 1.1 2005/10/11 17:34:57 mlhuang Exp $
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 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 # keep pl_conf running
72 restart_pl_conf() {
73     echo "* Checking pl_conf"
74     vserver pl_conf exec /sbin/service pl_conf status >/dev/null 2>&1
75     if [ $? -ne 0 ] ; then
76         echo "* Restarting pl_conf"
77         vserver pl_conf restart
78     fi
79 }
80
81 # kill all the processes running in slice contexts
82 vkillall() {
83     vps -A | awk '(int($2) > 1) { system("vkill -c " $2 " -s 9 " $1); }'
84     # unmounts all the /proc and /dev/pts mounts in each vserver
85     tries=10
86     while grep -q /vservers/ /proc/mounts && [ $tries -gt 0 ] ; do
87         tries=$(($tries -1))
88         # arizona_stork seems to generate some weird mount points of the form
89         # /vservers/arizona_stork/tmp/0.886421543959\040(deleted) that should be
90         # /vservers/arizona_stork/tmp/0.886421543959
91         awk '(/vservers\//) { sub(/\\040.*$/, ""); print "Unmounting " $2; system("umount " $2); }' /proc/mounts
92     done
93 }   
94
95 # /vservers gets re-mounted read-only by the kernel if an ext3 journal
96 # transaction aborts
97 fix_vservers() {
98     echo "* Fixing /vservers"
99
100     # test to see if /vservers is mounted read-only
101     mkdir -p /vservers/.vtmp
102     tmp=$(mktemp /vservers/.vtmp/fixit.XXXXXX)
103     if [ $? -eq 0 ] ; then
104         rm -f $tmp
105         return 0
106     fi
107
108     # kill all processes running in slice contexts
109     vkillall
110
111     # stop the key automounter
112     service autofs stop
113
114     # stop vcached
115     pidfile=/var/run/vcached.pid
116     if [ -r "$pidfile" ] ; then
117         kill $(cat $pidfile)
118     fi
119     touch $pidfile
120
121     # unmounts /vservers
122     if umount /vservers ; then
123         # install expect if necessary
124         if ! rpm -q expect ; then
125             yum -y install expect
126         fi
127
128         # tell expect to hit the 'y' key every time fsck asks
129         expect -c 'set timeout 3600; spawn fsck /dev/mapper/planetlab-vservers; expect "<y>?" { send "y\r"; exp_continue }'
130
131         # blow away the vserver cache
132         rm -rf /vservers/.vcache/*
133
134         # re-mount /vservers
135         mount /vservers
136     else
137         echo "Unable to unmount /vservers!" >&2
138     fi
139
140     # allow vcached to run again
141     rm -f $pidfile
142
143     # restart the key automounter
144     service autofs start
145 }
146
147 kill_duplicate_ssh() {
148     echo "* Killing stale duplicate SSH instances"
149
150     # count the number of SSH instances started by each slice
151     ps -C sshd -o command= |
152     grep " \[priv\]" |
153     sort | uniq -c |
154     while read instances sshd slice priv ; do
155         # kill all old instances
156         if [ $instances -gt 10 ] ; then
157             ps -C sshd -o pid=,start_time=,command= |
158             grep "$slice \[priv\]" |
159             while read pid start_time command ; do
160                 start_time=$(date -d "$start_time" +%s)
161                 min=$(date -d "6 hours ago" +%s)
162                 if [ $start_time -lt $min ] ; then
163                     echo "* Killing $slice sshd pid $pid"
164                     kill -9 $pid
165                 fi
166             done
167         fi
168     done
169 }
170
171 # XXX kill zombie slices
172
173 # XXX reboot if boot state changes
174
175 run fix_vservers
176
177 run fix_etc_shadow
178
179 run restart_services
180
181 run restart_pl_conf
182
183 run restart_netflow
184
185 run kill_duplicate_ssh