Last active
October 17, 2016 00:17
-
-
Save SergeyStorm/543a102e8f5056eb738391e6f3686bfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# Sergey Storm, 6 of October 2016 at 22:21:22 | |
use strict; | |
use warnings; | |
use Net::XMPP::Client; | |
my $debug = 0; | |
my $cmd = 'dmesg'; | |
my $history_file = '/var/log/dmesg.db'; | |
my $history_depth = 10000; | |
my $compressor = 'xz'; | |
my $compress = 1; | |
sub send_XMPP { | |
my $jcon = new Net::XMPP::Client(); | |
$jcon->Connect( hostname => 'domain.com' ); | |
$jcon->AuthSend( | |
username => 'username', | |
password => 'pASsW0rD', | |
resource => 'resource' | |
); | |
my $msg = new Net::XMPP::Message(); | |
$msg->SetMessage( | |
to => 'username@domain.com', | |
from => 'server@domain.com', | |
type => 'chat', | |
body => $_[0] | |
); | |
$jcon->Send( $msg ); | |
$jcon->Disconnect(); | |
} | |
sub main { | |
open DMESG, '-|', $cmd or die "$!"; | |
my @dmesg_lines = <DMESG>; | |
close DMESG; | |
my @hist_lines; | |
if ( -r $history_file ) { | |
open HIST, '<', $history_file or die "$!"; | |
@hist_lines = <HIST>; | |
close HIST; | |
# Rotate log | |
if ( @hist_lines >= $history_depth ) { | |
print "Rotating history file\n" if $debug; | |
my $new_name = $history_file.'.'.time(); | |
rename $history_file, $new_name; | |
if ( $compress ) { | |
my $res = `$compressor $new_name`; | |
if ( $debug ) { | |
if ( $? ) { | |
print "Warning, rotated file compression failed: $res\n"; | |
} else { | |
print "Rotated file compression successful\n"; | |
} | |
} | |
} | |
} | |
} | |
my @new_lines; | |
my $hist_cursor = $#hist_lines; | |
my $hist_lines_count = scalar @hist_lines; | |
print "dmesg lines: $#dmesg_lines\n" if $debug; | |
SCAN_HIST: | |
for my $idx ( reverse 0..$#dmesg_lines ) { | |
if ( $hist_lines_count > 0 ) { | |
if ( $dmesg_lines[$idx] =~ m/^\[[\s\t]*(\d+\.\d+)\]\s(.*)$/ | |
&& $hist_lines[$hist_cursor] =~ m/^\[[\s\t]*(\d+\.\d+)\]\s(.*)$/ ) { | |
if ( $dmesg_lines[$idx] eq $hist_lines[$hist_cursor] ) { | |
# Decide that changes are finished | |
last SCAN_HIST; | |
} else { | |
push @new_lines, $dmesg_lines[$idx]; | |
} | |
} | |
} else { | |
print "No history to compare, treating as new record\n" if $debug; | |
push @new_lines, $dmesg_lines[$idx]; | |
} | |
} | |
if ( @new_lines > 0 ) { | |
print "Writing ",@new_lines+0," lines\n" if $debug; | |
open HIST, '>>', $history_file or die "$!"; | |
my $prev_msg = ''; | |
my @notify_lines; | |
# Continuous repeats | |
my $cont_rep = 0; | |
foreach my $idx ( reverse 0 .. $#new_lines ) { | |
print HIST $new_lines[$idx]; | |
if ( $new_lines[$idx] =~ m/^\[[\s\t]*(\d+\.\d+)\]\s(.*)$/ ) { | |
my $time = $1; | |
my $msg = $2; | |
if ( $msg eq $prev_msg ) { | |
$cont_rep++; | |
} else { | |
if ( $cont_rep > 2 ) { | |
push @notify_lines, 'Last message repeated '.($cont_rep+1)." times\n"; | |
push @notify_lines, $new_lines[$idx]; | |
} else { | |
push @notify_lines, $new_lines[$idx]; | |
} | |
$cont_rep = 0; | |
} | |
$prev_msg = $msg; | |
} | |
} | |
if ( $cont_rep > 0 ) { | |
push @notify_lines, 'Last message repeated '.($cont_rep+1)." times\n"; | |
} | |
send_XMPP( join( '', @notify_lines ) ); | |
} else { | |
print "There is no changes\n" if $debug; | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment