Advertisement
Guest User

Untitled

a guest
Oct 6th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. no warnings;
  5.  
  6. use Net::XMPP::Client;
  7.  
  8. my $debug = 0;
  9.  
  10. my $cmd = 'dmesg';
  11. my $history_file = '/var/log/dmesg.db';
  12.  
  13. sub send_XMPP {
  14. my $jabber_con = new Net::XMPP::Client();
  15. $jabber_con->Connect( hostname => 'domain.com' );
  16. $jabber_con->AuthSend(
  17. username => 'username',
  18. password => 'pASsW0rD',
  19. resource => 'resource'
  20. );
  21. my $msg = new Net::XMPP::Message();
  22. $msg->SetMessage(
  23. to => 'somebody@domain.com',
  24. from => 'this_machine@domain.com',
  25. type => 'chat',
  26. body => $_[0]
  27. );
  28. $jabber_con->Send( $msg );
  29. $jabber_con->Disconnect();
  30. }
  31.  
  32.  
  33. open DMESG, '-|', $cmd or die "$!";
  34. my @dmesg_lines = <DMESG>;
  35. close DMESG;
  36.  
  37. my @hist_lines;
  38. if ( -r $history_file ) {
  39. open HIST, '<', $history_file or die "$!";
  40. @hist_lines = <HIST>;
  41. close HIST;
  42. }
  43.  
  44. my @new_lines;
  45.  
  46. my $hist_cursor = $#hist_lines;
  47.  
  48. my $hist_lines_count = scalar @hist_lines;
  49.  
  50. print "dmesg lines: $#dmesg_lines\n" if $debug;
  51.  
  52. SCAN_HIST:
  53. for my $idx ( reverse 0..$#dmesg_lines ) {
  54. if ( $hist_lines_count > 0 ) {
  55. if ( $dmesg_lines[$idx] =~ m/^\[[\s\t]*(\d+\.\d+)\]\s(.*)$/
  56. && $hist_lines[$hist_cursor] =~ m/^\[[\s\t]*(\d+\.\d+)\]\s(.*)$/ ) {
  57.  
  58. if ( $dmesg_lines[$idx] eq $hist_lines[$hist_cursor] ) {
  59. # Deside that changes are finished
  60. last SCAN_HIST;
  61. } else {
  62. push @new_lines, $dmesg_lines[$idx];
  63. }
  64. }
  65. } else {
  66. print "No history to compare, treating as new record\n" if $debug;
  67. push @new_lines, $dmesg_lines[$idx];
  68. }
  69. }
  70.  
  71. if ( @new_lines > 0 ) {
  72. print "Writing ",@new_lines+0," lines\n" if $debug;
  73. open HIST, '>>', $history_file or die "$!";
  74. foreach my $idx ( reverse 0 .. $#new_lines ) {
  75. print HIST $new_lines[$idx];
  76. print $new_lines[$idx] if $debug;
  77. }
  78. send_XMPP( join( '', reverse @new_lines) );
  79. } else {
  80. print "There is no changes\n" if $debug;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement