1
0

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