LJ Archive

Listing 2. cvschecker.pl

#!/usr/bin/perl

use File::Find;
use strict;
use vars qw(@Files);

my $email = 'root@localhost';
my $host = "192.168.10.5";
my $cvsroot = "/usr/local/hbids_cvs";
my $collectorCmd = "/usr/local/bin/collector.pl";

system "cvs -d $cvsroot checkout -ko $host";
chdir $host;
system "$collectorCmd"; ### get the files from $host

find(\&findfiles, "/home/mbr/${host}");

### check to see if any file has been changed
for my $file (@Files) {
    if (`cvs -d $cvsroot commit -m . $file`) {
        my $cvsfile = $file;
        $cvsfile =~ s|^\S+?$host|$host|;
        $cvsfile = $cvsroot ."/" . $cvsfile .",v";
        my $prerev = "";

        ### use rlog to get the previous revision
        open RLOG, "rlog $cvsfile|";
        my $found = 0;
        while (<RLOG>) {
            if (/^revision\s(\S+)/) {
                $prerev = $1;
                $found++;
            }
            last if ($found == 2);
       }
       close RLOG;

       ### get the changes using 'cvs diff'
       my $diff = `cvs -d $cvsroot diff -r       \
                                    $prerev $file`;
       open TMP, "> /tmp/change";
       print TMP $diff;
       close TMP;
       $file =~ s|^\S+?$host||g;

       ### send an email that contains the changes
       `mail -s "Changed file on $host: $file"
                             $email < /tmp/change`;
    }
}
exit 0;

### find all files in the local directory structure,
### but ignore directories and CVS files
sub findfiles() {
    my $file = $File::Find::name;
    unless (-d $file || $file =~ /CVS/) {
        push @Files, $file;
    }
    return;
}
LJ Archive