syslog-stat.pl 5.3 KB

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