syslog-stat.pl 5.2 KB

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