Tagging module Mom - Mom-2.3-1
[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 fprobe-ulog codemux; 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 # kill all the processes running in slice contexts
67 vkillall() {
68     vps -A | awk '(int($2) > 1) { system("vkill -c " $2 " -s 9 " $1); }'
69     # unmounts all the /proc and /dev/pts mounts in each vserver
70     tries=10
71     while grep -q /vservers/ /proc/mounts && [ $tries -gt 0 ] ; do
72         tries=$(($tries -1))
73         awk '(/vservers\//) { sub(/\\040.*$/, ""); print "Unmounting " $2; system("umount " $2); }' /proc/mounts
74     done
75 }   
76
77 # /vservers gets re-mounted read-only by the kernel if an ext3 journal
78 # transaction aborts
79 fix_vservers() {
80     echo "* Fixing /vservers"
81
82     # test to see if /vservers is mounted read-only
83     mkdir -p /vservers/.vtmp
84     tmp=$(mktemp /vservers/.vtmp/fixit.XXXXXX)
85     if [ $? -eq 0 ] ; then
86         rm -f $tmp
87         return 0
88     fi
89
90     # kill all processes running in slice contexts
91     vkillall
92
93     # stop vcached
94     pidfile=/var/run/vcached.pid
95     if [ -r "$pidfile" ] ; then
96         kill $(cat $pidfile)
97     fi
98     touch $pidfile
99
100     # unmounts /vservers
101     if umount /vservers ; then
102         # install expect if necessary
103         if ! rpm -q expect ; then
104             yum -y install expect
105         fi
106
107         # tell expect to hit the 'y' key every time fsck asks
108         expect -c 'set timeout 3600; spawn fsck /dev/mapper/planetlab-vservers; expect "<y>?" { send "y\r"; exp_continue }'
109
110         # blow away the vserver cache
111         rm -rf /vservers/.vcache/*
112
113         # XXX re-mount /vservers
114         # mount /vservers
115
116         # shutdown instead to avoid clearing disk quotas
117         shutdown -r now "/vservers filesystem repaired, rebooting"
118     else
119         echo "Unable to unmount /vservers!" >&2
120     fi
121
122     # allow vcached to run again
123     rm -f $pidfile
124 }
125
126 kill_duplicate_ssh() {
127     echo "* Killing stale duplicate SSH instances"
128
129     # count the number of SSH instances started by each slice
130     ps -C sshd -o command= |
131     grep " \[priv\]" |
132     sort | uniq -c |
133     while read instances sshd slice priv ; do
134     # kill all old instances
135     if [ $instances -gt 10 ] ; then
136         ps -C sshd -o pid=,start_time=,command= |
137         grep "$slice \[priv\]" |
138         while read pid start_time command ; do
139             start_time=$(date -d "$start_time" +%s)
140             min=$(date -d "6 hours ago" +%s)
141             if [ $start_time -lt $min ] ; then
142                 echo "* Killing $slice sshd pid $pid"
143                 kill -9 $pid
144             fi
145         done
146     fi
147     done
148 }
149
150 kill_nm_inslice(){
151     pids=$(vps aux | awk '$1 != "root" && $14 == "/usr/share/NodeManager/nm.py" {print $2}')
152     for pid in $pids ; do
153         line=$(vps aux | grep $pid)
154         echo NM found in slice. Killing PID $pid
155         echo $line
156         vkill -9 $pid
157     done
158 }
159
160 kill_nonroot_nm(){
161     # For whatever reason, Some NM's, after fork and chcontext...don't chcontext.  Kill them.
162     pids=$(ps aux | awk '$1 != "root" && $12 == "/usr/share/NodeManager/nm.py" {print $2}')
163     for pid in $pids ; do
164         line=$(ps aux | grep $pid)
165         echo NM found not belonging to root. Killing PID $pid
166         echo $line
167         kill -9 $pid
168     done
169 }
170
171 kill_multi_nm(){
172     # if there is more than one nm running around, kill them, then nm restart
173     pids=$(ps aux | awk '$1 == "root" && $12 == "/usr/share/NodeManager/nm.py" {print $2}')
174     i=0
175     for pid in $pids ; do
176         i=$[$i+1]
177     done
178     if [ $i -gt 1 ] ; then
179         # stop nm
180         echo "More than 1 NM found belonging to root.  Restarting NM."
181         /etc/init.d/nm stop 
182         pids=$(ps aux | awk '$1 == "root" && $12 == "/usr/share/NodeManager/nm.py" {print $2}')
183         for pid in $pids ; do
184             kill -9 $pid
185         done
186         /etc/init.d/nm start
187     fi
188 }
189 # XXX kill zombie slices
190
191 run restart_services
192
193
194 run kill_nonroot_nm
195
196 run kill_nm_inslice
197
198 run kill_multi_nm
199
200 run fix_vservers
201
202 run fix_etc_shadow
203
204 run kill_duplicate_ssh