Sample sendmail Start Script for the kitche Service

#!/contrib/bin/perl
#
#/var/adm/use/bin/kitche_start_sendmail.pl
#
# kitche_start_sendmail.pl starts the primary mail service when the kitche
# service starts. After this script runs, the system running the kitche service
# should be as follows:
# 1. /var/adm/sendmail_primary.cf copied into /var/adm/sendmail/sendmail.cf
# 2. symbolic link from /usr/var/spool/mqueue.primary to /usr/var/ase/mnt ->
#    /kitche/usr/var/spool/mqueue
# 3. symbolic link from /usr/var/spool/mqueue to /usr/var/spool/mqueue.primary
# 4. if system running xirtlu service prior to kitche service, move any 
#    "stranded" mail messages from mqueue.backup to mqueue.primary
# 5. sendmail started.
#
$| = 1;

# lock types for flock routine
#
$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;

# Set a lock on /tmp/.ASE_RUNNING_SENDMAIL to prevent xirtlu_start_sendmail.pl
# from running at the same time
#
$sendmail_lockfile = "/tmp/.ASE_RUNNING_SENDMAIL";
open(FILE, "> $sendmail_lockfile") || die "Couldn't open $sendmail_lockfile\n";
lock(FILE);

$mail_service = "kitche";
$bmail_service = "xirtlu";
#$cnx_ck = $mh_ck = $bmh_ck = $im_ck1 = $im_ck2 = "0";
$mh_ck = $bmh_ck = $im_ck1 = $im_ck2 = "0";
$kase1_count = 0;
$mqueue_real = "/usr/var/ase/mnt/$mail_service/usr/var/spool/mqueue";
$mqueue_primary = "/usr/var/spool/mqueue.primary";
$mqueue_backup = "/usr/var/spool/mqueue.backup";
$mqueue = "/usr/var/spool/mqueue";
chop($hostname = `/sbin/hostname`);
($host, $domain) = split(/\./,$hostname);
$mchost = "mc0" . $host;

# Don't bother running script if host not running kitche service
#
$kitche_ck = `/usr/sbin/netstat -i | egrep -v 'kitche1|kitche2' | grep $mail_service 2> /dev/null`;
die "$mail_service not running on $host.\n" if ($kitche_ck eq "");

# Stop sendmail if it happens to be running
#
$mqueue_mounted = `/sbin/mount -t advfs,ufs | grep $mqueue_real`;
$sendmail_running = `ps agx | grep -v grep | grep 'accepting connections'`;
if ( $sendmail_running && $mqueue_mounted ) { `/sbin/init.d/sendmail stop`;}

# Ensure that this host is a member of the cluster
#
#open(CNX,"/usr/sbin/cnxshow |") || die "\nExiting: Could not execute cnxshow command\n\n";
#while () {
#   chop $_;
#   next unless (/\s+$mchost\s+/);
#   ($cl_hostname, $cl_if, $cs_id, $incarnation, $comm, $member) = split(/\s+/,$_);
#   $cnx_ck = "1" if ($cl_if eq $mchost && $comm eq "Yes" && $member eq "Yes");
#}
#close CNX;

# Bow out if host is not active in cluster
#
#die "\nExiting: $mchost not active in cluster\n\n" if ($cnx_ck eq "0");

# Ensure kitche service is running on this host
#
while ($mh_ck eq "0" && $kase1_count lt 10)
{
  sleep 1;
  $mh = `/usr/sbin/netstat -i` || die "Could not perform netstat\n";
  $mh_ck = "1" if (grep(/\s+$mail_service\s+/,$mh)) || die "\nExiting: $mail_service not running on $host\n\n";
  $kase1_count++;
}
die "\nExiting: $mail_service not running on $host\n\n" if ($kase1_count == 10);

# ogated has been known to die during relocates. Run a check to make sure 
# it is running
#
$ogated_running = `ps agx | grep -v grep | grep ogated 2> /dev/null`;
if ( ! $ogated_running ) { `/sbin/init.d/ogated stop; /sbin/init.d/ogated start`; }

# One more check of ogated
#
$ogated_running = `ps agx | grep -v grep | grep ogated 2> /dev/null`;
if ( ! $ogated_running ) { print "**WARNING** ogated not starting.\n"; }

# What mqueue directories are mounted? mqueue from kitche? mqueue.backup 
# from xirtlu?
#
$mqueue_mounted = `/sbin/mount -t advfs,ufs | grep $mqueue_real`;
$bmqueue_mounted = `/sbin/mount -t advfs,ufs | grep $mqueue_backup`;

# Is xirtlu backup mail service running on this system? If so, merging of 
# mqueue.backup into mqueue.primary needs to occur
#
$bmh = `/usr/sbin/netstat -i` || die "Could not perform netstat\n";
$bmh_ck = "1" if (grep(/\s+$bmail_service\s+/,$bmh));

# Need another check as mqueue should be mounted through kitche service. 
# If mqueue is not mounted then something else is wrong and no point in 
# going on.
#
if ( ! $mqueue_mounted && $ogated_running ) { die "\nExiting. $mqueue_real not mounted.\n\n"; }

