"Fossies" - the Fresh Open Source Software Archive

Member "checksuite-3.3/checklinks" (3 Jun 2010, 5342 Bytes) of package /linux/privat/old/checksuite-3.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl -w
    2 ########################################################
    3 # Link Checker by Larry Long - larry@djslyde.com       #
    4 # [checklinks] - 8/9 programs in checksuite v3.3       #
    5 #                                                      #
    6 # This will query the specified directory or mount     #
    7 # for broken symbolic links. It can also remove the    #
    8 # broken links if specified. This can log and/or       #
    9 # e-mail a notification.                               #
   10 ########################################################
   11 use strict;
   12 use Getopt::Std;
   13 use Net::SMTP;
   14 
   15 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin';
   16 
   17 # Options: -h (help) -q (query) -r (remove) -d (directory) -e (email) -p (pager)
   18 my %opt;getopts('hqrlod:e:p:',\%opt);
   19 usage_info() unless defined @ARGV;
   20 usage_info() if exists $opt{h};
   21 
   22 # Localize variables throughout the rest of the program
   23 my($dir,$info,@findcmd,$line,$symlink,@data,$data,$link,$title,$question,@note,$notify,$logfile,$logsnip,$total,$email,$subject,@pnote,$pmail,$psubject,$host,$logdate,$script,$findtotal,@bad,$bad,$badlink);
   24 
   25 # Are we root?
   26 if($> != 0)
   27   {
   28   print STDERR "\n$0: This program HAS to be ran as root!!!\n\nPlease su to root
   29  or use 'sudo $0'!\n";
   30   exit 2;
   31   }
   32 
   33 # Define variables
   34 $dir = $opt{d};$dir = '/' unless defined $opt{d};
   35 $email = $opt{e};$email = 'root@localhost' unless defined $opt{e};
   36 $pmail = $opt{p};
   37 $host = `/bin/hostname`;
   38 $logfile = "/var/log/checksuite.d/checklinks";
   39 $logdate = `date '+%m/%d/%Y %H:%M:%S' `;
   40 $script = " - [checksuite] checklinks\n";
   41 $logsnip = "----\n";
   42 $notify = 0;
   43 $subject = "[checksuite] advisory - symbolic links check on $host";
   44 $psubject = "[checksuite] checklinks";
   45 chomp $host;chomp $logdate;chomp $subject;chomp $psubject;
   46 
   47 # Do the dirty work!
   48 push(@note, "\n");
   49 if(exists $opt{q})
   50   {
   51   $findtotal = `/usr/bin/find $dir -path '/var/named/chroot' -prune -o -path '/proc' -prune -o -path '/media' -prune -o -type l -print0 | xargs -0 file | grep 'broken sym' | wc -l`;
   52   if($findtotal >= 1)
   53     {
   54     @findcmd = `/usr/bin/find $dir -path '/var/named/chroot' -prune -o -path '/proc' -prune -o -path '/media' -prune -o -type l -print0 | xargs -0 file | grep 'broken sym'`;
   55     push(@note, "\nBroken Symbolic Links Check for $dir:\n\n");
   56     foreach $line (@findcmd)
   57       {
   58       @data = split(/\:/, $line);
   59       $symlink = $data[0];$symlink =~ s/\://;
   60       $link = $data[1];$link =~ s/\`//;$link =~ s/\'//;
   61       @bad = split(/\s+/, $link);
   62       $badlink = $bad[5];
   63       push(@note, "$symlink -> $badlink\n");
   64       }
   65     push(@note, "\nCheck Completed.\n");
   66     push(@note, "You can manually remove any broken links or run 'checklinks -h' for more information.\n");
   67     $notify++;
   68     } 
   69   }
   70 elsif(exists $opt{r})
   71   {
   72   push(@note, "\nBeginning Removal of Bad Symbolic Links for $dir:\n\n");
   73   @findcmd = `/usr/bin/find $dir -path '/var/named/chroot' -prune -o -path '/proc' -prune -o -path '/media' -prune -o -type l -print0 | xargs -0 file | grep 'broken sym'`;
   74   foreach $line (@findcmd)
   75     {
   76     @data = split(/\:/, $line);
   77     $symlink = $data[0];$symlink =~ s/\://;
   78     system("rm -f '$symlink'");
   79     push(@note, "$symlink has been removed.\n");
   80     }
   81   push(@note, "\nRemovals Completed.\n");
   82   $notify++;
   83   }
   84 
   85 # Define where the output goes
   86 if($notify > 0)
   87   {
   88   log_data() if exists $opt{l};
   89   email_data() if exists $opt{e};
   90   screen_data() if exists $opt{o};
   91   pager_data() if exists $opt{p};
   92   }
   93 
   94 #Subroutines
   95 sub usage_info
   96   {
   97   my $info = "
   98 Usage: $0 [-h | -lo] [-q] [-r] [-d <directory>] [-e <email>] [-p <email>]
   99 Options:
  100 -h      display help
  101 -l      log the output to /var/log/checksuite.d/checklinks
  102 -o      force output to screen
  103 -q      query the filesystem for broken links
  104 -r      remove broken links
  105 -d      specify the directory to check (/) is default
  106 -e      e-mail the output to a specified e-mail address
  107 -p      send shortened output to a pager or cell phone
  108 Where:
  109 <directory>     specific directory - default is (/)
  110 <email>         e-mail address of the recipient of the notification
  111         default is 'root'
  112 \n";
  113   die $info;
  114   }
  115 
  116 sub log_data
  117    {
  118    open(LOG, ">>$logfile") or die "Uh oh! Can't open logfile!\n";
  119    print LOG $logdate,$script,@note,$logsnip;
  120    close(LOG);
  121    }
  122 
  123 sub screen_data
  124    {
  125    print STDERR @note;
  126    }
  127 
  128 sub email_data
  129    {
  130    my $smtp = Net::SMTP->new($host);
  131    if(! ref($smtp))
  132       {
  133       log_die("Cannot connect to SMTP\n");
  134       }
  135    $smtp->mail($email);
  136    $smtp->to($email);
  137    $smtp->data();
  138    $smtp->datasend("To: " . $email . "\n");
  139    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  140    $smtp->datasend("Return-Path: " . $email. "\n");
  141    $smtp->datasend("Subject: " . $subject . "\n");
  142    $smtp->datasend("\n");
  143    $smtp->datasend(@note);
  144    $smtp->datasend();
  145    $smtp->quit();
  146    }
  147 
  148 sub pager_data
  149    {
  150    my $smtp = Net::SMTP->new($host);
  151    if(! ref($smtp))
  152       {
  153       log_die("Cannot connect to SMTP\n");
  154       }
  155    $smtp->mail($pmail);
  156    $smtp->to($pmail);
  157    $smtp->data();
  158    $smtp->datasend("To: " . $pmail . "\n");
  159    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  160    $smtp->datasend("Return-Path: " . $pmail. "\n");
  161    $smtp->datasend("Subject: " . $psubject . "\n");
  162    $smtp->datasend("\n");
  163    $smtp->datasend(@pnote);
  164    $smtp->datasend();
  165    $smtp->quit();
  166    }
  167