Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- no warnings;
- use Net::XMPP::Client;
- my $debug = 0;
- my $cmd = 'dmesg';
- my $history_file = '/var/log/dmesg.db';
- sub send_XMPP {
- my $jabber_con = new Net::XMPP::Client();
- $jabber_con->Connect( hostname => 'domain.com' );
- $jabber_con->AuthSend(
- username => 'username',
- password => 'pASsW0rD',
- resource => 'resource'
- );
- my $msg = new Net::XMPP::Message();
- $msg->SetMessage(
- to => 'somebody@domain.com',
- from => 'this_machine@domain.com',
- type => 'chat',
- body => $_[0]
- );
- $jabber_con->Send( $msg );
- $jabber_con->Disconnect();
- }
- 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;
- }
- 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] ) {
- # Deside 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 "$!";
- foreach my $idx ( reverse 0 .. $#new_lines ) {
- print HIST $new_lines[$idx];
- print $new_lines[$idx] if $debug;
- }
- send_XMPP( join( '', reverse @new_lines) );
- } else {
- print "There is no changes\n" if $debug;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement