#!/home/mui/proj/perl-devel/bin/perl ## # Perldaemon process to check a mount point. Send errors to logfile. ## use strict; use POSIX qw(setsid); ## # Signals to Trap and Handle ## $SIG{'INT' } = 'interrupt'; $SIG{'HUP' } = 'interrupt'; $SIG{'ABRT'} = 'interrupt'; $SIG{'QUIT'} = 'interrupt'; $SIG{'TRAP'} = 'interrupt'; $SIG{'STOP'} = 'interrupt'; $SIG{'TERM'} = 'interrupt'; # Globals my $DELAY = 300; my $MNT = "/mnt/net/ajaxx"; my $LOG = "/tmp/mntchkd.log"; my $PROGRAM = "mntchkd"; # Input validation bits if ((@ARGV <= 0) || (@ARGV >= 7)) { print "syntax error\n"; usage(); exit 1; } # Check for usage message first if ($ARGV[0] eq "usage") { usage(); exit 0; } # XXX Still need to add these funcs for (my $i = 0; $i <@ARGV; $i++) { if ($ARGV[$i] =~ /^-version|-V$/) { my $do_version = 1; } elsif ($ARGV[$i] eq '-usage') { my $do_usage = 1; } elsif ($ARGV[$i] eq '-m') { $MNT = $ARGV[++$i]; } elsif ($ARGV[$i] eq '-l') { $LOG = $ARGV[++$i]; } elsif ($ARGV[$i] eq '-d') { $DELAY = $ARGV[++$i]; } else { usage(); } } unless(my $pid = fork()) { exit if $pid; setsid; umask 0; my $date = longfmt(); appendfile($LOG, "$date Starting $PROGRAM"); while(1) { $date = longfmt(); if ( -d "$MNT" ) { appendfile($LOG, "$date Mount point $MNT present"); } else { appendfile($LOG, "$date Mount point $MNT missing"); } sleep $DELAY; } } ## # Append file: Append a string to a file. ## sub appendfile { my ($fp, $msg) = @_; if (open(FILE, ">>$fp")) { print FILE ("$msg\n"); close FILE; } } ## # Insert 0: Fix up date strings ## sub insert0 { my ($date) = shift; if ($date < 10) { return "0$date"; } return $date; } ## # Interrupt: Simple interrupt handler ## sub interrupt { appendfile($LOG, "caught @_ exiting"); print "caught @_ exiting\n"; die; } ## # Long format: Custom datestring for the logfile ## sub longfmt { my ($sec,$min,$hour,$mday,$mon,$year, $wday,$yday,$iddst) = localtime(time); my $datestring; $year += 1900; $mon++; $mon = insert0($mon); $mday = insert0($mday); $min = insert0($min); $datestring = "$year-$mon-$mday $hour:$min"; return($datestring); } sub usage { print "$PROGRAM [option argument][option argument]...\n"; print "$PROGRAM [-d delay][-m mountpoint][-l logfile]\n"; print "Options:\n"; print " -d delay Set check delaytime in seconds to delay\n"; print " -m directory Set mountpount to directory\n"; print " -l logfile Send log messages to logfile\n"; }