
Twitter Perl Script - by IamVarr
This was posted on the forums by IamVarr and I wanted to make sure its archived as a tutorial. Thanks again Iamvarr!
The following perl script can be run on a server to monitor error logs via rss feeds on twitter.
#!/usr/bin/perl
# tail.pl
# the follow line needs to be in /usr/local/bin filename twitter
# curl --basic --user "<username>:<password>" --data-ascii "status=`echo $@|tr ' ' '+'`" "http://twitter.com/statuses/update.json"
# libfile-perl module needed to run script
#############################################################################
use warnings;
use strict;
use File::Tail;
my $line;
my $name = "/var/log/apache2/error.log";
my $file=File::Tail->new(name=>$name, maxinterval=>10, adjustafter=>7);
while (defined($line=$file->read)) {
# if ($line){ # commented out to get error
# my $error = 'There is error in the error.log'; # comment out to get real error
system('twitter', "$line"); # replace $line with $error for custom message
# }
}
# EOF
The file can be modified to either give a custom message or the real error. You can modify the file to fliter using m/// regex to only post wanted error messages. I have included in the header the command used by the script, which is saved in the location mention. The twitter CLI information was found here: http://binnyva.blogspot.com/2007/03/using-twitter-part-1-command-line.ht...
There is also a perl module required, which you have Perl installed all ready and use apt-get the following command :
sudo apt-get install libfile-tail-perl
Then you will need to have a twitter account for each server and be sure to check the box to not make the post public. Once to script is started by typing : perl tail.pl : you can scribe to the feeds to follow your servers and about every 30 seconds the log files are checked. Should you want to download the Perl module from the CPAN by going to:
http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm.
In the download there is a script to monitor several files at once.
Enjoy,
Iamvarr
