remove a debugging echo statement
[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 loadconfig samba
9
10 cmd="$1"
11 shift
12
13 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
14
15 # set default samba cleanup period - in minutes
16 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
17     SAMBA_CLEANUP_PERIOD=10
18 }
19
20 # function to see if ctdb manages winbind
21 check_ctdb_manages_winbind() {
22   [ -z "$CTDB_MANAGES_WINBIND" ] && {
23     secmode=`testparm -s --parameter-name=security 2> /dev/null`
24     case $secmode in
25         ADS|DOMAIN)
26             CTDB_MANAGES_WINBIND="yes";
27             ;;
28         *)
29             CTDB_MANAGES_WINBIND="no";
30             ;;
31     esac
32   }
33 }
34
35 ###########################
36 # periodic cleanup function
37 periodic_cleanup() {
38     # running smbstatus scrubs any dead entries from the connections
39     # and sessionid database
40     echo "Running periodic cleanup of samba databases"
41     smbstatus -n > /dev/null 2>&1 &
42 }
43
44 case $cmd in 
45      startup)
46         # create the state directory for samba
47         /bin/mkdir -p $CTDB_BASE/state/samba
48
49         # make sure samba is not already started
50         service smb stop > /dev/null 2>&1
51         killall -0 -q smbd && {
52             sleep 1
53             # make absolutely sure samba is dead
54             killall -q -9 smbd
55         }
56
57         # restart the winbind service
58         check_ctdb_manages_winbind
59         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
60                 service winbind stop > /dev/null 2>&1
61                 killall -0 -q winbindd && {
62                     sleep 1
63                     # make absolutely sure winbindd is dead
64                     killall -q -9 winbindd
65                 }
66                 service winbind start
67         }
68
69         # start Samba service. Start it reniced, as under very heavy load 
70         # the number of smbd processes will mean that it leaves few cycles for
71         # anything else
72         nice_service smb start
73         ;;
74         
75      takeip)
76         # nothing special for Samba
77         ;;
78
79      releaseip)
80         # nothing special for Samba
81         ;;
82
83      recovered)
84         # nothing special for Samba
85         exit 0
86         ;;
87
88      shutdown)
89         # shutdown Samba when ctdb goes down
90         service smb stop
91
92         # stop the winbind service
93         check_ctdb_manages_winbind
94         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
95                 service winbind stop
96         }
97         ;;
98
99      monitor)
100         # Create a dummy file to track when we need to do periodic cleanup
101         # of samba databases
102         [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
103                 touch $CTDB_BASE/state/samba/periodic_cleanup
104         }
105         [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
106                 # Cleanup the databases
107                 periodic_cleanup
108                 touch $CTDB_BASE/state/samba/periodic_cleanup
109         }
110
111         testparm -s 2>&1 | egrep '^WARNING|^ERROR|^Unknown' && {
112             echo "ERROR: testparm shows smb.conf is not clean"
113             exit 1
114         }
115
116         [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] && {
117                 smb_dirs=`testparm -s 2> /dev/null | egrep '^[[:space:]]*path = '  | cut -d= -f2`
118                 ctdb_check_directories "Samba" $smb_dirs        
119         }
120
121         smb_ports=`testparm -s --parameter-name="smb ports" 2> /dev/null`
122         ctdb_check_tcp_ports "Samba" $smb_ports
123
124         # check winbind is OK
125         check_ctdb_manages_winbind
126         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
127                 ctdb_check_command "winbind" "wbinfo -p"
128         }
129         ;;
130
131 esac
132
133 # ignore unknown commands
134 exit 0