Greetings everyone!
I've been running into a bit of a pickle as of late. I've recently set up a router on a ZBOX, running PFSense. Now, I want to use PERL to write a little monitoring script that pushes messages to SLACK (a service my company is using to communicate)
Now, to get to the point, I'm using the module DateTime in my script. However:
Can't locate DateTime/Format/Parse.pm in @INC (you may need to install the DateTime::Format::Parse module) (@INC contains: /root/.cpan /usr/local/bin /usr/local/lib/perl5/site_perl/mach/5.20 /usr/local/lib/perl5/site_perl /usr/local/lib/perl5/5.20/mach /usr/local/lib/perl5/5.20 /usr/local/lib/perl5/site_perl/5.20 /usr/local/lib/perl5/site_perl/5.20/mach .) at /usr/local/test/LogfileMonitor/LogfileMonitor.pl line 8.
So far, most of my perl modules were installed using the FreeBSD package manager, which always worked favorably since I'm too... helpless to get CPAN to work on this particular installation. That is, CPAN itself is working, downloads things, builds them, installs them all no problem as far as I can tell. But whenever I try to use them in my code they aren't found. I tried fiddling with the paths too, which is why /root/.cpan is in my @INC path.
I'm not quite sure how to phrase a concrete question here, but does anyone have an idea on how I could go about fixing this?
Here is the full code:
package LogfileMonitor;
use strict;
use warnings;
use IO::File;
use File::ReadBackwards;
use DateTime;
use DateTime::Format::DateParse;
use LWP::UserAgent;
#Don't forget trailing slash.
my $filePath = '/var/log/';
my @fileNames = ('gateways.log', 'system.log');
my $slackHandle = my handle
my $token = my token
# main Function
sub LFM(){
my $run = 1;
my $test = 0;
while($run && $test < 2){
for(my $i = 0; $i <= @fileNames; $i++){
sendMessage($slackHandle, scanFile($filePath,$fileNames[$i
+]));
sleep(300);
}
$test++;
}
}
# get relevant Lines from Logfile
sub scanFile(){
my $currentTime = DateTime->now(time_zone => "local");
my $handle = File::ReadBackwards->new($_[0] . $_[1]);
#print $handle;
print "\n";
print $_[0].$_[1];
print "\n\n\n";
for(my $i = 0; $i <= 15; $i++) {
my $line = $handle->readline;
if(defined $line) {
my $messageTime = substr($line, 0, 15);
my $messageAgent = substr($line, 16, index($line, ':'));
my $message = substr($line, index($line, ':', 16));
$messageTime = DateTime::Format::DateParse->parse_datetime
+($messageTime);
my $diff = $currentTime->substract($messageTime);
if ($diff->minutes() <= 5){
return $line;
} else {
return undef;
}
}
}
}
# push Message to Slack
sub sendMessage(){
my $Messages = $_[1];
#my $slackHandle = @_[0];
#my $userAgent = LWP::UserAgent->new();
#my $data = qq{{ "text" : "@_[1]"}};
#my $httpRequest = HTTP::Request->new(POST=>$slackHandle);
#$httpRequest->header('content-type' => 'application/json');
#$httpRequest->content($data);
#my $response = $userAgent->request($httpRequest);
#if ($response->is_success){
# print $response->decoded_content;
#}
#else
#{
# print $response->code;
# print $response->message;
#}
print $Messages;
}
#Call main Function
LFM();
It's nowhere near done, and probably not efficient, but it should suffice to produce some kind of output, shouldn't it?
EDIT:
I fixed up the code some, now I get another error though.
Can't call method "time_zone" on an undefined value at /usr/local/lib/
+perl5/site_perl/mach/5.20/DateTime.pm line 1376.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.