Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

diffsquid - find the differences in Squid configuration files

by grinder (Bishop)
on Aug 07, 2002 at 15:06 UTC ( #188343=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility scripts
Author/Contact Info grinder
Description:

Analyse two squid configuration files, and report parameters that are present in one file but not in the other, or have different values. Also attempt to identify valid parameter names in the comments and report on those as well (useful when new versions are released).

#! /usr/bin/perl -w

use strict;
use constant MISSING => 'not specified';

my $first  = shift or die "No first file given on command line.\n";
my $second = shift or die "No second file given on command line.\n";

open F, $first or die "Cannot open $first for input: $!\n";
my %f1 = contents( <F> );
close F;

open F, $second or die "Cannot open $second for input: $!\n";
my %f2 = contents( <F> );
close F;

for( sort {$f1{$a}->[0] <=> $f1{$b}->[0]} keys %f1 ) {
    if( !exists $f2{$_} ) {
        print "$first($f1{$_}->[0]) $_ not present in $second\n";
    }
    elsif( defined $f1{$_}->[1] ) {
        if( !defined $f2{$_}->[1] ) {
            print "$first($f1{$_}->[0]) $_=$f1{$_}->[1] [$second($f2{$
+_}->[0]) not specified]\n";
        }
        elsif( $f1{$_}->[1] ne $f2{$_}->[1] ) {
            print "$first($f1{$_}->[0]) $_=$f1{$_}->[1] [$second($f2{$
+_}->[0]) differs: $f2{$_}->[1]]\n";
        }
    }
}

for( sort {$f2{$a}->[0] <=> $f2{$b}->[0]} keys %f2 ) {
    if( !exists $f1{$_} ) {
        print "$second($f2{$_}->[0]) $_ not present in $first\n";
    }
}

sub contents {
    my %param;
    my %tag;
    my $line = 0;
    for( @_ ) {
        ++$line;
        /^\s*#\s*TAG:\s+(\S+)/ and $tag{$1} = $line;
        s/#.*$//;
        s/\s+$//;
        next unless $_;
        my( $key, $value ) = split( / /, $_, 2 );
        $param{$key} = [$line, $value];
    }
    for( keys %tag ) {
        $param{$_} = [$tag{$_}, undef] unless $param{$_} ;
    }
    %param;
}
=over 1 
=head1 NAME

diffsquid - compare squid configuration files

=head1 SYNOPSIS

B<diffsquid> filespec filespec

=head1 DESCRIPTION

Analyse two squid configuration files, and report parameters that are
present in one file but not in the other, or have different values.
Also attempt to identify valid parameter names in the comments and
report on those as well (useful when new versions are released).

=head1 OPTIONS

=over 5

None.

=head1 DETAILS

By default, a squid configuration file "fresh out the box" contains
no values, as sensible default values are chosen for everything.
Fortunately most, if not all, parameters and their possible values
appear in the comments. As an added bonus, they are easily greppable
since those lines contain the string 'TAG:'.

This script takes two filenames, which should be Squid configuration
files, and isolates the TAG: comment lines to extract the parameter
names, and extracts all the parameters with explicit values.

It then compares the two sets of paramters and displays the
differences: the parameters than exist in both files but have
different settings, and parameters that exist in one file but not
the other.

=head1 EXAMPLES

Here is an excerpt of the output when run with against a working
2.4.STABLE3 config and a freshly downloaded copy from 2.4.STABLE7.

 squid.conf-2.4.3(1051) connect_timeout=45 seconds [squid.conf-2.4.7(1
+200) not specified]
 squid.conf-2.4.3(1283) http_access=allow all [squid.conf-2.4.7(1462) 
+differs: deny all]
 squid.conf-2.4.3(1303) miss_access=allow all [squid.conf-2.4.7(1478) 
+not specified]

These three lines indicate that two parameters are present in the firs
+t
file, but not in the second file. One parameter (http_access) is prese
+nt
in both files, but the value is different. The number in parentheses a
+re
line numbers.

 squid.conf-2.4.7(670) log_ip_on_direct not present in squid.conf-2.4.
+3
 squid.conf-2.4.7(707) referer_log not present in squid.conf-2.4.3
 squid.conf-2.4.7(786) ftp_sanitycheck not present in squid.conf-2.4.3

These lines indicate new parameters in the second file that do not
exist in the first file. This is probably an indication of new functio
+nality
that has been added to Squid in subsequent versions.

=head1 BUGS

The script is only as good as the comments it finds in the configurati
+on
file.

=head1 COPYRIGHT

Copyright (c) 2002 David Landgren.

This script is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 AUTHOR

 David "grinder" Landgren
 grinder on perlmonks (http://www.perlmonks.org/)
 eval {join chr(64) => qw[landgren bpinet.com]}

=cut

Comment on diffsquid - find the differences in Squid configuration files
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://188343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-17 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (99 votes), past polls