I have a perl script that reads our firewall access logs to see who has used a specific vpn account. This works fine when run from the command line, but fails when run from crontab. The code is here
#!/usr/bin/perl -w
use strict;
use warnings;
use MIME::Lite::TT::HTML;
my $basedir = "/var/log";
my @verdir = qw(fw_d);
my $fulldir;
my $configs;
my $combidir;
my @results;
my @files1;
foreach $combidir (@verdir) {
$fulldir = "$basedir/$combidir";
opendir (DIR, $fulldir);
my @files = grep { $_ ne '.' && $_ ne '..' && $_ ne 'CVS' } readdir DIR;
closedir (DIR);
@files1 = sort {expand($a)cmp expand($b)}(@files);
foreach my $configs (@files1) {
my $now = time;
my @stat = stat("$fulldir/$configs");
if ($stat[9] > ($now - 2592000)) {
system("/usr/bin/less -f $fulldir/$configs | /bin/grep NETOPS_TUNNEL >> /usr/local/CCS/ravpn/output.log"); }
}
}
results();
sendmailnew(\@results);
sub results{
my $input = "/usr/local/CCS/ravpn/output.log";
open my $fh, "<", "$input" or die "could not open '$input': $!";
while (<$fh>){
if ($_ =~ /(................)fw.*(Group = NETOPS_TUNNEL). (Username = .(authenticated.)/){
push (@results, "$1 $2 $3 $4<br>")
}
}
return @results
}
sub expand {
my $file=shift;
$file=~s{(\d+)}{sprintf "%04d", $1}eg; # expand all numbers to 4 digits
return $file;
}
sub sendmailnew {
my %params;
my @results = @{$_[0]};
$params{sorted} = "@results";
my %options;
$options{INCLUDE_PATH} = '/usr/local/CCS/ravpn/templates/';
my $msg = MIME::Lite::TT::HTML->new(
From => "",
#To => "",
BCC => "",
Subject => "",
Encoding => 'quoted-printable',
Template => {
text => 'test.txt.tt',
html => 'sort.html.tt',
},
TmplOptions => \%options,
TmplParams => \%params,
);
$msg->send;
system("rm -rf /usr/local/CCS/ravpn/output.log");
}
When this is run from the command line, it goes to the directory /var/log/fw_d, gets all entries that are less than 30 days old, and passes them to have less and grep run against them, the results are outputted to output.log. (this file is created when it is run via crontab, but nothing is ever outputted). When run from the command line, entries are added to the output.log
Once the output.log is created, that is scanned for a specific set of items, and the results are pushed into an array called results, which is then emailed out.
This script works great if run manually, but if run from crontab, either the user we use to create the logs, or the root user, it fails to output anything to the output.log file.
I have a feeling its going to be something simple, but im not sure.
this is the crontab -e entry
40 03 * * * perl /usr/local/CCS/ravpn/ravpn.pl
The files that are being read are either plain text, or zipped text files, .gz, so i needed a way of reading them without unzipping hence the use of less.
Any help is appreciated.
Try to replace
with
Less is not meant to run without terminal. There is no need to do that in your case. Even better if you do this operation inside perl without
systemcall using perl own functions:PS: I see that you use /usr/local/CCS/ravpn/output.log as a temporary file. There is no need for that file based on what this script does. It makes your script even simpler.
Edit: works with .gz files too. You can use IO::Uncompress::Gunzip also but this is more complicated already.