# since this is a start, change sendmail.cf to the primary file.
#
`/usr/bin/cp -p /var/adm/sendmail/sendmail_primary.cf /var/adm/sendmail/sendmail.cf`;

# remove sym links that points to the primary mqueue just in case they 
# are out of date
#
`/usr/bin/rm $mqueue`;
`/usr/bin/rm $mqueue_primary`;

# add the sym links to the primary mqueue.
#
`/usr/bin/ln -s $mqueue_real $mqueue_primary`;
`/usr/bin/ln -s $mqueue_primary $mqueue`;

# if system is running the xirtlu service and mqueue.backup is mounted,
# move files into mqueue.primary
#
if ( $bmh_ck eq "1" && $bmqueue_mounted ne "") {
  print "Flushing $mqueue_backup directory.\n";
  chdir("$mqueue_backup") || die "Can't chdir $mqueue_backup.\n";
  foreach (<*>) {
    next if (/^xf/ || /^tf/ || /^.nfs/ || /quota.group/ || /quota.user/ || /.tag
s/);
    $file = $_;
    $dup_file = `/sbin/ls $file $mqueue.primary 2> /dev/null`;
    $file =~ s/AA/AZ/ if ($dup_file);
    `/usr/bin/mv $file $mqueue_primary`;
  }
}

# check that mountd is still running
#
$mountd_ck = `ps agx | grep mountd | grep -v grep 2> /dev/null`;
`/usr/sbin/mountd -i -n` if (! "$mountd_ck");

# start sendmail
#
`/sbin/init.d/sendmail start`;
print "\n Sendmail running as primary.\n\n" if ( $sendmail_running && $mqueue_mounted && $ogated_running );
print "Done.\n";

# If the kitche service relocated and the other system is up and running
# the xirtlu service, we need to start sendmail again that was stopped
# when the kitche moved.
#
$other_host = mc0kitche1 if (grep(/kitche2/,$host));
$other_host = mc0kitche2 if (grep(/kitche1/,$host));

$other_host_ping = `/sbin/ping -c 2 $other_host`;
$other_host_up = "1" if (grep(/round-trip/,$other_host_ping));

$bmail_service_ping = `/sbin/ping -c 2 $bmail_service`;
$bmail_service_up = "1" if (grep(/round-trip/,$bmail_service_ping));

$bmail_service_ck2 = `/usr/sbin/netstat -i | grep $bmail_service 2> /dev/null`;

$other_sendmail_up = "0";
$other_sendmail = `rsh $other_host ps agxww | grep 'sendmail: accepting connections'| grep -v grep 2> /dev/null` if ($other_host_up eq "1");
$other_sendmail_up = "1" if ($other_host_up eq "1" && $other_sendmail ne "");

# only under the following conditions do we want to start mail services:
# 1. the other host and is up
# 2. the xirtlu service appears to be running
# 3. the xirtlu service is not running on this host
# 4. mail services do not appear to be running on the other host
#
if ($other_host_up eq "1" && $bmail_service_up eq "1" && $bmail_service_ck2 eq "" && $other_sendmail_up eq "0") {
  print "Starting mail services on $other_host.\n";
  system("rsh -n $other_host /usr/var/adm/ues/bin/xirtlu_start_sendmail.pl");
}

# All is done. Now unlock the file so that xirtlu_start_sendmail.pl can run
# if it is waiting in the wings.
#
unlock(FILE);
close FILE;

#
# Subroutines
#
sub lock {
  flock(FILE, $LOCK_EX);
  print "$sendmail_lockfile locked.\n";
}

sub unlock {
  flock(FILE, $LOCK_UN);
  print "$sendmail_lockfile unlocked.\n";
}

#
# Following subroutines are not used in this script, but kept around for 
# reference.
#
sub do_start
{
# stop sendmail so we can mount the imap dir and change .cf files.
  `/sbin/init.d/sendmail stop`;

# run fuser to insure noone is setting in the /var/spool/imap so we can mount it.
  `/usr/sbin/fuser -k -c /usr/var/spool/imap`;

# now mount the imap directory.
  `/usr/sbin/mount /usr/var/spool/imap`;

# since this is a start, change sendmail.cf to the primary file.
  `/usr/bin/cp -p /var/adm/sendmail/sendmail_primary.cf /var/adm/sendmail/sendmail.cf`;

# remove the link that points to the primary mqueue.
  `/usr/bin/rm /usr/var/spool/mqueue`;

# add the link to the backup mqueue.
  `/usr/bin/ln -s /usr/var/spool/mqueue.primary /usr/var/spool/mqueue`;

# start sendmail
  `/sbin/init.d/sendmail start`;
}

sub do_stop
{
# stop sendmail so we can umount the imap dir and change .cf files.
  `/sbin/init.d/sendmail stop`;

# run fuser to insure noone is setting in the /var/spool/imap so we can umount it.
  `/usr/sbin/fuser -k -c /usr/var/spool/imap`;

# now umount /var/spool/imap.
  `/usr/sbin/umount /usr/var/spool/imap`;
}