syslog-stat.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/perl
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use FindBin '$Bin';
  6. use lib "$Bin";
  7. use strict;
  8. use Time::Local;
  9. use FileHandle;
  10. use Rstat::config;
  11. use Rstat::main;
  12. use Rstat::mysql;
  13. use Data::Dumper;
  14. use DBI;
  15. use Time::Local;
  16. use Date::Parse;
  17. use Getopt::Long;
  18. use Proc::Daemon;
  19. use Cwd;
  20. my $pf = '/var/run/syslog-stat.pid';
  21. my $log = '/var/log/remote/messages.log';
  22. my $daemon = Proc::Daemon->new(
  23. pid_file => $pf,
  24. work_dir => $HOME_DIR
  25. );
  26. # are you running? Returns 0 if not.
  27. my $pid = $daemon->Status($pf);
  28. my $daemonize = 1;
  29. GetOptions(
  30. 'daemon!' => \$daemonize,
  31. "help" => \&usage,
  32. "reload" => \&reload,
  33. "restart" => \&restart,
  34. "start" => \&run,
  35. "status" => \&status,
  36. "stop" => \&stop
  37. ) or &usage;
  38. exit(0);
  39. sub stop {
  40. if ($pid) {
  41. print "Stopping pid $pid...";
  42. if ($daemon->Kill_Daemon($pf)) {
  43. print "Successfully stopped.\n";
  44. } else {
  45. print "Could not find $pid. Was it running?\n";
  46. }
  47. } else {
  48. print "Not running, nothing to stop.\n";
  49. }
  50. }
  51. sub status {
  52. if ($pid) {
  53. print "Running with pid $pid.\n";
  54. } else {
  55. print "Not running.\n";
  56. }
  57. }
  58. sub run {
  59. if (!$pid) {
  60. print "Starting...";
  61. if ($daemonize) {
  62. # when Init happens, everything under it runs in the child process.
  63. # this is important when dealing with file handles, due to the fact
  64. # Proc::Daemon shuts down all open file handles when Init happens.
  65. # Keep this in mind when laying out your program, particularly if
  66. # you use filehandles.
  67. $daemon->Init;
  68. }
  69. setpriority(0,0,19);
  70. $SPID=~s/\.pl$/\.pid/;
  71. write_to_file($SPID,$$);
  72. my %trash_patterns = (
  73. 'Receive illegal destination ip packet 255.0.0.0 ,drop it' =>'1',
  74. 'Receive illegal destination ip packet 0.0.0.0 ,drop it' =>'1',
  75. 'SD Normal' =>'1',
  76. 'SD Abnormal' =>'1',
  77. 'source:0.0.0.0 destination:0.0.0.0 user:admin cmd:login' =>'1',
  78. 'FAN\'S speed level - 1 changed to level - 0.' => '1',
  79. 'FAN\'S speed level - 0 changed to level - 1.' => '1',
  80. "Environment-I-FANS-SPEED-CHNG: FAN'S speed level"=>'1'
  81. );
  82. my %warning_patterns = (
  83. 'SHUTDOWN-CTRL' => '1',
  84. 'PORT_FLOW' => '1',
  85. 'System ColdStart' => '1',
  86. 'Deny user/' => '1',
  87. 'LOOP-BACK-DETECTED' => 'loop',
  88. 'Find loop' =>'loop',
  89. 'SYS-5-LOOP' => 'loop',
  90. 'drifting from' => 'loop',
  91. 'Port-security has reached' => '1',
  92. 'Unauthenticated IP-MAC' => '1',
  93. 'FAN_FAILED' => '0',
  94. 'has the same IP Address' => '1',
  95. 'Loop detected on port e0' => 'loop',
  96. 'loopguard' => 'zyxel_loop',
  97. 'without management command' => '1',
  98. 'System cold start' =>'1',
  99. 'topology changes' => '1',
  100. 'HMON-0-power'=>'1',
  101. 'On battery power in response to an input power problem'=>'1',
  102. 'No longer on battery power'=>'1',
  103. 'Environment-W-PS-STAT-CHNG'=>'1',
  104. 'System warm start' => '1'
  105. );
  106. while (1) {
  107. eval {
  108. # Create new database handle. If we can't connect, die()
  109. system('touch "'.$log.'"');
  110. my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  111. if ( !defined $dbh ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  112. open(SYSLOG, "tail -n 0 -F $log |") || die "$log not found!";
  113. while (my $logline = <SYSLOG>) {
  114. next unless defined $logline;
  115. chomp($logline);
  116. my ($timestamp,$host_ip,$message) = split (/\|/, $logline);
  117. next if (!$message);
  118. $message =~ s/\r/ /g;
  119. $message =~ s/\\015//g;
  120. $message =~ s/\\012//g;
  121. next if (!$message);
  122. next if (!$host_ip);
  123. if (time()-$last_refresh_config>=60) { init_option($db); }
  124. log_debug("Raw message: $message");
  125. #is trash messages?
  126. my $trash = 0;
  127. foreach my $pattern (keys %trash_patterns) {
  128. next if (!$pattern);
  129. if ($message=~/$pattern/i) {
  130. log_debug("Trash pattern: $pattern");
  131. $trash = 1;
  132. last;
  133. }
  134. }
  135. next if ($trash);
  136. my $hostname=$host_ip;
  137. my $netdev = get_device_by_ip($db,$host_ip);
  138. my $id = 0;
  139. if ($netdev) {
  140. $hostname = $netdev->{device_name};
  141. $id = $netdev->{id};
  142. } else {
  143. log_debug("Host with $host_ip is not found in netdevices!");
  144. }
  145. my $q_msg=$db->quote($message);
  146. my $ssql="INSERT INTO remote_syslog(device_id,ip,message) values('".$id."','".$host_ip."',".$q_msg.")";
  147. do_sql($db,$ssql);
  148. foreach my $pattern (keys %warning_patterns) {
  149. next if (!$pattern);
  150. if ($message=~/$pattern/i) {
  151. log_info("Warning pattern $pattern found! Send email.",1);
  152. sendEmail("Syslog warning for $hostname [".$host_ip."]!",$host_ip." ".$message);
  153. last;
  154. }
  155. }
  156. }
  157. close(SYSLOG);
  158. };
  159. if ($@) { log_error("Exception found: $@"); }
  160. }
  161. } else {
  162. print "Already Running with pid $pid\n";
  163. }
  164. }
  165. sub usage {
  166. print "usage: syslog-monitord.pl (start|stop|status|restart)\n";
  167. exit(0);
  168. }
  169. sub reload {
  170. print "reload process not implemented.\n";
  171. }
  172. sub restart {
  173. stop;
  174. run;
  175. }