1
0

syslog-stat.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitriev, rnd@rajven.ru
  4. #
  5. use utf8;
  6. use warnings;
  7. use Encode;
  8. use open qw(:std :encoding(UTF-8));
  9. no warnings 'utf8';
  10. use English;
  11. use base;
  12. use FindBin '$Bin';
  13. use lib "/opt/Eye/scripts";
  14. use strict;
  15. use Time::Local;
  16. use FileHandle;
  17. use eyelib::config;
  18. use eyelib::main;
  19. use eyelib::database;
  20. use eyelib::common;
  21. use eyelib::logconfig;
  22. use Data::Dumper;
  23. use DBI;
  24. use Time::Local;
  25. use Date::Parse;
  26. use IO::Socket::UNIX qw( SOCK_STREAM );
  27. use Cwd;
  28. my $socket_path='/run/syslog-ng.socket';
  29. wrlog($W_INFO,"Starting...");
  30. setpriority(0,0,19);
  31. my %trash_patterns = (
  32. 'Receive illegal destination ip packet 255.0.0.0 ,drop it' =>'1',
  33. 'Receive illegal destination ip packet 0.0.0.0 ,drop it' =>'1',
  34. 'SD Normal' =>'1',
  35. 'SD Abnormal' =>'1',
  36. 'source:0.0.0.0 destination:0.0.0.0 user:admin cmd:login' =>'1',
  37. 'FAN\'S speed level - 1 changed to level - 0.' => '1',
  38. 'FAN\'S speed level - 0 changed to level - 1.' => '1',
  39. "Environment-I-FANS-SPEED-CHNG: FAN'S speed level"=>'1'
  40. );
  41. my %warning_patterns = (
  42. 'SHUTDOWN-CTRL' => '1',
  43. 'PORT_FLOW' => '1',
  44. 'System ColdStart' => '1',
  45. 'Deny user/' => '1',
  46. 'LOOP-BACK-DETECTED' => 'loop',
  47. 'Find loop' =>'loop',
  48. 'SYS-5-LOOP' => 'loop',
  49. 'drifting from' => 'loop',
  50. 'Port-security has reached' => '1',
  51. 'Unauthenticated IP-MAC' => '1',
  52. 'FAN_FAILED' => '0',
  53. 'has the same IP Address' => '1',
  54. 'Loop detected on port e0' => 'loop',
  55. 'loopguard' => 'zyxel_loop',
  56. 'without management command' => '1',
  57. 'System cold start' =>'1',
  58. 'topology changes' => '1',
  59. 'HMON-0-power'=>'1',
  60. 'On battery power in response to an input power problem'=>'1',
  61. 'No longer on battery power'=>'1',
  62. 'Environment-W-PS-STAT-CHNG'=>'1',
  63. 'System warm start' => '1'
  64. );
  65. while (1) {
  66. eval {
  67. my $db = init_db();
  68. open(SYSLOG,$socket_path) || die("Error open fifo socket $socket_path: $!");
  69. while (my $logline = <SYSLOG>) {
  70. next unless defined $logline;
  71. chomp($logline);
  72. my ($timestamp,$host_ip,$message) = split (/\|/, $logline);
  73. next if (!$message);
  74. $message =~ s/\r/ /g;
  75. $message =~ s/\\015//g;
  76. $message =~ s/\\012//g;
  77. next if (!$message);
  78. next if (!$host_ip);
  79. if (time()-$last_refresh_config>=60) { init_option($db); }
  80. log_debug("Raw message: $message");
  81. #is trash messages?
  82. my $trash = 0;
  83. foreach my $pattern (keys %trash_patterns) {
  84. next if (!$pattern);
  85. if ($message=~/$pattern/i) {
  86. log_debug("Trash pattern: $pattern");
  87. $trash = 1;
  88. last;
  89. }
  90. }
  91. next if ($trash);
  92. my $hostname=$host_ip;
  93. my $netdev = get_device_by_ip($db,$host_ip);
  94. my $id = 0;
  95. if ($netdev) {
  96. $hostname = $netdev->{device_name};
  97. $id = $netdev->{id};
  98. } else {
  99. log_debug("Host with $host_ip is not found in netdevices!");
  100. }
  101. my $q_msg=$db->quote($message);
  102. my $ssql="INSERT INTO remote_syslog(device_id,ip,message) values(?,?,?)";
  103. do_sql($db,$ssql,$id,$host_ip,$q_msg);
  104. foreach my $pattern (keys %warning_patterns) {
  105. next if (!$pattern);
  106. if ($message=~/$pattern/i) {
  107. wrlog($W_INFO,"Warning pattern $pattern found! Send email.",1);
  108. sendEmail("Syslog warning for $hostname [".$host_ip."]!",$host_ip." ".$message);
  109. last;
  110. }
  111. }
  112. }
  113. close(SYSLOG);
  114. };
  115. if ($@) { wrlog($W_ERROR,"Exception found: $@"); sleep(60); }
  116. }
  117. wrlog($W_INFO,"Process stopped.");
  118. exit 0;