Sunday, January 2, 2011

Perl script for finding Broken links in Linux

Description:

Creating Symbolic links across filesystem are very handy but at the same time, they can be a real pain when they got broken. This script checks the directories and report for any broken symbolic links.
I used the Perl module File::Find (comes default with any Perl package) for traversing through all the filenames in a specified directory and report the broken links. To be honest, I grabbed this logic from an online book on Perl programming.

Supported platforms: Any Unix platform with Perl version 5.x installed.

How to use:
On executing this script, it will prompt you to enter the filesystem path as parameter.
An example is shown below:

[root@evtlsb01 opt]# ./check_broken_link.pl

Enter the filesystem path (like /etc /opt /var) : /var /etc /usr /home

Disconnected Link => /var/lib/jbossas/server/production/lib/jboss-remoting.jar -> /usr/share/java/jboss-remoting.jar

Disconnected Link => /var/lib/jbossas/server/default/lib/jboss-remoting.jar -> /usr/share/java/jboss-remoting.jar

Disconnected Link => /etc/alternatives/jaxws_api -> /usr/share/java/glassfish-jaxws.jar

Disconnected Link => /etc/alternatives/jaxws_2_1_api -> /usr/share/java/glassfish-jaxws.jar

Disconnected Link => /etc/alternatives/jaxb_2_1_api -> /usr/share/java/glassfish-jaxb.jar

Disconnected Link => /etc/alternatives/jaxb_api -> /usr/share/java/glassfish-jaxb.jar

Disconnected Link => /usr/share/java/jaxws_api.jar -> /etc/alternatives/jaxws_api

Disconnected Link => /usr/share/java/jaxb_api.jar -> /etc/alternatives/jaxb_api

Disconnected Link => /usr/share/java/jaxws_2_1_api.jar -> /etc/alternatives/jaxws_2_1_api

Disconnected Link => /usr/share/jbossas/client/jboss-remoting.jar -> /usr/share/java/jboss-remoting.jar

Total number of Disconnected links:  10

[root@evtlsb01 opt]# 

How it works:

As I mentioned in the description, it uses the Perl Module File::Find for traversing through all the filenames in a directory we specify and for each file it calls the &wanted subroutine, which in turn uses the Stat function to match the symbolic link files which are broken.


Script

#!/usr/bin/perl
# This script checks the filenames in a directory and report for any broken symbolic links
# Author:  Ashok Raj
# Version 1.2
# Date: 01/06/2008

use File::Find ();
use vars qw/*name *dir *prune/;
my ($cnt,$i,$cnt_sub) = (0,0,0);
print "\n";
*name = *File::Find::name;
*dir = *File::Find::dir;
*prune = *File::Find::prune;
print "Enter the filesystem path (like /etc /opt /var) : ";
my $arr = <>;
chomp($arr);
print "\n";
my @inpts = split(/ /, $arr);

foreach(@inpts)
{
    File::Find::find({wanted => \&wanted}, $inpts[$i] ); # Calling wanted subroutine which use stat function to match broken links
    $cnt = $cnt_sub + $cnt;
    $i++;
    $cnt_sub = 0;
}
print "Total number of Disconnected links: $cnt \n\n";

sub wanted {
                     if (-l $_) {
                                     @stat = stat($_);
                                      if ($#stat == -1)
                                      {
                                        $flname = `ls -l $name`;
                                        ($flperm, $numlnk, $flown1, $flown2, $dt, $mnth, $tm1, $tm2, $cfnm, $ar, $dsfl) = split /\s+/, $flname;
                                        print "Disconnected Link => $cfnm $ar $dsfl\n\n";
                                        $cnt_sub++;
                                         }
                                     }
}

No comments:

Post a Comment

Popular Posts

About Me

My photo
The intent of this blog is to share my work experience and spread some smart solutions on Linux to System Administrators. I'm hoping the solutions shared in this Blog would be helpful and come as a handy for Viewers. Brief about me: I have 18+ years work experience in System and Cloud Administration domain, primarily works on VMware Cloud Products (vSphere, vCloud Director, vRealize Automation, NSX Adv. Load Balancer, vROps).