Tip: Perl Script to Archive Log Files

This is a simple Perl script I wrote to search a given directory for *.log files, tag with the current date and archive them.

#!/usr/bin/perl

$DIR=”/var/www/html/sites/logs”;
$DATE=`date +%F`;
chomp $DATE;

@log = `/usr/bin/find $DIR -type f -name “*.log” `;

#print “\nRotating $DATE\n”;

foreach $log (@log) {
chomp $log;
$new_fn=”$log”.”-”.”$DATE”;
#print “$log $new_fn\n”;
`mv “$log” “$new_fn”`;

}

@raw=`/usr/bin/find $DIR -type f -name “*$DATE*”`;

foreach $raw (@raw) {
chomp $raw;
#print “$raw\n”;
`gzip -9 $raw`;

}

EDIT:

Modified version:

#!/usr/bin/perl

$DIR=”/var/www/html/sites/logs”;
$DATE=`date +%F`;
chomp $DATE;

@log = `/usr/bin/find $DIR -type f -name “*.log” `;

#print “\nRotating $DATE\n”;

foreach $log (@log) {
chomp $log;
$new_fn=”$log”.”-”.”$DATE”;
#print “$log $new_fn\n”;
`mv “$log” “$new_fn”`;
`gzip -9 $new_fn`;

}

Popularity: 7% [?]

Tip: Using dig to Show Nameservers

If the need to scan a list of domains arises, be it one or one hundred, I have one handy Perl script that will list all nameservers of a domain. This script uses the command dig, and filters and lists the result. Do note that Perl must be installed to do this script.

# vi mx_checker.pl

#!/usr/bin/perl

$file=$ARGV[0];
chomp($file);

@list = `cat $file`;

foreach $domain(@list) {
chomp($domain);
$ns = `dig ns $domain +short`;
chomp($ns);
$ns =~s/\n/\t/g;
print “$domain\t$ns\n”;
}

To make the script executable, change the permissions:

# chmod 700 mx_checker.pl

Then build the list of domains by listing it on a text file, one domain per file.

# vi domains.txt

google.com
yahoo.com
usautoparts.com

Now that the script and domain list is ready, it is time to execute the script:

# ./mx_checker.pl domains.txt

The nameservers will then be listed and can be piped to a text file, which can be exported to a spreadsheet as tab-separated values.

# ./mx_checker.pl domains.txt >> nameservers.xls

Popularity: 6% [?]

Tip: Perl script to Check Site Availability

System Administrators’s ‘common tasks’ are usually monitoring numbers of servers and network connections, and doing each monitoring is rather tedious. So to make our jobs and lives easier, we write scripts to automate repetitive tasks.

Since this is my Linux blog anyway, I decided to share one of scripts that I use in the office. This one is the Perl script that I use to check a specific site if the keyword is present, hence confirming that the site is up and accessible. Read the rest of this entry »

Popularity: 10% [?]