cope with non-standard install dirs in event scripts
[metze/ctdb/wip.git] / config / events.d / 50.samba
1 #!/bin/sh
2 # ctdb event script for Samba
3
4 PATH=/bin:/usr/bin:$PATH
5
6 . $CTDB_BASE/functions
7 loadconfig ctdb
8
9 cmd="$1"
10 shift
11
12 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
13
14 # set default samba cleanup period - in minutes
15 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
16     SAMBA_CLEANUP_PERIOD=10
17 }
18
19 ###########################
20 # periodic cleanup function
21 periodic_cleanup() {
22     # running smbstatus scrubs any dead entries from the connections
23     # and sessionid database
24     echo "`date` Running periodic cleanup of samba databases"
25     smbstatus -n > /dev/null 2>&1
26 }
27
28 case $cmd in 
29      startup)
30         # create the state directory for samba
31         /bin/mkdir -p $CTDB_BASE/state/samba
32
33         # wait for all shared directories to become available
34         smb_dirs=`testparm -s 2> /dev/null | egrep '^\s*path = '  | cut -d= -f2`
35         ctdb_wait_directories "Samba" $smb_dirs 
36
37         # make sure samba is not already started
38         service smb stop > /dev/null 2>&1
39         service winbind stop > /dev/null 2>&1
40         killall -0 -q smbd winbindd && {
41             sleep 1
42             # make absolutely sure samba is dead
43             killall -q -9 smbd winbindd
44         }
45
46         # start Samba service. Start it reniced, as under very heavy load 
47         # the number of smbd processes will mean that it leaves few cycles for
48         # anything else
49         service winbind start
50         nice service smb start
51
52         # wait for the Samba tcp ports to become available
53         smb_ports=`testparm -s --parameter-name="smb ports" 2> /dev/null`
54         ctdb_wait_tcp_ports "Samba" $smb_ports
55
56         # wait for winbind to be ready
57         ctdb_wait_command "winbind" "wbinfo -p"
58         ;;
59         
60      takeip)
61         # nothing special for Samba
62         ;;
63
64      releaseip)
65         # nothing special for Samba
66         ;;
67
68      recovered)
69         # nothing special for Samba
70         exit 0
71         ;;
72
73      shutdown)
74         # shutdown Samba when ctdb goes down
75         service smb stop
76         service winbind stop
77         ;;
78
79      monitor)
80         # Create a dummy file to track when we need to do periodic cleanup
81         # of samba databases
82         [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
83                 touch $CTDB_BASE/state/samba/periodic_cleanup
84         }
85         [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
86                 # Cleanup the databases
87                 periodic_cleanup
88                 touch $CTDB_BASE/state/samba/periodic_cleanup
89         }
90
91         testparm -s 2>&1 | egrep '^WARNING|^ERROR|^Unknown' && {
92             echo "`date` ERROR: testparm shows smb.conf is not clean"
93             exit 1
94         }
95
96         smb_dirs=`testparm -s 2> /dev/null | egrep '^\s*path = '  | cut -d= -f2`
97         ctdb_check_directories "Samba" $smb_dirs        
98
99         smb_ports=`testparm -s --parameter-name="smb ports" 2> /dev/null`
100         ctdb_check_tcp_ports "Samba" $smb_ports
101
102         # check winbind is OK
103         ctdb_check_command "winbind" "wbinfo -p"
104         ;;
105
106 esac
107
108 # ignore unknown commands
109 exit 0